Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

python - How to get an array of tkinter custom GUI objects

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...