Tkinter; Toplevel in a new class

醉酒当歌 提交于 2020-01-24 16:46:05

问题


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:


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)


来源:https://stackoverflow.com/questions/25957601/tkinter-toplevel-in-a-new-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!