Continuous variable update within Mainframe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stringtheorist
    New Member
    • Jul 2016
    • 2

    Continuous variable update within Mainframe

    Hi everyone,

    Basically, I have created a thread which continuously writes a global variable in a loop. Then I have the mainframe which should read and display that variable.

    The problem is that once the mainframe runs, it displays the value of the variable which it read at the time of startup only, and does not continuously update itself. How do I get the mainframe to update it's variable values with a specified interval?

    The variable in question is called "data":

    Thanks for the help!

    Code:
    #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
    # -*- coding: utf-8 -*-
    
    from tkinter import *
    import time
    import urllib.request
    from bs4 import BeautifulSoup
    import threading
    from queue import Queue
    
    data = None
    
    class httpReq(threading.Thread):
        def run(self):
            
            global data
            
            while True:
                url = "https://twitter.com/realDonaldTrump"
                page = urllib.request.urlopen(url)
                soup = BeautifulSoup(page, "html.parser")
                data = soup.title.text
                print(data)
    
    x = httpReq()
    x.start()
    
    class Example(Frame):
            
            global data
            
            def __init__(self, parent):
                Frame.__init__(self, parent)
                self.parent = parent
                self.initUI()
    
            def initUI(self):
                self.parent.title("Example App")
                self.pack(fill=BOTH, expand=True)
                
                frame1 = Frame(self)
                frame1.pack(fill=X)
                
                lbl1 = Label(frame1, text="DATA", width= 20)
                lbl1.pack(side=LEFT, padx=5, pady=5)
                
                lbl2 = Label(frame1, text= data)
                lbl2.pack(fill=X, padx=5, expand=True)
    
    def main():
        root = Tk()
        root.geometry("1920x1080")
        app = Example(root)
        root.mainloop()
    
    if __name__ == '__main__':
        main()
Working...