[Python] How to update a variable on live

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use Tkinter in order to create a photon counter, but here I will use the time.time() to simulate the data constantly changing. I have tried to synthesize my code to find and easily reproduce the problem.

    The goal is to have a class object from which a method creates a panel that shows the actual time as fast as possible. With the knowledge that later on I will close the panel to do other processes, so my code must continue to run after I close the window.

    Here is my code:

    import numpy as np
    import matplotlib.pyplot as plt
    import tkinter as tk
    import time

    class GUI:
    def __init__(self, master : tk.Tk, string : str):
    self.master = master
    self.string = time.time()
    master.title('A simple window')

    self.label = tk.Label(master, text=self.string)
    self.label.pack()


    def update_text(self, string):
    self.string = time.time()
    self.label.config(text=self.string)

    root=tk.Tk()
    my_gui = GUI(root,"HOLA")

    while 1:
    my_gui.update_text(time.time())


    my_gui.master.update_idletasks()
    my_gui.master.update()

    root.mainloop()


    The code works very well in my opinion. But when I close the panel I get the error TclError: invalid command name ".!label".

    I then tried another version with VarStr and I included my time variable at the beginning of my class (sorry if the description is unclear, but you can look at the code and it will be easier to understand):

    class GUI:
    def __init__(self, master : tk.Tk, value):
    self.master = master

    self.string=tk.StringVar()
    self.string.set(value) #The textvariable already updates the label automatically when the StringVar changes.
    master.title("A simple window")

    self.label = tk.Label(master, textvariable=self.string)
    self.label.pack()
    self.label.after(1, self.update_text)


    def update_text(self):
    self.string.set(self.string)
    self.label.after(1, self.update_text)

    root=tk.Tk()
    my_gui = GUI(root,time.time())

    root.mainloop()


    Now I don't get any Error in the log, but the panel no longer updates and only shows "PY_VAR". I also no longer have my infinite loop from which I get an updated time.time(), which was useful as in my real code I have loops upon loops in which in one of them my variable of interest is updated and when I finished with it I could just close the panel and then should be able to continue running my code.

    Continue reading...

Compartilhe esta Página