我有一个多页面GUI并创建了一个后台函数“def data_scan”,它收集新数据(它是机器人上的距离传感器)并更新一些全局变量。我不确定处理页面中标签更新的最佳方法。我正在data_scan中尝试在页面中设置标签txt。但是错误是找不到page1.label等等。
问题:我不确定Page(x)是否应该例行调用变量数据,或者data_scan函数是否应该设置标签txt。
当前代码
import Tkinter as tk
import time
import thread
L_RAW = 0.1
def data_scan ():
global Page1
while 1:
L_RAW = 23.2 #just a static value at this point
Page1.label5['text'] = L_RAW
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
class Page1(Page): #Status Screen
def __init__(self, *args, **kwargs):
self.root = root
self.label5 = tk.Label(self, text = "XX cm")
self.label5.place(x=265, y=255)
class MainApplication(tk.Frame): # Function to Build the Screen Classes
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.root = root
p1 = Page1(self)
buttonframe = tk.Frame(self,background='#444444')
buttonframe.pack(side="top", fill="x", expand=False)
container = tk.Frame(self,background="#888888",)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
b1 = tk.Button(buttonframe, text=" Status ", command=lambda:p1.lift())
b1.pack(side="left", padx=5, pady=4)
thread.start_new_thread(data_scan,())
if __name__ == "__main__":
root = tk.Tk()
main = MainApplication(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("480x320")
root.mainloop()