Tkinter; Toplevel in a new class

2019-07-08 21:50发布

I'm working on a project using Python and Tkinter. I want to modularize it.

One of the main problems is that the implementation of my Toplevel widget is too big.

I heard that it's possible to put this widget in a new class. The problem is I don't know how.

Here is how I define my main window:

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        Config(self)

So for my Toplevel widget I tried:

class Config(tk.Toplevel): 
   def __init__(self, main):
       tk.Toplevel.__init__(self)

Is it the right way to do this ?

1条回答
看我几分像从前
2楼-- · 2019-07-08 22:50

Yes, that is the right way to do it. Though, you might want to keep a reference to the window so you can call methods on it later:

self.config = Config(self)
查看更多
登录 后发表回答