【问题标题】:I cant use event in fuctions in tkinter我不能在 tkinter 的函数中使用事件
【发布时间】: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
  • 获取按钮点击时的文本
  • 谢谢我知道了

标签: python tkinter


【解决方案1】:

您需要使用这行代码将您的函数绑定到要检测的小部件...

<Button-Variable-Name-Here>.bind("<Button-1>", click)

这将检查是否曾经用鼠标左键单击过小部件。

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多