Skip to content Skip to sidebar Skip to footer

Python: How Do You Obtain Variables From Another Script Which Are Constantly Being Updated

I've made a script that uses a while True loop to constantly update a series of variables based on UDP packets that I am constantly recieving. I want to ultimately create a GUI tha

Solution 1:

Below is a sample script to show you how you can update the value in the label widget at a certain time interval. I have provided you the hyperlinks to help you understand tkinter's methods. Best regards.

Key points:

  • use the textvariable option of the tk.Label widget.
  • use tkinter's control variable. I have shown you how to set and get it's value.
  • you can use tkinter's widget method called .after() without having to explicitly use a while-statement and time.sleep() method. Tkinter has it's own event loop that you can use.
  • writing your tkinter GUI as a class makes it easier to implement what you need.

Example Script:

import tkinter as tk

classApp(tk.Frame):
    def__init__( self, master, *args, **kw ):
        super().__init__( master )
        self.master = master
        self.create_label()
        self.update_label()

    defcreate_label( self ):
        self.var = tk.IntVar() # Holds an int; default value 0
        self.label = tk.Label(self, textvariable=self.var ) # Use textvariable not text 
        self.label.pack()

    defupdate_label( self ):
        value = self.get_value()
        self.var.set( value ) # Set the label widget textvariable value. 
        self.after(1000, self.update_label) # Call this method after 1000 ms.defget_value( self ):
        '''To simulate calling a function to return a value'''
        value = self.var.get() + 1return value
    
if __name__ == "__main__":
    root = tk.Tk()
    root.geometry('100x100+0+24')

    app = App( root )
    app.pack()

    root.mainloop() #This command activates tkinter's event loop

Edit:

  • As a clarification, this answer shows how to utilize the .after() and .mainloop() methods in GUI Testing.py, i.e. using tkinter event loop and not use two while-loops, to achieve what you wanted to do. This is a way to simplify your GUI script.
  • For more sophisticated algorithms, e.g. more than one while-loop is involved, you have to look into using threads(note it has its issues) or more recently I found a way of using python's Asyncio approach to do it. The learning curve for these two approaches is a lot steeper. To use the asyncio approach, you can explore modifying my answer to do what you want.

Solution 2:

Best solution is to use threads however If you plan to do in simplest possible manner then implement the main loop inside your Tkinter GUI and once you read the packet simply update it on your GUI in same loop. Here is the Updated and working Code.

import tkinter as tk
import time


def setvalue(self, x):
    self.label.config(text=x, )
    root.update()
    time.sleep(1)

def changevalues(self):
    x = 99
    self.label = tk.Label(root, text=x)
    self.label.pack()
    while x >0:
        x -= 1setvalue(root,x)

root = tk.Tk()
changevalues(root)
root.mainloop()

Post a Comment for "Python: How Do You Obtain Variables From Another Script Which Are Constantly Being Updated"