【发布时间】:2022-01-17 10:45:39
【问题描述】:
我正在尝试在 tkinter 的帮助下制作一个计算器。我定义了一个函数 click,它返回用户点击的值,但它给出了一个我不理解的错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
TypeError: click() missing 1 required positional argument: 'event'
我的代码如下:
from tkinter import *
def click(event):
text = event.widget.cget("text")
icon = r"C:\Users\Sumit\vs code python\projects\tkinter_course\calculator.ico"
root = Tk()
root.configure(bg = "black")
root.geometry("644x500")
root.title("Calculator by Arnav")
root.wm_iconbitmap(icon)
scvalue = StringVar()
scvalue.set("")
screen = Entry(root, textvar=scvalue, font="lucida 37 bold")
screen.pack(fill=X, ipadx=8, pady=10, padx=12)
f1 = Frame(root, bg = 'white' ).pack(side = LEFT)
b1 = Button(f1 , text = "1", font = 'lucida 35 bold',command=click).pack(anchor = 'nw',side =
LEFT, padx = 23)
b2 = Button(f1 , text = "2", font = 'lucida 35 bold',command=click).pack(anchor = 'nw',side =
LEFT, padx = 23)
b3 = Button(f1 , text = "3", font = 'lucida 35 bold',command=click).pack(anchor = 'nw',side =
LEFT, padx = 23)
root.mainloop()
这说明我没有定义事件,但我有。 请帮忙。
【问题讨论】:
-
event仅在您bind小部件时传递。它由 tkinter 隐式传递。 -
那我该怎么做呢?\
-
为什么需要
event? -
获取按钮点击时的文本
-
谢谢我知道了