Tkinter understanding mainloop

后端 未结 3 1194
离开以前
离开以前 2020-11-21 06:17

Till now, I used to end my Tkiter programs with: tk.mainloop(), or nothing would show up! See example:

from Tkinter import *
import random
impor         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 07:00

    I'm using an MVC / MVA design pattern, with multiple types of "views". One type is a "GuiView", which is a Tk window. I pass a view reference to my window object which does things like link buttons back to view functions (which the adapter / controller class also calls).

    In order to do that, the view object constructor needed to be completed prior to creating the window object. After creating and displaying the window, I wanted to do some initial tasks with the view automatically. At first I tried doing them post mainloop(), but that didn't work because mainloop() blocked!

    As such, I created the window object and used tk.update() to draw it. Then, I kicked off my initial tasks, and finally started the mainloop.

    import Tkinter as tk
    
    class Window(tk.Frame):
        def __init__(self, master=None, view=None ):
            tk.Frame.__init__( self, master )
            self.view_ = view       
            """ Setup window linking it to the view... """
    
    class GuiView( MyViewSuperClass ):
    
        def open( self ):
            self.tkRoot_ = tk.Tk()
            self.window_ = Window( master=None, view=self )
            self.window_.pack()
            self.refresh()
            self.onOpen()
            self.tkRoot_.mainloop()         
    
        def onOpen( self ):        
            """ Do some initial tasks... """
    
        def refresh( self ):        
            self.tkRoot_.update()
    

提交回复
热议问题