Reputation:
I am trying to make a calculator with the help of tkinter. I have defined a function click, which returns the value the user clicked but it is giving a error which I am not understanding.
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'
My code is as follows:
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()
It is telling that I have not defined event, but I have. Pls help.
Upvotes: 2
Views: 431
Reputation: 228
You need to bind your function to the widget you want to detect with this line of code...
<Button-Variable-Name-Here>.bind("<Button-1>", click)
This will then check if the widget is ever clicked with the left mouse button.
Upvotes: 1