Search code examples
pythontkintertkinter-entry

How to input/use float through Entry box tkinter Python?


I am trying to learn about tkinter so i create small GUI python to input 2 numbers and use their tk.Entry() box to input number and find the sum of both. But I fail to update the float number and generate their sum. Can anyone help add some components so to make it work? When i generate the result , it only show 0 which is the initial value of variable.

import tkinter as tk
from tkinter import *

root = tk.Tk()
root.title("Shear and Moment Diagram Calculator")
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)

num1 = float()
num2 = float()

tk.Label(frame, font =("Helvetica", 20),
        text="Number 1:" ).grid(row=0)

tk.Label(frame, font =("Helvetica", 20),
        text="Number 2:" ).grid(row=1,column=0)

num1= tk.Entry(frame, textvariable = num1 ).grid(row=0, column = 1)
num2= tk.Entry(frame, textvariable = num2 ).grid(row=1, column = 1)

def generate():
    num3= num1 + num2
    print(num3)


generate=tk.Button(frame, text="result", height="2", width="10",
                   fg="Black", bg="yellow", command = generate )
generate.grid(row=2, column=2)

mainloop()

Solution

  • You can use a StringVar() for textvariable to accept a floating point numeric string and then convert them to float using float() inside the generate function.

    import tkinter as tk
    
    root = tk.Tk()
    root.title("Shear and Moment Diagram Calculator")
    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)
    
    tk.Label(frame, font=("Helvetica", 20), text="Number 1:").grid(row=0)
    
    tk.Label(frame, font=("Helvetica", 20), text="Number 2:").grid(row=1,column=0)
    
    # define entry variables
    n1 = tk.StringVar()
    n2 = tk.StringVar()
    
    # assign the StringVar to the entry widget textvariables
    num1= tk.Entry(frame, textvariable=n1)
    num1.grid(row=0, column=1)
    num2= tk.Entry(frame, textvariable=n2)
    num2.grid(row=1, column=1)
    
    def generate():
        # get the entered value from the entry field and convert it to float and then add
        num3 = float(n1.get()) + float(n2.get())
        print(num3)
    
    generate=tk.Button(frame, text="result", height="2", width="10", fg="Black", bg="yellow", command=generate )
    generate.grid(row=2, column=2)
    
    root.mainloop()
    

    Here is the GUI and the result of floating number addition:

    GUI

    You can also use DoubleVar() to accomplish this task.

    Hope it helps!