如何才能从Python GUI中的用户输入中不获得以10为基数的错误?
我正在尝试使用GUI和Python来创建一个带有整数输入的简单函数 我对GUI概念非常陌生,所以我不知道还能做什么如何才能从Python GUI中的用户输入中不获得以10为基数的错误?,python,user-interface,Python,User Interface,我正在尝试使用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? ") pe
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()
如果您注释掉代码中的
''和.pack()
,然后将command=runProcess
添加到应该运行的按钮,则会发现问题
从tkinter导入*
root=Tk()
peopleText=Label(root,text=“你家有多少人?”)
wageText=Label(root,text=“扣除前一个月你挣多少钱?”)
peopleInput=条目(根)
输入=输入(根)
runButton=按钮(root,text=“Run”)
peopleText.grid(行=1,列=0)
wageText.grid(行=2,列=0)
peopleInput.grid(行=1,列=1)
wageInput.grid(行=2,列=1)
def runProcess():
incomeSNAP=int(peopleInput.get())-1
incomeHousing=int(peopleInput.get())-1
如果int(wageInput.get())可能重复,则仍有许多问题需要处理,但对于特定的错误,您需要删除runButton.bind(“”,runProcess())
上的()
,因为您需要提供函数的引用。传递runProcesss()
将立即执行该函数。此外,它应该是
。您要查找的神奇关键字是try
和除了。
File "/Users/noahpark/PycharmProjects/pythonProject/gui.py", line 21, in runProcess
incomeSNAP = int(peopleInput.get()) - 1
ValueError: invalid literal for int() with base 10: ''