0

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()
3
  • print the value of won.get(). Are you getting a string without spaces? Commented May 30, 2022 at 4:40
  • This thread may be relevant to the issue you are having. Commented May 30, 2022 at 4:41
  • 3
    Calling Tk() multiple times causes Vars to work inconsistently, among various other problems. Use Toplevel() instead to create additional windows. Commented May 30, 2022 at 4:47

1 Answer 1

0

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()
Sign up to request clarification or add additional context in comments.

1 Comment

You will have to learn how to use TopLevel().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.