Home > Software design >  Unable to convert String value to float value
Unable to convert String value to float value

Time:09-22

In Python why the string value is not converted into float value. I am in this error for about 15 days and I am not able to find the solution to the problem. The code is below. Thanks in advance.

# Importing the libraries
from tkinter import *
from tkinter import ttk

# *Program Constants*
WELCOME_FONT = "lucida 20 bold"
ENTRY_FONT = "lucida 13"
GRAM_FONT = "lucida 10"
POUND_FONT = "lucida 10"
OUNCE_FONT = "lucida 10"

class WeightConverter:
    def __init__(self):
        self.window = Tk()
        self.window.geometry("600x300")
        self.window.title("Weight Converter GUI")
        self.window.wm_iconbitmap("Asset/icon.ico")
        self.welcome()
        self.user_value = StringVar()
        self.entrybox()
        self.convertbutton()
        self.entry()

    def welcome(self):
        welcome_label = Label(self.window, text="Welcome to the Weight 
        Converter",font=WELCOME_FONT, padx=85)
        welcome_label.grid(row=0, columnspan=3)

    def entrybox(self):
        entry_label = Label(self.window, text="Enter the weight in kg", font=ENTRY_FONT)
        entry_label.grid(row=1, columnspan=1, pady=20)
        entry_box = ttk.Entry(self.window, textvariable=self.user_value)
        entry_box.grid(row=1, columnspan=4)

    def convertbutton(self):
        convert_button = ttk.Button(self.window, text="Convert", command=self.entryAndConversion)
        convert_button.grid(row=2, columnspan=3, pady=10)

    def entryAndConversion(self):
        gram_text = ttk.Label(self.window, text="Gram", font=GRAM_FONT)
        gram_text.grid(row=3, column=0, pady=5)

        pound_text = ttk.Label(self.window, text="Pound", font=POUND_FONT)
        pound_text.grid(row=4, column=0, pady=5)

        ounce_text = ttk.Label(self.window, text="Ounce", font=OUNCE_FONT)
        ounce_text.grid(row=5, column=0, pady=5)

        gram_entry_box = ttk.Entry(self.window)
        gram_entry_box.grid(row=3, columnspan=4)

        pound_entry_box = ttk.Entry(self.window)
        pound_entry_box.grid(row=4, columnspan=4)

        ounce_entry_box = ttk.Entry(self.window)
        ounce_entry_box.grid(row=5, columnspan=4)

        gram = float(self.user_value.get()) * 1000
        pound = float(self.user_value.get()) * 2.20462
        ounce = float(self.user_value.get()) * 35.274

        gram_entry_box.delete("1.0", END)
        gram_entry_box.insert(END, gram)

        pound_entry_box.delete("1.0", END)
        pound_entry_box.insert(END, pound)

        ounce_entry_box.delete("1.0", END)
        ounce_entry_box.insert(END, ounce)

    def run(self):
        self.window.mainloop()

if __name__ == '__main__':
    converter = WeightConverter()
    converter.run()

It is giving this error

Traceback (most recent call last):
    File "e:\Python\Weight converter GUI\main.py", line 77, in <module>
        converter = WeightConverter()
    File "e:\Python\Weight converter GUI\main.py", line 24, in __init__
        self.entry()
    File "e:\Python\Weight converter GUI\main.py", line 59, in entry
        gram = float(self.user_value.get()) * 1000
ValueError: could not convert string to float: ''


    

CodePudding user response:

As the people are saying here, this has probably to do with the fact that if the string is empty, it will throw this error.

as such:

gram = float(self.user_value.get()) * 1000

should become

gram= float(0) if len(self.user_value.get()) < 1 else float(self.user_value.get()) * 1000

CodePudding user response:

Refer for more explanation.

https://stackoverflow.com/questions/33974856/error-while-inserting-a-value-in-python-tkinter-entry-box

Change your code as follows.

        gram_entry_box.delete(0, END)
        gram_entry_box.insert(END, gram)

        pound_entry_box.delete(0, END)
        pound_entry_box.insert(END, pound)

        ounce_entry_box.delete(0, END)
        ounce_entry_box.insert(END, ounce)
  • Related