0

I'm trying to create a label which automatically shows the result from an inputed variable. Basically I'm trying to combine these two programs :

from tkinter import *

root = Tk()
var = StringVar()
var.set('hello')
l = Label(root, textvariable = var)
l.pack()
t = Entry(root, textvariable = var)
t.pack()
root.mainloop() # the window is now displayed

this one (source : Update Tkinter Label from variable) automatically updates the label, however it can only update it to what was inputed by the user.

and this one :

from tkinter import *
myWindow = Tk()
def MyCalculateFunction():
    pressure, temprature = float(box_pressure.get()), float(box_temprature.get())
    result = pressure + temperature

label_result.config(text="%f + %f = %f" % (pressure, temprature, result))

box_pressure = Entry(myWindow)
box_pressure.pack()

box_temprature = Entry(myWindow)
box_temprature.pack()

button_calculate = Button(myWindow, text="Calcuate", command=MyCalculateFunction)
button_calculate.pack()

label_result = Label(myWindow)
label_result.pack() 

the problem I have with this one it that if the user changes the pressure or temperature, the result doesn't automatically change. (source : How to get value from entry (Tkinter), use it in formula and print the result it in label)

How can I make it so that when a user changes any variable, Python automatically calculates the new result and changes the label on its own?

sheyy_dt
  • 11
  • 2

1 Answers1

1

Just a few things you missed out.

  1. Tkinters widgets need variables to hold the values inputed into them, which you missed out on creating in your temperature and pressure widgets.

  2. You are better served calculating your values and then set the widgets variable.

Hopefully this helps.

    from tkinter import *
    myWindow = Tk()
    def MyCalculateFunction():
         label_variable=StringVar()
         label_result= Label(myWindow, textvariable=label_variable)
         label_result.pack()
         pressure, temperature = float(pressure_variable.get()), float(temperature_variable.get())
         result = pressure + temperature
         label_variable.set("%f + %f = %f" % (pressure, temperature, result))


    pressure_variable=StringVar()
    box_pressure = Entry(myWindow, textvariable=pressure_variable)
    box_pressure.pack()

    temperature_variable=StringVar()
    box_temprature = Entry(myWindow, textvariable=temperature_variable)
    box_temprature.pack()

    button_calculate = Button(myWindow, text="Calcuate",  command=MyCalculateFunction)
    button_calculate.pack()
Samuel Kazeem
  • 787
  • 1
  • 8
  • 15
  • If the code related to label_result and the associated variable is moved outside the function it will no longer add a Label each time the Calculate button is pressed. – Tls Chris Nov 26 '18 at 14:55
  • Also if either Entry field doesn't convert to a float an error occurs. The app needs some type checking in the Calculate function to make it more robust. – Tls Chris Nov 26 '18 at 15:03