【发布时间】:2021-03-25 09:51:59
【问题描述】:
我将使用我的导师提供给我的这个起始文件来编写一个程序。问题是她给我们的文件没有运行。我不知道这是因为我使用的是 IDLE,还是代码只在某些操作系统上运行。
例如,我使用 Windows,但我的老师同时使用 Windows 和 Linux 系统。我无法让她弄清楚这个问题,所以我希望你们能。
目前,当我运行启动文件时,出现错误:
TypeError: main() is not defined.
当我在程序底部将main() 切换为GUI() 时,出现新错误:
TypeError: __init__() missing 1 required positional argument: 'rootWindow'
这是完整的代码:
from tkinter import*
from tkinter import tk
class GUI:
def __init__(self,rootWindow):
self.label = ttk.Label(rootWindow, text="Hellow World!")
self.label.grid(row=0,column=0)
self.button1=ttk.Button(rootWindow,text="Hello",command=self.hello)
self.button1.grid(row=0,column=1)
self.button2=ttk.Button(rootWindow,text="Bye",command=self.bye)
self.button2.grid(row=0,column=2)
def bye(self):
self.label.config(text="GoodbyeWorld!")
def hello(self):
self.label.config(text="HelloWorld!")
def main():
global label
rootWindow = Tk()
gui = GUI(rootWindow)
rootWindow.mainloop()
main()
【问题讨论】:
-
GUI()需要rootWindow参数。 -
main未定义。GUI.main是。
标签: python python-3.x class tkinter main