I need to do some basic conditional logic on entries, but I am running into an issue.
There are 2 entries, "dosage" and "week". In both will be entered only numbers (25, 3, etc.). I have a "Get Info" button that activates a function what will preform conditional logic on the entries, although since when the program runs there isn't any text inside of the entries, I run into an error.
ValueError: invalid literal for int() with base 10: ''
Here is my code:
from tkinter import *
import tkinter.font as font
# Window setup
root = Tk()
root.title("Dosage GUI")
root.geometry("330x450")
# Entries
weekEntry = Entry(root)
dosageEntry = Entry(root)
# Labels to describe entries
weekLabel = Label(root, text="Week #")
dosageLabel = Label(root, text="Dosage in mg (enter only a number)")
# Information text
# State = disabled, but when changing text, change to normal, then back to disabled
def setInfoText(textBox, text):
textBox.config(state="normal")
textBox.insert(1.0, text)
textBox.config(state="disabled")
infoTextFont = font.Font(size=8)
infoText = Text(root, height=17, width=30, padx=10,pady=10, state="disabled", font=infoTextFont, bd=5,bg="#e8ebea")
# Get Info button
def getInfo(week, dosage):
# Check what is possible based on the dosage rules
if week > 3:
setInfoText(infoText, "PI may change dosage if needed.")
if week < 3:
setInfoText(infoText, "PI cannot change dosage until week 3.")
getInfoFont = font.Font(size=13,weight="bold")
getInfoBut = Button(root, text="Get Info", padx=20,pady=20,bd=10,bg="#7299ed",activebackground="#729ffc", fg="#40e677", command=getInfo(int(weekEntry.get()), int(dosageEntry.get())))
getInfoBut['font'] = getInfoFont
# Grid
weekEntry.grid(row=0,column=5)
dosageEntry.grid(row=0, column=10)
weekLabel.grid(row=5,column=5)
dosageLabel.grid(row=5,column=10)
getInfoBut.place(x=100,y=70)
infoText.place(x=50,y=170)
# mainloop
root.mainloop()
The command
keyword argument needs a function as parameter.
Right now you're passing getInfo(int(weekEntry.get()), int(dosageEntry.get()))
- which is the return value of getInfo - which is None (because there are no return statements in that function).
Instead, you may want to pass this a statement inside a lambda - this way you create a function that gets called whenever that button is pressed:
getInfoBut = Button(root, text="Get Info", padx=20,pady=20,bd=10,bg="#7299ed",activebackground="#729ffc", fg="#40e677", command=lambda: getInfo(int(weekEntry.get()), int(dosageEntry.get())))
The reason the error occurs is because Python attempts to evaluate the call to getInfo
even before the UI shows - the fields weekEntry
anddosageEntry
are empty at this point - attempting to convert an empty string to an int results in the error.