Jump to content

Python Tkinter Program ValueError HELP!!

Go to solution Solved by gojira2001,

I don't know what happened but the program seems to be working perfectly now. thankyou for helping.

There is Entry variable in the Tkinter program named "income". We have to convert the str into int for using it in calculations. When gincome=int(income.get()) is used the error is "ValueError: invalid literal for int() with base 10"

Please explain the error or the problem in logic. 

Cheers

 

IMG_20210303_135538.jpg

IMG_20210303_135918.jpg

hV2sq.py

Link to comment
https://linustechtips.com/topic/1311597-python-tkinter-program-valueerror-help/
Share on other sites

Link to post
Share on other sites

1) In the future it will make it easier to help you if you copy and paste the code into a code tag (click the <> button, paste in your code, change "No syntax highlighting" to "Python" then click "Insert into post") rather than posting blurry photos of your laptop

 

2) What are you typing into the income box? If it has a decimal point or is blank, it can't be converted to an integer. Try replacing "int" with "float" instead, and check that the box is not empty before processing it.

 

 

____________________________________________________________________________________________________________________________________

 

 

____________________________________________________________________________________________________________________________________

pythonmegapixel

into tech, public transport and architecture // amateur programmer // youtuber // beginner photographer

Thanks for reading all this by the way!

By the way, my desktop is a docked laptop. Get over it, No seriously, I have an exterrnal monitor, keyboard, mouse, headset, ethernet and cooling fans all connected. Using it feels no different to a desktop, it works for several hours if the power goes out, and disconnecting just a few cables gives me something I can take on the go. There's enough power for all games I play and it even copes with basic (and some not-so-basic) video editing. Give it a go - you might just love it.

Link to post
Share on other sites

image.png.cceb3cbec6a9b4c1adc950dad69f6b65.png

 

Please do the bare minimum and paste your code in a code block instead of taking a picture of your screen. Post the full error too.

 

This is probably happening because you can't convert an empty string to an integer. To avoid this either check for minimum length before the conversion or use a try: except: block.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

use float instead of int, then use round()

round(number, ndigits)

Ryzen 5700g @ 4.4ghz all cores | Asrock B550M Steel Legend | 3060 | 2x 16gb Micron E 2666 @ 4200mhz cl16 | 500gb WD SN750 | 12 TB HDD | Deepcool Gammax 400 w/ 2 delta 4000rpm push pull | Antec Neo Eco Zen 500w

Link to post
Share on other sites

2 minutes ago, pythonmegapixel said:

1) In the future it will make it easier to help you if you copy and paste the code into a code tag (click the <> button, paste in your code, change "No syntax highlighting" to "Python" then click "Insert into post") rather than posting blurry photos of your laptop

 

2) What are you typing into the income box? If it has a decimal point, it can't be converted to an integer. Try replacing "int" with "float" instead.

 

 

Sorry it was my first time on the forum. will keep this in mind next time. As for the input i typed 1400000

Link to post
Share on other sites

1 minute ago, gojira2001 said:

Sorry it was my first time on the forum. will keep this in mind next time.

No problem.

1 minute ago, gojira2001 said:

As for the input i typed 1400000

That should work; I'm not sure why it wouldn't.

 

If you copy and paste your code into a post now, I'll test it myself and see if I can work out what's going on.

____________________________________________________________________________________________________________________________________

 

 

____________________________________________________________________________________________________________________________________

pythonmegapixel

into tech, public transport and architecture // amateur programmer // youtuber // beginner photographer

Thanks for reading all this by the way!

By the way, my desktop is a docked laptop. Get over it, No seriously, I have an exterrnal monitor, keyboard, mouse, headset, ethernet and cooling fans all connected. Using it feels no different to a desktop, it works for several hours if the power goes out, and disconnecting just a few cables gives me something I can take on the go. There's enough power for all games I play and it even copes with basic (and some not-so-basic) video editing. Give it a go - you might just love it.

Link to post
Share on other sites

4 minutes ago, Sauron said:

image.png.cceb3cbec6a9b4c1adc950dad69f6b65.png

 

Please do the bare minimum and paste your code in a code block instead of taking a picture of your screen. Post the full error too.

 

This is probably happening because you can't convert an empty string to an integer. To avoid this either check for minimum length before the conversion or use a try: except: block.

<

from tkinter import *
from tkinter import messagebox
#import numpy as np
#import matplotlib.pyplot as plt

root = Tk()
root.title("Household Expenses Manager")


fincome=0


#def graph():
    #IMPORT DATA FROM SQL TABLE
    #plt.


def clearAll() :

    name.delete(0, END)
    age.delete(0, END)
    income.delete(0, END)


def checkError() :

    if (name.get() == "" or age.get() == ""
        or income.get() == ""):

        messagebox.showerror("Input Error", "Enter Valid Details")

        clearAll()        


def taxcal():

    global fincome
    gincome=int(income.get())
    if gincome<250000:
        fincome=gincome
    elif gincome>250000 and gincome<500000:
        fincome=gincome*0.95
    elif gincome>500000 and gincome<750000:
        fincome=gincome*0.90
    elif gincome>750000 and gincome<1000000:
        fincome=gincome*0.85
    elif gincome>1000000 and gincome<1250000:
        fincome=gincome*0.80
    elif gincome>1250000 and gincome<1500000:
        fincome=gincome*0.75
    else:
        fincome=gincome*0.70
    mincome=fincome/12
    p5 = Label(f3, text=fincome*0.50)
    p8 = Label(f3, text=fincome*0.30)
    p11 = Label(f3, text=fincome*0.20)
    p5.grid(row=3, column=1)
    p8.grid(row=3, column=2)
    p11.grid(row=3, column=3)

def clk():
    
    checkError()
    taxcal()
    clearAll()
    


def clk2():
    return

f1 = LabelFrame(root, text="Quote-", padx=20, pady=20)
f2 = LabelFrame(root, text="Personal Details", padx=20, pady=20)
f3 = LabelFrame(root, text="Financial Planner", padx=20, pady=20)   #CHANGE THE NAME OF THE FRAME
f4 = LabelFrame(root, text="Simplified Summary", padx=20, pady=20)  #CHANGE THE NAME OF THE FRAME

f1.pack(anchor="w")
f2.pack(anchor="w")
f3.pack(anchor="w")
f4.pack(anchor="w")

quote = Label(f1, text="When You change Yourself, You change the World")
quote.pack()


l1 = Label(f2, text="Full Name:")
l2 = Label(f2, text="Age(Yrs):")
l3 = Label(f2, text="Annual Income(₹):")
l4 = Label(f2, text="Monthly Income After Tax reduction:")
l5 = Label(f2, text=mincome)

name = Entry(f2, width=30)
age = Entry(f2, width=10)
income = Entry(f2, width=20)

b1 = Button(f2, text="Continue", command=clk)
bq = Button(root, text="Quit Program", command=root.quit)


l1.grid(row=1, column=0)
l2.grid(row=1, column=3)
l3.grid(row=1, column=6)
l4.grid(row=2, column=6)
l5.grid(row=2, column=7)

name.grid(row=1, column=1)
age.grid(row=1, column=4)
income.grid(row=1, column=7)

b1.grid(row=1, column=9)
bq.pack()

#go = Button(f3, text="fetch Data", command=clk2)
#go.grid(row=0, column=1)

dropoptions = [
    "January", 
    "Febuary", 
    "March", 
    "April", 
    "May", 
    "June", 
    "July", 
    "August", 
    "September", 
    "October", 
    "November", 
    "December"
]

month = StringVar()
month.set("Enter Month")

drop = OptionMenu(f3, month, *dropoptions)
p1 = Label(f3, text="Ideal Expenses")
p2 = Label(f3, text="Real Expenses")
p3 = Label(f3, text="deviation")

p4 = Label(f3, text="Needs:")
p5 = Label(f3, text=fincome*0.50)
need = Entry(f3)
p6 = Label(f3, text="Ans:")

p7 = Label(f3, text="Wants:")
p8 = Label(f3, text=fincome*0.30)
want = Entry(f3)
p9 = Label(f3, text="Ans")

p10 = Label(f3, text="Savings:")
p11 = Label(f3, text=fincome*0.20)
saving = Entry(f3)
p12 = Label(f3, text="Ans")


drop.grid(row=0, column=0)
p1.grid(row=3, column=0)
p2.grid(row=4, column=0)
p3.grid(row=5, column=0)


p4.grid(row=2, column=1)
p5.grid(row=3, column=1)
need.grid(row=4, column=1)
p6.grid(row=5, column=1)

p7.grid(row=2, column=2)
p8.grid(row=3, column=2)
want.grid(row=4, column=2)
p9.grid(row=5, column=1)

p10.grid(row=2, column=3)
p11.grid(row=3, column=3)
saving.grid(row=4, column=3)
p12.grid(row=5, column=1)


root.mainloop()

 

>

<Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\kamal kant rastogi\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\cs\hV2sq.py", line 71, in clk
    taxcal()
  File "C:\cs\hV2sq.py", line 38, in taxcal
    gincome=int(income.get())
ValueError: invalid literal for int() with base 10: ''
>

Link to post
Share on other sites

1 minute ago, pythonmegapixel said:

No problem.

That should work; I'm not sure why it wouldn't.

 

If you copy and paste your code into a post now, I'll test it myself and see if I can work out what's going on.

 

1 minute ago, gojira2001 said:

<

from tkinter import *
from tkinter import messagebox
#import numpy as np
#import matplotlib.pyplot as plt

root = Tk()
root.title("Household Expenses Manager")


fincome=0


#def graph():
    #IMPORT DATA FROM SQL TABLE
    #plt.


def clearAll() :

    name.delete(0, END)
    age.delete(0, END)
    income.delete(0, END)


def checkError() :

    if (name.get() == "" or age.get() == ""
        or income.get() == ""):

        messagebox.showerror("Input Error", "Enter Valid Details")

        clearAll()        


def taxcal():

    global fincome
    gincome=int(income.get())
    if gincome<250000:
        fincome=gincome
    elif gincome>250000 and gincome<500000:
        fincome=gincome*0.95
    elif gincome>500000 and gincome<750000:
        fincome=gincome*0.90
    elif gincome>750000 and gincome<1000000:
        fincome=gincome*0.85
    elif gincome>1000000 and gincome<1250000:
        fincome=gincome*0.80
    elif gincome>1250000 and gincome<1500000:
        fincome=gincome*0.75
    else:
        fincome=gincome*0.70
    mincome=fincome/12
    p5 = Label(f3, text=fincome*0.50)
    p8 = Label(f3, text=fincome*0.30)
    p11 = Label(f3, text=fincome*0.20)
    p5.grid(row=3, column=1)
    p8.grid(row=3, column=2)
    p11.grid(row=3, column=3)

def clk():
    
    checkError()
    taxcal()
    clearAll()
    


def clk2():
    return

f1 = LabelFrame(root, text="Quote-", padx=20, pady=20)
f2 = LabelFrame(root, text="Personal Details", padx=20, pady=20)
f3 = LabelFrame(root, text="Financial Planner", padx=20, pady=20)   #CHANGE THE NAME OF THE FRAME
f4 = LabelFrame(root, text="Simplified Summary", padx=20, pady=20)  #CHANGE THE NAME OF THE FRAME

f1.pack(anchor="w")
f2.pack(anchor="w")
f3.pack(anchor="w")
f4.pack(anchor="w")

quote = Label(f1, text="When You change Yourself, You change the World")
quote.pack()


l1 = Label(f2, text="Full Name:")
l2 = Label(f2, text="Age(Yrs):")
l3 = Label(f2, text="Annual Income(₹):")
l4 = Label(f2, text="Monthly Income After Tax reduction:")
l5 = Label(f2, text=mincome)

name = Entry(f2, width=30)
age = Entry(f2, width=10)
income = Entry(f2, width=20)

b1 = Button(f2, text="Continue", command=clk)
bq = Button(root, text="Quit Program", command=root.quit)


l1.grid(row=1, column=0)
l2.grid(row=1, column=3)
l3.grid(row=1, column=6)
l4.grid(row=2, column=6)
l5.grid(row=2, column=7)

name.grid(row=1, column=1)
age.grid(row=1, column=4)
income.grid(row=1, column=7)

b1.grid(row=1, column=9)
bq.pack()

#go = Button(f3, text="fetch Data", command=clk2)
#go.grid(row=0, column=1)

dropoptions = [
    "January", 
    "Febuary", 
    "March", 
    "April", 
    "May", 
    "June", 
    "July", 
    "August", 
    "September", 
    "October", 
    "November", 
    "December"
]

month = StringVar()
month.set("Enter Month")

drop = OptionMenu(f3, month, *dropoptions)
p1 = Label(f3, text="Ideal Expenses")
p2 = Label(f3, text="Real Expenses")
p3 = Label(f3, text="deviation")

p4 = Label(f3, text="Needs:")
p5 = Label(f3, text=fincome*0.50)
need = Entry(f3)
p6 = Label(f3, text="Ans:")

p7 = Label(f3, text="Wants:")
p8 = Label(f3, text=fincome*0.30)
want = Entry(f3)
p9 = Label(f3, text="Ans")

p10 = Label(f3, text="Savings:")
p11 = Label(f3, text=fincome*0.20)
saving = Entry(f3)
p12 = Label(f3, text="Ans")


drop.grid(row=0, column=0)
p1.grid(row=3, column=0)
p2.grid(row=4, column=0)
p3.grid(row=5, column=0)


p4.grid(row=2, column=1)
p5.grid(row=3, column=1)
need.grid(row=4, column=1)
p6.grid(row=5, column=1)

p7.grid(row=2, column=2)
p8.grid(row=3, column=2)
want.grid(row=4, column=2)
p9.grid(row=5, column=1)

p10.grid(row=2, column=3)
p11.grid(row=3, column=3)
saving.grid(row=4, column=3)
p12.grid(row=5, column=1)


root.mainloop()

 

>

<Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\kamal kant rastogi\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\cs\hV2sq.py", line 71, in clk
    taxcal()
  File "C:\cs\hV2sq.py", line 38, in taxcal
    gincome=int(income.get())
ValueError: invalid literal for int() with base 10: ''
>

 

Link to post
Share on other sites

3 minutes ago, gojira2001 said:

 

A code block. This:

image.png.aabf11e49a561360bcab3bd17464d265.png

 

And make sure to select "python" in the bottom right for syntax highlighting.

 

And yes, it seems the problem is that the string is empty. Check for length or use try:except: as I mentioned.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×