【发布时间】:2023-03-06 13:37:02
【问题描述】:
所以我正在尝试构建一个简单的计算器,它在标签中显示数字 pi,即 3.14,并且每次单击“点击我”按钮时,它都会添加另一个十进制值3.14。例如,一旦点击Label会显示3.141,第二次:3.1415等等。
代码如下:
# Import the Tkinter functions
from Tkinter import *
# Create a window
loan_window = Tk()
loan_window.geometry('{}x{}'.format(500, 100))
# Give the window a title
loan_window.title('Screeen!')
## create the counter
pi_num = 3.14
# NUMBER frame
frame = Frame(loan_window, width=100, height=50)
frame.pack()
def addPlaceValue():
pi_num= 3.14
return pi_num
## create a label and add it onto the frame
display_nums = Label(frame, text = 'pi_num')
display_nums.pack()
#### create a label and add it onto the frame
##display_nums = Label(frame, text = pi_num)
##display_nums.pack()
##
# Create a button which starts the calculation when pressed
b1 = Button(loan_window, text = 'Click me', command= addPlaceValue, takefocus = False)
b1.pack()
# Bind it
loan_window.bind('<Return>', addPlaceValue())
# event loop
loan_window.mainloop()
我曾多次尝试跟踪按钮点击,但均未成功。我看到一个问题;代码不知道第 n 次单击按钮。有什么想法吗?
【问题讨论】:
标签: python user-interface button tkinter tkinter-canvas