Odświeżanie w tkinter danych pobranych z bazy mysql

0

Witam, piszę program do własnego użytku w pythonie. Jego zadanie jest proste, ma pobierać poszczególne dane z bazy mysql z ostatniej linii i wyświetlić na ekranie. Do tego ma wyświetlać aktualny czas i co 60 sekund odświeżać dane z tabeli. Mam taki kod:

import tkinter as tk
import mysql.connector

import datetime as dt # dla obsługi daty i czasu

class Application:
    def __init__(self):
        self.window = tk.Tk()

        self.window.geometry("800x600")
        self.window.configure(background="black")
        self.rozmiarWartosci = 28
        
        self.lb_clock = tk.Label(self.window, font = ("Times New Roman", 45), bg = "black", fg = "white")
        self.lb_clock.pack()
        
        self.timer() # pierwsze wywołanie metody timer
        self.sypialnia_pokoj() # wywolanie sypialni


        self.rama = tk.LabelFrame(self.window, text = "Dane Pogodowe - Sypialnia", bg = "black", fg = "white", padx=5, pady = 5)
        self.rama.pack(padx=5, pady=5)

        self.r2 = tk.LabelFrame(self.rama, padx=5, pady=5)
        self.r2.configure(background="black")
        self.r2.pack(fill="both", expand="yes")

        self.lin1 = tk.Label(self.r2, text="Temperatura: ", font=("Arial", 13), bg="black", fg="white")
        self.lin1.grid(row=0, column=0)

        self.lin2 = tk.Label(self.r2, text=self.temp1, font=("Arial", self.rozmiarWartosci), bg="black", fg="white")
        self.lin2.grid(row=0, column=1)

        self.lin3 = tk.Label(self.r2, text="°C", font=("Arial", 13), bg="black", fg="white")
        self.lin3.grid(row=0, column=2)

        self.lin4 = tk.Label(self.r2, text="   ", font=("Arial", 13), bg="black", fg="white")
        self.lin4.grid(row=0, column=3)

        self.lin5 = tk.Label(self.r2, text="Ciśnienie:", font=("Arial", 13), bg="black", fg="white")
        self.lin5.grid(row=0, column=4)

        self.lin6 = tk.Label(self.r2, text=self.cisn1, font=("Arial", self.rozmiarWartosci), bg="black", fg="white")
        self.lin6.grid(row=0, column=5)

        self.lin7 = tk.Label(self.r2, text="hPa", font=("Arial", 13), bg="black", fg="white")
        self.lin7.grid(row=0, column=6)

        self.lin8 = tk.Label(self.r2, text="   ", font=("Arial", 13), bg="black", fg="white")
        self.lin8.grid(row=0, column=7)

        self.lin9 = tk.Label(self.r2, text="Wilgotność:", font=("Arial", 13), bg="black", fg="white")
        self.lin9.grid(row=0, column=8)

        self.lin10 = tk.Label(self.r2, text=self.wilg1, font=("Arial", self.rozmiarWartosci), bg="black", fg="white")
        self.lin10.grid(row=0, column=9)

        self.lin11 = tk.Label(self.r2, text="%", font=("Arial", 13), bg="black", fg="white")
        self.lin11.grid(row=0, column=10)

        self.lin12 = tk.Label(self.r2, text="   ", font=("Arial", 13), bg="black", fg="white")
        self.lin12.grid(row=0, column=11)

        self.lin13 = tk.Label(self.r2, text=self.jakosc1, font=("Arial", self.rozmiarWartosci), bg="black", fg=self.kolor1)
        self.lin13.grid(row=0, column=12)

        
        
        
        self.window.mainloop()
        
    def timer(self):
        self.lb_clock.config(text = str(dt.datetime.now().time()).split(".")[0]) # poberanie czasu
        self.window.after(1000, self.timer) # ustawienie kolejnego wywołania metody timer

    def sypialnia_pokoj(self):
    # baza mysql sypialni
    
        mydb = mysql.connector.connect(host = "localhost", user = "******", password = "******", database = "dom")
        mycursor = mydb.cursor()
        mycursor.execute("SELECT * FROM sypialnia ORDER BY id DESC LIMIT 1")
        wynik = mycursor.fetchall()
        for row in wynik:
            self.temp1=row[6]
            self.cisn1=row[8]
            self.wilg1=row[9]
            self.jakosc1=row[7]
            self.kolor1=row[10]
            
        mycursor.close()
        self.window.after(1000, self.sypialnia_pokoj)

apl = Application()

Program pokazuje aktualną godzinę, odczytuje ostatnie dane z bazy, ale już nie odświeża odczytu z bazy. Siedzę nad tym 2 dni i sam sobie nie poradzę. Co zrobić?

1

W takim wypadku najprostsze będzie użycie StringVar(). Np.: tak:

import tkinter as tk
import mysql.connector

import datetime as dt # dla obsługi daty i czasu

class Application:
    def __init__(self):
        self.window = tk.Tk()
        self.temp1 = tk.StringVar()
        self.cisn1 = tk.StringVar()
        self.wilg1 = tk.StringVar()
        self.jakosc1 = tk.StringVar()
        self.kolor1 = tk.StringVar()

        self.window.geometry("800x600")
        ...
        self.lin2 = tk.Label(self.r2, textvariable=self.temp1, font=("Arial", self.rozmiarWartosci), bg="black", fg="white")
        self.lin2.grid(row=0, column=1)
        ...
        self.lin6 = tk.Label(self.r2, textvariable=self.cisn1, font=("Arial", self.rozmiarWartosci), bg="black", fg="white")
        self.lin6.grid(row=0, column=5)
        ...
        self.lin10 = tk.Label(self.r2, textvariable=self.wilg1, font=("Arial", self.rozmiarWartosci), bg="black", fg="white")
        self.lin10.grid(row=0, column=9)
        ...
        self.lin13 = tk.Label(self.r2, textvariable=self.jakosc1, font=("Arial", self.rozmiarWartosci), bg="black", fg=self.kolor1.get())
        self.lin13.grid(row=0, column=12)
        ...
    def sypialnia_pokoj(self):
    # baza mysql sypialni
    
        mydb = mysql.connector.connect(host = "localhost", user = "******", password = "******", database = "dom")
        mycursor = mydb.cursor()
        mycursor.execute("SELECT * FROM sypialnia ORDER BY id DESC LIMIT 1")
        wynik = mycursor.fetchall()
        for row in wynik:
            self.temp1.set(row[6])
            self.cisn1.set(row[8])
            self.wilg1.set(row[9])
            self.jakosc1.set(row[7])
            self.kolor1.set(row[10])
            
        mycursor.close()
        self.window.after(1000, self.sypialnia_pokoj)
        ...

https://docs.python.org/3/library/tkinter.html#coupling-widget-variables

0

@Delor: Bardzo dziękuję, działa, nie znałem tej metody, dużo nauki jeszcze przede mną. Pozdrawiam :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1