Thursday 15 July 2010

python - Tkinter nested mainloop -


I am writing a videoplayer with tkinter / python , so basically I have a GUI There is a video that can play. Now, I would like to apply a stop button, which means that I have a mainlop () for GUI and another nested mainlop () And return to the GUI startup window, it is called:

Event loop can be nested; It is OK to call menloop from within an event handler.

However, I do not understand how to implement this nesting victim. Can anyone provide me a simple example of such a script?

Edit

This is the working version of my code, because it seems that I am doing a few foreigners. Of course, I am a newbie, so I could have nested menopause I am making a mistake about the requirement. / code / process from the PIL import as CV, Qi import cv2 import cv2.cv image, IMAGETech import TK def as tinkler image_capture (queue): vidFile = cv2. Videocacent (0 ) While true: flag, frame = vidFile.read () frame = cv2.cvtColor (frame, cv2.cv.CV_BGR2RGB) queue.put (frame) cv2.waitKey (10) def update_all (root, imagelabel, queue, process, Var): if var.get () == true: im = queue .get () a = Image.fromarray (im) b = ImageTk.PhotoImage (image = a) imagelabel.configure (image = b) imagelabel._image_cache = B # Root Collection Root. Avoid root.update () root.after (0, Func = lambda: update_all (root, imagelabel, queue, process, var)) Other: print var.get () root.quit () def playvideo (root, imagelabel, Queue, var: print 'start' p = process (target = image_capture, args = (task,)) p.start () update_all (root, imagelabel, queue, p, var) print 'nes enter ted mainloop' root .mainloop () p.terminate () if var.get () == incorrect: im = ImageTk.PhotoImage (file = 'logo.png') imagelabel.configure (image = IM) imagelabel._image_cache = im # Trash archive root . Update () var.set (True) Print 'Finishing', if '__name__ ==' __main__ ': #Initialize multiprocessing function = line () #GUI root window root = tk.Tk () # the image container image_label = Tk.Label (master = root) image_label.grid (column = 0, line = 0, column = 2, rowspan = 1) with image #fill label until the video is loaded bg_im = ImageTk.PhotoImage (file = 'logo .png ') image_label [' image '] = bg_im #frame button button_frame = tk.frame (root) button_frame.grid (column = 0, line = 1, column = 1) # Video to load video and a switch Wait for Videopath to be selected Load_button = tk.button (master = button_frame, text = 'load video', command = lambda: playvideo (root, image_lable, task, switch)) load_button.grid (column = 0, line = 0, sticky = 'ew') #Stop Button Switch = Vaccines BooleanWar (master = root, value = true, name = 'switch') stop_bu Tton = tk.button (master = button_frame, text = 'stop', command = lambda: switch.set (false)) stop_button.grid (column = 0, row = 1, sticky = 'ew') #quit button quit_button = Tk.Button (master = button_frame, text = 'quit', command = root.destroy) quit_button.grid (column = 0, line = 2, sticky = 'Eve') root.mainloop ()

One of the nested menopause in this tanker The example should be:

  import commentator DEF main (): print 'main' T.Menlope () print 'and man' t = takenator Vaccine () b = tinator Button (t, command = main) bp () t. Menopause ()   

A new main ballple is executed whenever you press the button.

main
main code main # main window main end main end main end main end main

No comments:

Post a Comment