I am trying to get an array of a custom tkinter object (it is a GUI window that shows a video stream).
I am running into errors with the code I tried to write.
I made a function to make the custom object and tried to make an array of those objects.
I am getting the following error:
File "array_videoFeeds.py", line 60, in
app = FeedCams(root) File "array_videoFeeds.py", line 8, in init
self.initialize() File "array_videoFeeds.py", line 54, in initialize
self.b = self.videowindow() File "array_videoFeeds.py", line 11, in videowindow
self.videowindow = window NameError: name 'window' is not defined
Here is my python code:
import tkinter as Tk
import numpy as np
class FeedCams(Tk.Frame):
def __init__(self,parent):
Tk.Frame.__init__(self, parent)
self.parent = parent
self.initialize()
def videowindow(self):
self.videowindow = window
self.cap = cap
self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
self.interval = 20 # Interval in ms to get the latest frame
# Create canvas for image
self.canvas = tk.Canvas(self.window, width=self.width, height=self.height)
# self.canvas.grid(row=0, column=0)
# Update image on canvas
self.update_image()
dynamic_windows.append(videowindow)
def update_image(self):
# Get the latest frame and convert image format
self.image = cv2.cvtColor(self.cap.read()[1], cv2.COLOR_BGR2RGB) # to RGB
self.image = Image.fromarray(self.image) # to PIL format
self.image = ImageTk.PhotoImage(self.image) # to ImageTk format
# Update image
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image)
# Repeat every 'interval' ms
self.videowindow.after(self.interval, self.update_image)
def initialize(self):
'''
Draw the GUI
'''
self.parent.title("RUN ON START TEST")
self.parent.grid_rowconfigure(1,weight=1)
self.parent.grid_columnconfigure(1,weight=1)
self.frame = Tk.Frame(self.parent)
self.frame.pack(fill=Tk.X, padx=5, pady=5)
# Create a array of videos
self.a = np.zeros((3,3))
for i in range(0,self.a.shape[0]):
for j in range(0,self.a.shape[1]):
self.b = self.videowindow()
self.b.grid(row=i, column= j)
# Start the main program here
if __name__ == "__main__":
root=Tk.Tk()
app = FeedCams(root)
root.mainloop()
How can I successfully make this work?
UPDATE:
I changed the line of code based on one of the replies to self.a = np.zeros((3,3)).
There error has been updated in the question.
question from:
https://stackoverflow.com/questions/65878378/how-to-get-an-array-of-tkinter-custom-gui-objects 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…