Reputation: 19
I have worked on this a good bit and everything I try does not seem to fix the problem. I am relatively inexperienced with the nuances of this programing language. I appreciate any tips.
from tkinter import *
root = Tk()
lbltitle = Label(root, text="Adding Program")
lbltitle.grid(row=0, column=3)
lbllabelinput = Label(root, text="Input first number")
lbllabelinput.grid(row=1, column=0)
entnum1 = Entry(root, text=1)
entnum1.grid(row=1, column=1)
lbllabelinput2 = Label(root, text="Input Second number")
lbllabelinput2.grid(row=1, column=2)
entnum2 = Entry(root, text=1)
entnum2.grid(row=1, column=3)
def callback():
ent1 = entnum1.get()
ent2 = entnum2.get()
if ent1 != 0 and ent2 != 0:
result = int(ent1) + int(ent2)
lblresult = Label(root, text=str(result))
lblresult.grid(row=3)
btnadd = Button(root, text="add", command=callback())
btnadd.grid(row=2)
root = mainloop()
here is the traceback
Traceback (most recent call last):
File "/Users/matt9878/Google Drive/AddingProgram/AddingProgram.py", line 31, in <module>
btnadd = Button(root, text="add", command=callback())
File "/Users/matt9878/Google Drive/AddingProgram/AddingProgram.py", line 27, in callback
result = int(ent1) + int(ent2)
ValueError: invalid literal for int() with base 10: ''
Upvotes: 1
Views: 87
Reputation: 76254
btnadd = Button(root, text="add", command=callback())
callback
should not have parentheses here. That makes the function execute immediately instead of waiting for the button to be pressed.
btnadd = Button(root, text="add", command=callback)
Additionally, if ent1 != 0 and ent2 != 0
is always going to evaluate to True
because ent1
and ent2
are always strings, and a string is never equal to zero. Perhaps you meant if ent1 != '' and ent2 != '':
, or just if ent1 and ent2:
Additionally, you should delete the text
attributes from your Entry objects. I don't know what they're supposed to do since I don't see it listed in the documentation, but it looks like as long as they're both equal to one, typing in one entry will cause the same text to appear in the other entry.
Upvotes: 3