python 3, tkinter, SenseHat, how to display a updating tempreture in a label

994 Views Asked by At

I am creating a interface so that I can see temperature, pressure and humidity on a gui on a raspberry pi 7" touchscreen. However I am having trouble with updating the temperature (I haven't done humidity or pressure yet), so what happens is when I run the program it does display the temperature when it is run but it does not continuously update it.

Here is my code, not all of the application however the part at which the tempreture is read and displayed.

main_window = Tk()
temp = sense.get_temperature()
tempreture_label = Label(main_window, text = temp)
tempreture_label.pack()
main_window.mainloop()

I have been looking at other questions where they recommend using set() in slightly similar situations but I cant work out how it would help me.

I have also tried linking my problem to a updating tkinter clock however I believe I am confusing myself.

Any help wold be greatful

1

There are 1 best solutions below

3
Nae On

You can use after method to periodically update the label's text assuming sense.get_temperature() returns the temperature when called:

import tkinter as tk

def temp_up(period_ms):
    global temperature_label
    #temp = sense.get_temperature()
    temp = "simulation"
    print("eacht time")
    temperature_label['text'] = temp
    temperature_label.after(period_ms, temp_up, period_ms)


root = tk.Tk()

temperature_label = tk.Label(root)
temperature_label.pack()

temp_up(1000)

root.mainloop()

Based on Bryan's suggestion which makes use of after's *args, temperature_label.after(period_ms, lambda arg = period_ms: temp_up(arg)) line is replaced with the suggested line.