Tkinter;新班的顶级 [英] Tkinter; Toplevel in a new class
本文介绍了Tkinter;新班的顶级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Python和Tkinter进行一个项目。我要对其进行模块化。
I'm working on a project using Python and Tkinter. I want to modularize it.
主要问题之一是我的 Toplevel
小部件的实现太大了。
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)
所以对于我的 Toplevel
小部件,我尝试过:
So for my Toplevel
widget I tried:
class Config(tk.Toplevel):
def __init__(self, main):
tk.Toplevel.__init__(self)
这样做是否正确?
推荐答案
是的,这是正确的方法。但是,您可能希望保留对窗口的引用,以便以后可以在其上调用方法:
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)
这篇关于Tkinter;新班的顶级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文