0

I'm writing a program that will inform me, when my CPU temperature goes over 70 grades. It works fine in terminal, but I would like to open it in Tkinter window. Is there a way for Tkinter to update value in window every loop from infinity loop?

I made it this way, but it stops after one loop. Is there a way to update the value in window every time without opening new Tkinter window every loop please?


from time import sleep
from gpiozero import CPUTemperature as temp
from gtts import gTTS
import os
from tkinter import *

vessel = ""
directory = '/home/pi/Desktop/Notifications'
files = 'Notifications/70.mp3'

window = Tk()
window.title("CPUの温度モニター")




def round():
    CPUTemperature = temp()
    CPUTemperature = CPUTemperature.temperature
    return CPUTemperature


while True:
    CPUTemp = round()
    if CPUTemp == vessel:
        continue 
    elif CPUTemp != vessel:
        vessel = CPUTemp
        lbl = Label(window, text=vessel)
        lbl.grid(column=0, row=0)
        window.mainloop()
        print (vessel)
        if vessel > 70:
            path = os.path.exists(directory)
            if path == False:
                path = os.makedirs(directory)
            if os.path.isfile(files):
                os.system("mpg123 " + files)
                sleep(5)
            else:
                tts = gTTS(text="警告。CPUの温度は70度を超えました", lang='ja')
                tts.save(files)
                os.system("mpg123 " + files)
                sleep(5)
        sleep(5)





  • `window.mainloop` is not an update function. As the name suggests, it is a loop, so any code afterwards will never be reached. Try using `window.update()` instead – Mirac7 Apr 06 '19 at 07:40
  • Read [How do you run your own code alongside Tkinter's event loop?](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) – stovfl Apr 06 '19 at 09:30
  • If this ran, eventually it would run out of memory and crash because a new `Label` widget is created each iteration of the `while` loop. Existing widgets can be modified by calling their [`configure()`](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) method. You can make that happen automatically by using store the temperature value in a [control variable](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/control-variables.html) and using that to create the `Label` via its `textvariable=` configuration option. – martineau Apr 06 '19 at 09:49

0 Answers0