Pozz Phut
Pozz Phut

Reputation: 35

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()

Upvotes: 3

Views: 22404

Answers (2)

Somraj Chowdhury
Somraj Chowdhury

Reputation: 1043

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!

Upvotes: 3

furas
furas

Reputation: 142631

textvariable needs tk.StringVar(), tk.IntVar() or tk.DoubleVar()

Using StringVar() you can convert value using float(), calculate sum and put it back in other StringVar() assigned to Label() or Entry(). You can also display text "Error" if values will be incorrect.

enter image description here

import tkinter as tk

# --- functions ---

def generate():
    try:
        result = float(num1.get()) + float(num2.get())
    except Exception as ex:
        print(ex)
        result = 'error'

    num3.set(result)

# --- main ---

root = tk.Tk()

num1 = tk.StringVar()
num2 = tk.StringVar()
num3 = tk.StringVar()

tk.Label(root, text="Number 1:").grid(row=0, column=0)
tk.Label(root, text="Number 2:").grid(row=1, column=0)
tk.Label(root, text="Result:").grid(row=2, column=0)

tk.Entry(root, textvariable=num1).grid(row=0, column=1)
tk.Entry(root, textvariable=num2).grid(row=1, column=1)
tk.Entry(root, textvariable=num3).grid(row=2, column=1)

button = tk.Button(root, text="Calculate", command=generate)
button.grid(row=3, column=1)

root.mainloop()

Upvotes: 5

Related Questions