Skip to content Skip to sidebar Skip to footer

How To Get Value From Entry (tkinter), Use It In Formula And Print The Result It In Label

When using the function entry of Tkinter, you can write a string value and do things with it; but I'm actually working with formulas. The idea is fairly simple: to put a bunch of b

Solution 1:

You can either get values by .get() from widgets

from tkinter import *
#Create the window
myWindow = Tk()

#Define your formula here
def MyCalculateFunction():

    #Get your valuefrom box_pressure
    #Remember toconvert string tointegerorfloat/double
    pressure, temprature =float(box_pressure.get()), float(box_temprature.get())
    result= pressure + temprature

    #Show your resultwith label
    label_result.config(text="%f + %f = %f" % (pressure, temprature, result))

#Create a input box for pressure
box_pressure = Entry(myWindow)
box_pressure.pack()

#Create a input box for temprature
box_temprature = Entry(myWindow)
box_temprature.pack()

#Create a button
button_calculate = Button(myWindow, text="Calcuate", command=MyCalculateFunction)
button_calculate.pack()

#Create a label
label_result = Label(myWindow)
label_result.pack()

or get it from textvariable

#Bind it with variable
variable_pressure = DoubleVar()
box_pressure = Entry(myWindow, textvariable=variable_pressure)
box_pressure.pack()

#Get/Set value by .get() / .set()
variable_pressure.set(42)

# shows 42
print(variable_pressure.get())

Post a Comment for "How To Get Value From Entry (tkinter), Use It In Formula And Print The Result It In Label"