Home > Net >  could not convert string to float: '' error in python(Tkinter)
could not convert string to float: '' error in python(Tkinter)

Time:05-30

I made a program that converts 1 currency to another(in this case only won to dollars). However, when I try to click on US radio button, it says "could not convert string to float: '' ". So, what's the problem here? I tried to run it without an additional window, and it worked perfectly fine, but when I open a converter window in a new window, it does not work. What is the problem and how do I make it so the converter would work fine when you open it in a new window?

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror
from tkinter import *
root = tk.Tk()
root.geometry('5000x5000')
def openconverter():    
    root = tk.Tk()
    root.title('Change Converter')
    root.geometry('400x210')

    def won_to_dollar(f):
        US = 0.00079
        return f*US
    frame = ttk.Frame(root)

    options = {'padx': 5, 'pady': 5}

    won = tk.StringVar()
    won_entry = ttk.Entry(frame, textvariable=won)
    won_entry.grid(column=1, row=0, **options)
    def convert_button_clicked(won_to_dollar):
        try:
            f = float(won.get())
            c = won_to_dollar(f)
            result = f'{f} won = {c:.2f}'
            result_label.config(text=result)
        except ValueError as error:
            showerror(title='Error', message=error)
    result_label = ttk.Label(frame)
    result_label.grid(row=1, columnspan=3, **options)
    frame.grid(padx=10, pady=10)
    r = IntVar()
    Radiobutton(root, text="US", variable = r, value = 1, command = lambda : convert_button_clicked(won_to_dollar)).place(x = 220, y = 20)
    myLabel = Label(root, text = r.get())
    myLabel.grid
    root.mainloop()
    
Converted = Button(root, text="converter",font = ("Helvetica", 15), width=50, height=50, compound="c", activeforeground = "green", command = lambda: openconverter())
Converted.place(x=10, y=185) 
root.mainloop()

CodePudding user response:

I think that you have over complicated your code... This could be done so simply

from tkinter import *

def calc():
    US = 0.00079
    amount = float(entryVar.get())
    ansLabel.config(text=f"Your answer = {US*amount}")

root = Tk()
root.geometry("500x500")

entryVar = StringVar()
entryVar.set('')

entry = Entry(root, textvariable=entryVar)
entry.pack()

Button(root, command=calc, text="Calculate").pack()

ansLabel = Label(root, text = "Your answer = ")
ansLabel.pack()

root.mainloop()

CodePudding user response:

Try this and click convert and enter value and select US.

from tkinter import ttk
from tkinter.messagebox import showerror
from tkinter import *
root = Tk()
    
root.title('Change Converter')
root.geometry('400x810')


def openconverter():     
    def won_to_dollar(f):
        US = 0.00079
        return f*US
    frame = Frame(root)

    options = {'padx': 5, 'pady': 5}

    won = StringVar()
    won_entry = Entry(frame, textvariable=won)
    won_entry.grid(column=1, row=0, **options)
    def convert_button_clicked(won_to_dollar):
        try:
            f = won.get()
            c = won_to_dollar(float(f))
            result = f'{f} won = {c}'
            result_label.config(text=result)
        except ValueError as error:
            showerror(title='Error', message=error)
            
    result_label = Label(frame)
    result_label.grid(row=1, columnspan=3, **options)
    frame.grid(padx=10, pady=10)
    r = IntVar()
    Radiobutton(root, text="US", variable = r,
                value = 1,
                command = lambda : convert_button_clicked(won_to_dollar)).place(x = 220, y = 20)
    myLabel = Label(root, text = r.get())
    myLabel.grid
     
    
Converted = Button(root, text="converter",
                   font = ("Helvetica", 15), width=50, height=50, compound="c",
                   activeforeground = "green", command = lambda: openconverter())

Converted.place(x=10, y=5) 
root.mainloop()
  • Related