我正在嘗試在 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()
這說明我沒有定義事件,但我有。請幫忙。
uj5u.com熱心網友回復:
您需要使用這行代碼將函式系結到要檢測的小部件...
<Button-Variable-Name-Here>.bind("<Button-1>", click)
然后,這將檢查是否曾經用滑鼠左鍵單擊過小部件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/416046.html
標籤:
