python - Tkinter Show webcam view in second window -
i using webcam view , performing analysis on images taken in. wish introduce functionality window can summoned , user can @ webcam view in new window, should desire. attempt causes buttons in main window swap on instance when open new window. what's going wrong?
here (working) example:
import tkinter tk import cv2 pil import image, imagetk class camview(): def __init__(self, parent): self.parent = parent self.window = tk.toplevel(parent) self.window.protocol("wm_delete_window", self.close) self.show_frame() def show_frame(self): imgtk = imagetk.photoimage(image=self.parent.img) lmain.imgtk = imgtk lmain.configure(image=imgtk) def close(self): self.parent.test_frame = none self.window.destroy() root = tk.tk() root.bind('<escape>', lambda e: root.quit()) lmain = tk.label(root) lmain.pack() class main(tk.frame): def __init__(self, parent): self.test_frame = none frame = tk.frame.__init__(self,parent) = tk.label(text='hello!').pack() b = tk.button(frame, text='open', command=self.load_window) b.pack() width, height = 800, 600 self.cap = cv2.videocapture(0) self.cap.set(cv2.cap_prop_frame_width, width) self.cap.set(cv2.cap_prop_frame_height, height) self.do_stuff() def do_stuff(self): _, frame = self.cap.read() frame = cv2.flip(frame, 1) cv2image = cv2.cvtcolor(frame, cv2.color_bgr2rgba) self.img = image.fromarray(cv2image) if self.test_frame != none: self.test_frame.show_frame() lmain.after(10, self.do_stuff) def load_window(self): self.test_frame = camview(self) control = main(root) root.mainloop()
in real code, working example - seems when load new window, places webcam frame in first window when don't want to!
fixed! getting confused because of self.lmain
. here working code:
import tkinter tk import cv2 pil import image, imagetk class camview(): def __init__(self, parent): self.parent = parent self.window = tk.toplevel(parent) self.lmain2 = tk.label(self.window) self.lmain2.pack() self.window.protocol("wm_delete_window", self.close) self.show_frame() def show_frame(self): imgtk = imagetk.photoimage(image=self.parent.img) self.lmain2.imgtk = imgtk self.lmain2.configure(image=imgtk) def close(self): self.parent.test_frame = none self.window.destroy() root = tk.tk() root.bind('<escape>', lambda e: root.quit()) class main(tk.frame): def __init__(self, parent): self.lmain = tk.label(parent) self.lmain.pack() self.test_frame = none frame = tk.frame.__init__(self,parent) = tk.label(text='hello!').pack() b = tk.button(frame, text='open', command=self.load_window) b.pack() width, height = 800, 600 self.cap = cv2.videocapture(0) self.cap.set(cv2.cap_prop_frame_width, width) self.cap.set(cv2.cap_prop_frame_height, height) self.do_stuff() def do_stuff(self): _, frame = self.cap.read() frame = cv2.flip(frame, 1) cv2image = cv2.cvtcolor(frame, cv2.color_bgr2rgba) self.img = image.fromarray(cv2image) if self.test_frame != none: self.test_frame.show_frame() self.lmain.after(10, self.do_stuff) def load_window(self): if self.test_frame == none: self.test_frame = camview(self) control = main(root) root.mainloop()
Comments
Post a Comment