By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,407 Members | 2,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,407 IT Pros & Developers. It's quick & easy.

Continuous variable update within Mainframe

P: 2
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!

Expand|Select|Wrap|Line Numbers
  1. #!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. from tkinter import *
  5. import time
  6. import urllib.request
  7. from bs4 import BeautifulSoup
  8. import threading
  9. from queue import Queue
  10.  
  11. data = None
  12.  
  13. class httpReq(threading.Thread):
  14.     def run(self):
  15.  
  16.         global data
  17.  
  18.         while True:
  19.             url = "https://twitter.com/realDonaldTrump"
  20.             page = urllib.request.urlopen(url)
  21.             soup = BeautifulSoup(page, "html.parser")
  22.             data = soup.title.text
  23.             print(data)
  24.  
  25. x = httpReq()
  26. x.start()
  27.  
  28. class Example(Frame):
  29.  
  30.         global data
  31.  
  32.         def __init__(self, parent):
  33.             Frame.__init__(self, parent)
  34.             self.parent = parent
  35.             self.initUI()
  36.  
  37.         def initUI(self):
  38.             self.parent.title("Example App")
  39.             self.pack(fill=BOTH, expand=True)
  40.  
  41.             frame1 = Frame(self)
  42.             frame1.pack(fill=X)
  43.  
  44.             lbl1 = Label(frame1, text="DATA", width= 20)
  45.             lbl1.pack(side=LEFT, padx=5, pady=5)
  46.  
  47.             lbl2 = Label(frame1, text= data)
  48.             lbl2.pack(fill=X, padx=5, expand=True)
  49.  
  50. def main():
  51.     root = Tk()
  52.     root.geometry("1920x1080")
  53.     app = Example(root)
  54.     root.mainloop()
  55.  
  56. if __name__ == '__main__':
  57.     main()
  58.  
Jul 27 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.