问题描述
我正在编写一个程序,该程序需要在按下按钮时调用两个值(来自 2 个条目)。我简化了代码以尝试隔离问题。出于某种原因,该程序没有按照我想要的方式运行。当按钮被按下时,输出是“A=”,然后如果我第二次点击按钮,我会得到“”A=entry1 B=entry2 A=“。我一直试图找出这个问题几个小时了,请帮忙。
import tkinter as tk
def button_function():
A = entry1.get()
print('A=',A)
B= entry2.get()
print('B=',B)
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
entry1 = tk.Entry(root)
entry1.place(relwidth=0.5,relheight=0.5)
entry2 = tk.Entry(root)
entry2.place(rely=0.5,relwidth=0.5,relheight=0.5)
button = tk.Button(root,text = "confirm",command= button_function)
button.place(relx=0.5,relheight=1)
root.mainloop()
解决方法
你只需要把command=button_function()
改成command=button_function
就可以完美运行!