我在我的程序中创建了2个窗口,我使用了两个类,因为代码很复杂,所以我把它分成两个不同的python文件。导入第二个窗口文件后,如何确保它打开而不会出现此图片中显示的错误
主窗口编码:
from tkinter import *
import classGUIProgram
class Window(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400+30+30")
self.wButton = Button(self, text='newWindow', command = self.OnButtonClick)
self.wButton.pack()
def OnButtonClick(classGUIProgram):
classGUIProgram.top = Toplevel()
master = Tk()
b = classGUIProgram.HappyButton(master)
master.mainloop()
if __name__ == "__main__":
window = Window(None)
window.title("title")
window.mainloop()
第二个窗口的编码:
^{pr2}$
您正在创建额外的
Tk
窗口。下面是一个使用Toplevel
小部件和另一个文件的示例。在主窗口.py
第二窗口.py
^{pr2}$使用另一个文件时,请确保没有您不希望运行的任何全局代码。您的类不必继承},这只是一个例子。但是您需要确保您只有一个
Tk
和{Tk
的实例,否则您将得到您遇到的行为相关问题 更多 >
编程相关推荐