我在这方面做了很多工作,我所做的一切似乎都不能解决问题。我对这种编程语言的细微差别相对缺乏经验。谢谢你给我小费。你知道吗
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()
这是回溯
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: ''
callback
这里不应该有括号。这使得函数立即执行,而不是等待按钮被按下。你知道吗另外,
if ent1 != 0 and ent2 != 0
总是要计算成True
,因为ent1
和ent2
总是字符串,而且字符串永远不等于零。也许你的意思是if ent1 != '' and ent2 != '':
,或者只是if ent1 and ent2:
此外,应该从Entry对象中删除
text
属性。我不知道他们应该做什么,因为我没有看到它列在文档中,但看起来只要他们都等于一,在一个条目中键入将导致相同的文本出现在另一个条目中。你知道吗相关问题 更多 >
编程相关推荐