python - Python3: Continuous variable update within Mainframe -
basically, have created thread continuously writes global variable in infinite loop. have mainframe should read , display variable.
the problem once mainframe runs, displays value of variable read @ time of startup, , not continuously update itself. how mainframe update it's variable values specified interval?
the variable in question called "data":
notes: if run code is, "data" variable set none. adding "time.sleep(5)" before executing mainframe allow time set variable http request, , see data populated.
thanks help!
#!/library/frameworks/python.framework/versions/3.5/bin/python3 # -*- coding: utf-8 -*- tkinter import * import time import urllib.request bs4 import beautifulsoup import threading 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="title 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("600x200") app = example(root) root.mainloop() if __name__ == '__main__': main()
save reference label, , set function updates label , arranges called again. in following example label updated once second.
class example(frame): def __init__(self, parent): ... # call once, after ui has been initialized self.update_data() ... def initui(self): ... # save reference label shows data self.lbl2 = label(frame1, text= data) self.lbl2.pack(fill=x, padx=5, expand=true) ... def update_data(self): # reset label self.lbl2.configure(text=data) # call function again in 1 second self.lbl2.after(1000, self.update_data)
Comments
Post a Comment