我是制作GUI的新手。我在IDLE(2.7.3)工作,想要打开一个带按钮的简单窗口。这是我的代码:
from Tkinter import *
import sys
win=Tk()
button1=Button(win, text = "ok", command = sys.exit, foreground = "red")
button1.pack()
mainloop()
如果我不使用mainloop()
,则窗口将无法打开。如果我使用mainloop()
我单击“确定”按钮时出现此错误,并且带按钮的窗口将不会关闭:
Traceback (most recent call last):
File "C:\Python27\GUI.py", line 6, in <module>
mainloop()
File "C:\Python27\lib\lib-tk\Tkinter.py", line 325, in mainloop
_default_root.tk.mainloop(n)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1412, in __call__
raise SystemExit, msg
答案 0 :(得分:0)
我将您的代码粘贴到python文件并在Windows 7上从命令提示符运行它,它运行正常。也许这是你的Tkinter安装的问题?
顺便说一下,“mainloop”行是强制性的。以粗略的方式解释它,这就是将UI绘制到屏幕上的原因。
答案 1 :(得分:0)
如果您在IDLE中运行Tkinter内容,请尝试将command = sys.exit
替换为command = win.destroy
。这样你的小部件就会被破坏,但你不会搞乱IDLE本身,这也取决于Tkinter的运行。
这些讨论也很重要: