【发布时间】:2016-04-07 08:07:36
【问题描述】:
在 Python 中使用 Tkinter 我试图更改按下按钮时文本框中显示的内容。到目前为止我的代码是:
screen = Tk()
text = Text(screen, height = 2, width = 30)
text.pack()
text.insert(END, '-')
def apress():
text.insert(END, 'a')
a = Tkinter.Button (screen, text = 'a', width = 5, command = apress).pack()
mainloop()
当代码运行时,没有任何反应,即使您单击“中止调试”,调试器也不会停止运行。有没有办法来解决这个问题?
【问题讨论】:
-
如果我错了,请纠正我,但
Text字段不应该有一个StringVar。 -
注意缩进。
-
我添加了
from Tkinter import *并删除了a = Tkinter.,您的代码对我有用。 Linux Mint 17,Python 2.7.11。 -
顺便说一句:
a = Button(...).pack()将None分配给a,因为pack()返回None。如果不需要a,请使用a = Button(...) ; a.pack()或Button(...).pack() -
@MalikBrahimi:不,你不需要在任何地方使用
StringVar。
标签: python python-2.7 tkinter