【发布时间】:2021-07-16 11:31:40
【问题描述】:
我正在编写一个程序,该程序需要在按下按钮时调用两个值(来自 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, relwidth=0.5, relheight=1)
root.mainloop()
【问题讨论】:
-
将
command=button_function()更改为command=button_function。 -
为什么要编辑代码并添加正确的部分?这没有意义
-
@PCM 正确的部分也没有得到预期的结果:(他已经提到过这个问题!我想知道这怎么可能!