我正在使用Tkinter创建GUI。我有一个用于设置所有GUI元素的类和另一个用于执行某些功能的类。
NotificationMessage
上面的代码是否是调用其他类方法的正确方法,还是应该在GUI的class class_one():
def method_one(self):
do_something()
class GUI()
def __init__(self):
button = Button(self, text="button", command=call_class_one_method)
button.pack()
def call_class_one_method(self):
c = class_one()
c.method_one()
方法中实例化该类?或许别的什么?
答案 0 :(得分:1)
在这种特定情况下,您应该在GUI.__init__
中对其进行实例化,除非您需要在每次单击按钮时创建新实例。
class GUI()
def __init__(self):
self.class_one = class_one()
button = Button(self, text="button", command=self.class_one.method_one)
...
答案 1 :(得分:0)
除非你按下每个按钮实际上需要一个新的类实例(在你的例子中似乎不是这种情况),你应该在 init 中实例化它。