Python Tkinter:将条目小部件链接到按钮和函数
我是Tkinter的新手,不知道如何继续。我试图将我定义的函数链接到一个由按钮激活的条目小部件。但我不知道如何让这三个人互相沟通。我想它打印以及返回到脚本,以便我可以在另一个功能中使用。这就是我到目前为止所做的:Python Tkinter:将条目小部件链接到按钮和函数,python,tkinter,Python,Tkinter,我是Tkinter的新手,不知道如何继续。我试图将我定义的函数链接到一个由按钮激活的条目小部件。但我不知道如何让这三个人互相沟通。我想它打印以及返回到脚本,以便我可以在另一个功能中使用。这就是我到目前为止所做的: import Tkinter as tk def TestMath(x): calculate = x + 4 print calculate return calculate root = tk.Tk() entry = tk.Entry(root)
import Tkinter as tk
def TestMath(x):
calculate = x + 4
print calculate
return calculate
root = tk.Tk()
entry = tk.Entry(root)
value = entry.get()
number = int(value)
button = tk.Button(root, text="Calculate")
calculation = TestMath(number)
root.mainloop()
按钮
调用分配给命令的函数=
(必须是“函数名”,不带()
和参数-或lambda函数)
TestMath
将计算分配给全局变量result
,其他函数可以访问该值
import Tkinter as tk
def TestMath():
global result # to return calculation
result = int(entry.get())
result += 4
print result
result = 0
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
button = tk.Button(root, text="Calculate", command=TestMath)
button.pack()
root.mainloop()
button调用的函数不必返回值,因为没有对象可以接收该值。
button(…,command=TestMath)
这个问题可以在Google上甚至在本网站上轻松研究。在发布简单问题之前,请环顾四周并进行研究。
import Tkinter as tk
def TestMath():
global result # to return calculation
result = int(entry.get())
result += 4
print result
result = 0
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
button = tk.Button(root, text="Calculate", command=TestMath)
button.pack()
root.mainloop()