0

我正在学习一些关于 Python Tkinter 的东西,我写了这个简单的代码:

from Tkinter import * 
import tkMessageBox


def print_it(msg):
    Text1.insert(INSERT, str(msg))

top = Tk()




Entry1 = Entry(width=300)
Entry1.pack(side='top')
Button1 = Button(text='Send',width=300,command=print_it("Message"))
Button1.pack(side='top')

Text1 = Text(height=600,width=300)
Text1.pack(side='top')



top.geometry('640x480+10+10')
top.title('GUI')
top.mainloop()

我不知道为什么,但它给了我错误:

NameError:未定义全局名称“Text1”

如果我将Text1print_it def 中的 替换为Entry1,它不会给我任何错误。

4

1 回答 1

1

您正在调用print_it而不是使用对函数的引用作为command参数的值。

替换以下行:

Button1 = Button(text='Send',width=300,command=print_it("Message"))

Button1 = Button(text='Send',width=300,command=lambda: print_it("Message"))
于 2013-08-16T13:56:14.970 回答