1

I am trying to use GUI with Python to create a simple function with an integer input.

I'm really new to the GUI concept so I don't know what else to do.

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()

I expected a window to pop up but I got this error:

  File "/Users/noahpark/PycharmProjects/pythonProject/gui.py", line 21, in runProcess
    incomeSNAP = int(peopleInput.get()) - 1
ValueError: invalid literal for int() with base 10: ''
4
  • Possible duplicate of Python String to Int Or None Commented Mar 25, 2019 at 3:28
  • There are a number of problems you still need to deal with, but for your specific error, you need to remove () on runButton.bind('<Button_1>', runProcess()) because you need to provide a reference to the function. Passing runProcesss() will execute the function immediately. Also it should be <Button-1>. Commented Mar 25, 2019 at 3:29
  • The magic keyword you are looking for are try and except. Commented Mar 25, 2019 at 3:37
  • stackoverflow.com/q/5767228/7432 Commented Mar 25, 2019 at 14:22

1 Answer 1

1

the problem is found is if you comment out the '<Button_1>' and .pack() your code and then add command=runProcess to the button it should run.

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()

Just to add to that, you can catch the value error when in case the user inputed a wrong variable say '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()

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.