0

I have some Python code with the scheduled jobs and I want to print out the information as a label on a tkinter window. but if I open the window the code stops to execute and I have to destroy the window every few seconds.

How can I let the window open and just only print the new information as a label which I have got with the scheduled jobs?

import getpass
from pandas import *
import time
import schedule
import tkinter as tk

username = getpass.getuser()
desktop_path = f"/home/{username}/Desktop"

#print("path=", desktop_path)

#enter the path to the sensors
sensor = '/sys/bus/w1/devices/28-00000007c9b5/w1_slave'
sensor_2 = '/sys/bus/w1/devices/28-000000084de0/w1_slave'

#get current time
now = time.localtime()
current_time = time.strftime("%H:%M:%S", now)

data_dict = {}
data_list = []


class csv_file:
    def __init__(self, filename, path):

        self.filename = filename

        self.path = path

    def readTempSensor(self, sensorName):

        self.sensorName = sensorName

        """Aus dem Systembus lese ich die Temperatur der DS18B20 aus."""

        f = open(self.sensorName, 'r')

        lines = f.readlines()

        f.close()

        return lines


    def read_temp(self, sensorName):

        self.sensorName = sensorName
        lines = self.readTempSensor(self.sensorName)

        # Solange nicht die Daten gelesen werden konnten, bin ich hier in einer Endlosschleife

        while lines[0].strip()[-3:] != 'YES':

            time.sleep(0.2)

            lines = readTempSensor(self.sensorName)

        temperaturStr = lines[1].find('t=')

        # Ich überprüfe ob die Temperatur gefunden wurde.

        if temperaturStr != -1:

            self.tempData = lines[1][temperaturStr + 2:]

            self.tempCelsius = float(self.tempData) / 1000.0

            self.tempKelvin = 273 + float(self.tempData) / 1000

            self.tempFahrenheit = float(self.tempData) / 1000 * 9.0 / 5.0 + 32.0
            
            # Rückgabe als Array - [0] tempCelsius => Celsius...
            
            self.Temperatur =f"{self.tempCelsius, self.sensorName[20:35]}"
            

            #return self.tempCelsius, self.sensorName
            return self.Temperatur
        

    def open_file(self):

        self.Data = [self.tempCelsius]

        self.time = time.strftime("%H:%M:%S")
        self.entry = pandas.DataFrame(self.Data)

        self.entry['temp'] = f"{self.time}"
        self.entry['temp_2'] = f"{self.sensorName[20:35]}"
        print(self.sensorName[20:35], "-->", "Temp C°", self.Data,"Time -->", self.time)     
               
        self.entry.to_csv(desktop_path + "/" + self.filename, mode='a', header=False, index=False, sep=' ')
        

        return self.Data, self.sensorName[20:35]


def create_file():

    one_sensor.open_file()
    two_sensor.open_file()

def get_temp():
    
    #data_list.clear()
    one_sensor.read_temp(sensor)
    two_sensor.read_temp(sensor_2)
    
    data_list.append(one_sensor.read_temp(sensor))
    data_list.append(two_sensor.read_temp(sensor_2))
    
    return data_list

def max_temp(list):
    
    window = tk.Tk()
    window.geometry("640x640")
    print("max", max(list))
    label = tk.Label(text=f"Maximale Temperatur = {max(list)}")
    label.pack()
    list.clear()
    window.after(5000,lambda:window.destroy())
    window.mainloop()
    

#create a new sensor
one_sensor = csv_file("first_sensor", desktop_path)
two_sensor = csv_file("second_sensor", desktop_path)

#shedule the job

schedule.every(2).seconds.do(get_temp)
schedule.every(2).seconds.do(create_file)
schedule.every(2).seconds.do(max_temp, get_temp())
#schedule.every(15).minutes.do(get_temp)
#schedule.every(15).minutes.do(create_file)

while True:
    schedule.run_pending()
    time.sleep(1)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Take a look at https://docs.python.org/3/library/threading.html – Diego Torres Milano Oct 26 '22 at 19:43
  • Does this answer your question? [Using Multiprocessing module for updating Tkinter GUI](https://stackoverflow.com/questions/13228763/using-multiprocessing-module-for-updating-tkinter-gui) – Random Davis Oct 26 '22 at 19:52
  • Hello thanks for your help, i did this ``` def window(): window = tk.Tk() window.geometry("640x640") label = tk.Label(text=f"Maximale Temperatur {maximal_temp} ") label.pack() #window.after(5000,lambda:window.destroy()) window.mainloop() def window(): window = tk.Tk() window.geometry("640x640") label = tk.Label(text=f"Maximale Temperatur {maximal_temp} ") label.pack() #window.after(5000,lambda:window.destroy()) window.mainloop()``` – Vitalij Sawizki Oct 26 '22 at 20:33
  • Run the ```schedule``` loop in a thread, and move the GUI code in the ```max_temp()``` to the main thread. See [Run in background](https://schedule.readthedocs.io/en/stable/background-execution.html). – relent95 Oct 27 '22 at 01:50

0 Answers0