我如何无法从Python GUI中的用户输入中得到以10为底的错误?

时间:2019-03-25 03:24:57

标签: 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? ")

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: ''

1 个答案:

答案 0 :(得分:0)

发现的问题是,如果您注释掉'<Button_1>' and .pack()您的代码,然后将command=runProcess添加到应该运行的按钮上。

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", command=runProcess)
runButton.grid(row=3, column=0)
#runButton.bind('<Button_1>', runProcess())
#runButton.pack()

root.mainloop()

此外,如果用户输入了错误的变量“'string'

,您可能会发现值错误

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():
    try:
        peopleValue = int(peopleInput.get())
        wageValue = int(peopleInput.get())
    except ValueError:
        peopleValue = None
        wageValue = None
        print("Pls input integer")
        return

    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", command=runProcess)
runButton.grid(row=3, column=0)


root.mainloop()

希望有帮助