python - I am having trouble with Tkinter GUI control -


i have programme various controls, live webcam feed , matplotlib figure based on webcam data. controls located in left column, , webcam , matplotlib figure located in right column.

attempts use .grid() methods, canvases , frames etc don't work - python script fails execute , hangs indefinitely.

how can achieved?

minimum working example:

import tkinter tk  import cv2 pil import image, imagetk import numpy np  import matplotlib matplotlib.use('tkagg')  matplotlib.backends.backend_tkagg import figurecanvastkagg, navigationtoolbar2tkagg import matplotlib.pyplot plt matplotlib.figure import figure  root = tk.tk() root.bind('<escape>', lambda e: root.quit()) lmain = tk.label(root) lmain.pack()  class controller(tk.frame):     def __init__(self, parent=root, camera_index=0):         self.camera_index = 0          frame = tk.frame.__init__(self, parent,relief=tk.groove,width=100,height=100,bd=1)         self.pack()         self.parent = parent         self.var = tk.intvar()          self.parent.title('laser beam profiler')          labelframe = tk.labelframe(parent, text="this labelframe")         labelframe.pack(fill="both", expand="yes") #.grid(row=0, column=0)           self.plot = tk.button(labelframe, text = "plot", command = self.refresh_plot)         self.plot.pack()         self.exit = tk.button(labelframe, text = "exit", command = self.close_window, compound = tk.bottom)         self.exit.pack()          self.init_camera()         self.show_frame() #initialise camera         self.make_fig()      def make_fig(self):         self.fig = figure(figsize=(4,4), dpi=100)          self.ax = self.fig.add_subplot(111)           canvas = figurecanvastkagg(self.fig, self)          canvas.show()          canvas.get_tk_widget().pack(side=tk.bottom, fill=tk.both, expand=true)           toolbar = navigationtoolbar2tkagg(canvas, self)          toolbar.update()          canvas._tkcanvas.pack(side=tk.bottom, fill=tk.both, expand=true)      def refresh_plot(self):         self.ax.plot(self.img[0])         self.fig.canvas.draw()          self.ax.clear()         print 'updated plot'      def init_camera(self):         width, height = 400, 300         self.cap = cv2.videocapture(self.camera_index)         if not self.cap:             raise exception("camera not accessible")         self.cap.set(cv2.cap_prop_frame_width, width)         self.cap.set(cv2.cap_prop_frame_height, height)                  def show_frame(self):         _, frame = self.cap.read()         cv2image = cv2.cvtcolor(frame, cv2.color_bgr2rgba)          cv2.puttext(cv2image,"laser beam profiler", (50,50), cv2.font_hershey_simplex, 1, (255,255,255))         dim = np.shape(cv2image)          img = image.fromarray(cv2image)           imgtk = imagetk.photoimage(image=img)          lmain.imgtk = imgtk         lmain.configure(image=imgtk)         lmain.after(10, self.show_frame)          self.img = frame      def close_window(self):          self.parent.quit()         self.parent.destroy()  controller().mainloop() 

for minimum working example trying 'plot' , 'exit' buttons on left hand side, , webcam view matplotlib figure on right. places widgets in long column spills off screen.

the line

labelframe = tk.labelframe(parent, text="this labelframe") 

probably should have self instead of parent. after this

labelframe.pack(fill="both", expand="yes", tk.left) 

should work. since controller , labelframe had same parent , pack method controller called first, placed restrictions labelframe appear.

in controller.__init__() there call self.pack() in little bit unusual place, left called after widget has been instantiated i.e.

c = controller(root) c.pack() root.mainloop() 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -