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: ''
Noah Park
  • 15
  • 4
  • Possible duplicate of [Python String to Int Or None](https://stackoverflow.com/questions/18623668/python-string-to-int-or-none) –  Mar 25 '19 at 03: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('', runProcess())` because you need to provide a reference to the function. Passing `runProcesss()` will execute the function immediately. Also it should be ``. – Henry Yik Mar 25 '19 at 03:29
  • The magic keyword you are looking for are `try` and `except`. – Klaus D. Mar 25 '19 at 03:37
  • http://stackoverflow.com/q/5767228/7432 – Bryan Oakley Mar 25 '19 at 14:22

1 Answers1

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

xtrimzz
  • 59
  • 6