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

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

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

    Continue reading...

Compartilhe esta Página