Search code examples
pythonfunctiontkinterreturn

How to return multiple value from a function to another by tkinter entry box? (ValueError: could not convert string to float: )


I am really new to tkinter and python, and I try to create GUI by using tkinter. The sample is just calculate the multiplication and addition of two number and return, and i am stuck with generating result when I can not convert string to input ( which means there is no input). So, how can I return that 2 values to another function?

import tkinter as tk

'''OS'''
root = tk.Tk()
root.title("Simple Box")
operator=""

canvas = tk.Canvas(root, height=400,width=500,bg="White")
canvas.pack()

frame=tk.Frame(root, bg="Light Blue")
frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)
'''Variable '''
num1= tk.StringVar()
num2= tk.StringVar()

''' function'''
def operation1():
    global number1, number2
    multi_add =tk.Tk()
    multi_add.title("Multiplication and Addition")

    tk.Label(multi_add, font =("Helvetica", 20),
             text="Enter the first number:").grid(row=0)
    tk.Label(multi_add, font =("Helvetica", 20),
             text="Enter the second number").grid(row=1)

    number1=tk.Entry(multi_add, textvariable = num1).grid(row=0, column=1)
    number2=tk.Entry(multi_add, textvariable = num2).grid(row=1, column=1)

    return num1, num2 


def result():
    num3 = float(num1.get()) + float(num2.get())
    num4 = float(num1.get()) * float(num2.get())
    print("the result is ",num3 ,"and", num4)

'''buttons'''
result=tk.Button(frame, text="Result", font=('airal', 30 ,'bold'), height="1"
                 ,width="6", padx=10,pady=5, fg="Black",
                 bg="yellow", command= result)
result.place(x=300 , y=305)

op1=tk.Button(frame, text="operation 1", height="2", width="10", padx=10,
             pady=5, fg="Black", bg="yellow", command = operation1)
op1.place(x = 17, y = 310)


root.mainloop()

TraceBackError:

D Exception in Tkinter callback: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py", line 1705, in call return self.func(*args) File "/Users/Pozzphut/Desktop/Project Python/FigureFunction transfer.py", line 37, in result num3 = float(num1.get()) + float(num2.get()) ValueError: could not convert string to float:


Solution

  • Here is the solution:

    If you use .grid() in same line then number1 and number2 will return None and we are unable to get the data.

    Changes made as follows:

        number1=tk.Entry(multi_add, textvariable = num1)
        number2=tk.Entry(multi_add, textvariable = num2)
        number1.grid(row=0, column=1)
        number2.grid(row=1, column=1)
    

    and

    def result():
        num3 = float(number1.get()) + float(number2.get())
        num4 = float(number1.get()) * float(number2.get())
    

    Here's the whole code:

    import tkinter as tk
    
    '''OS'''
    root = tk.Tk()
    root.title("Simple Box")
    operator=""
    
    canvas = tk.Canvas(root, height=400,width=500,bg="White")
    canvas.pack()
    
    frame=tk.Frame(root, bg="Light Blue")
    frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)
    '''Variable '''
    num1= tk.StringVar()
    num2= tk.StringVar()
    
    ''' function'''
    def operation1():
        global number1, number2
        multi_add =tk.Tk()
        multi_add.title("Multiplication and Addition")
    
        tk.Label(multi_add, font =("Helvetica", 20),
                 text="Enter the first number:").grid(row=0)
        tk.Label(multi_add, font =("Helvetica", 20),
                 text="Enter the second number").grid(row=1)
    
        number1=tk.Entry(multi_add, textvariable = num1)
        number2=tk.Entry(multi_add, textvariable = num2)
        number1.grid(row=0, column=1)
        number2.grid(row=1, column=1)
    
    def result():
        num3 = float(number1.get()) + float(number2.get())
        num4 = float(number1.get()) * float(number2.get())
        print("the result is ",num3 ,"and", num4)
    
    '''buttons'''
    result=tk.Button(frame, text="Result", font=('airal', 30 ,'bold'), height="1"
                     ,width="6", padx=10,pady=5, fg="Black",
                     bg="yellow", command= result)
    result.place(x=300 , y=305)
    
    op1=tk.Button(frame, text="operation 1", height="2", width="10", padx=10,
                 pady=5, fg="Black", bg="yellow", command = operation1)
    op1.place(x = 17, y = 310)
    
    
    root.mainloop()
    

    and the 2nd approach

    No need to declare variables num1 and num2:

    import tkinter as tk
    
    '''OS'''
    root = tk.Tk()
    root.title("Simple Box")
    operator=""
    
    canvas = tk.Canvas(root, height=400,width=500,bg="White")
    canvas.pack()
    
    frame=tk.Frame(root, bg="Light Blue")
    frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)
    
    ''' function'''
    def operation1():
        global number1, number2
        multi_add =tk.Tk()
        multi_add.title("Multiplication and Addition")
    
        tk.Label(multi_add, font =("Helvetica", 20),
                 text="Enter the first number:").grid(row=0)
        tk.Label(multi_add, font =("Helvetica", 20),
                 text="Enter the second number").grid(row=1)
    
        number1=tk.Entry(multi_add)
        number2=tk.Entry(multi_add)
        number1.grid(row=0, column=1)
        number2.grid(row=1, column=1)
    
    def result():
        num3 = float(number1.get()) + float(number2.get())
        num4 = float(number1.get()) * float(number2.get())
        print("the result is ",num3 ,"and", num4)
    
    '''buttons'''
    result=tk.Button(frame, text="Result", font=('airal', 30 ,'bold'), height="1"
                     ,width="6", padx=10,pady=5, fg="Black",
                     bg="yellow", command= result)
    result.place(x=300 , y=305)
    
    op1=tk.Button(frame, text="operation 1", height="2", width="10", padx=10,
                 pady=5, fg="Black", bg="yellow", command = operation1)
    op1.place(x = 17, y = 310)
    
    
    root.mainloop()