我正在尝试使用GUI和Python来创建一个带有整数输入的简单函数。你知道吗
我对GUI的概念非常陌生,所以我不知道还能做什么。你知道吗
from tkinter import *
root = Tk()
peopleText = Label(root, text="How many people are in your household? ")
wageText = Label(root, text="How much do you make a month before deductions? ")
peopleInput = Entry(root)
wageInput = Entry(root)
runButton = Button(root, text="Run")
peopleText.grid(row=1, column=0)
wageText.grid(row=2, column=0)
peopleInput.grid(row=1, column=1)
wageInput.grid(row=2, column=1)
def runProcess():
incomeSNAP = int(peopleInput.get()) - 1
incomeHousing = int(peopleInput.get()) - 1
if int(wageInput.get()) <= incomeSNAP:
print("Yes")
else:
print("No")
if int(wageInput.get()) <= incomeHousing:
print("Yes")
else:
print("No")
runButton = Button(root, text="Run")
runButton.bind('<Button_1>', runProcess())
runButton.pack()
root.mainloop()
我以为会弹出一个窗口,但出现了以下错误:
File "/Users/noahpark/PycharmProjects/pythonProject/gui.py", line 21, in runProcess
incomeSNAP = int(peopleInput.get()) - 1
ValueError: invalid literal for int() with base 10: ''
问题是如果您注释掉代码中的
'<Button_1>' and .pack()
,然后将command=runProcess
添加到应该运行的按钮中。你知道吗更重要的是,如果用户输入了一个错误的变量,比如“string”,您可以捕获值错误
希望有帮助
相关问题 更多 >
编程相关推荐