I'm unable to print resulting answer to a label
I was getting error add() missing positional argument self
import tkinter
from tkinter import Entry
def add(self):
x1=float(Entry.get(e))
y1=float(Entry.get(e1))
ans=x1+y1
labr.config(text="Result is %d"%ans)
def sub():
x1=float(Entry.get(e))
y1=float(Entry.get(e1))
ans=x1-y1
print("answer is ",ans)
window=tkinter.Tk()
window.title("sample calculator")
window.geometry("300x300")
lab=tkinter.Label(window,text="calculator")
e=tkinter.Entry(window)
e1=tkinter.Entry(window)
b=tkinter.Button(window,bg="green",text="add",command=add)
b1=tkinter.Button(window,bg="blue",text="subtract",command=sub)
labr=tkinter.Label(window)
lab.pack()
e.pack()
e1.pack()
b.pack()
b1.pack()
window.mainloop()
Your function add
takes one argument self
but the button b
calls add()
when you click on it, hence the error about a missing argument.
I think that you copied this function from a class, but you forgot to remove the self
from the arguments because you don't use it inside add
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.