Python 3.x TKInter:在特定时间更新UI上的标签
我试图从API中获取一个JSON文件,并将其保存在一个文件中。然后在Raspberry Pi UI上查看它。我的问题是,更新JSON文件后,UI不会得到更新 代码如下:Python 3.x TKInter:在特定时间更新UI上的标签,python-3.x,tkinter,Python 3.x,Tkinter,我试图从API中获取一个JSON文件,并将其保存在一个文件中。然后在Raspberry Pi UI上查看它。我的问题是,更新JSON文件后,UI不会得到更新 代码如下: class Window(tk.Tk): x = datetime.today() y = x.replace(x.second+30 , hour=0, minute=0, second=0, microsecond=0) delta_t = y - x secs = delta_t.seconds + 1 sched =
class Window(tk.Tk):
x = datetime.today()
y = x.replace(x.second+30 , hour=0, minute=0, second=0, microsecond=0)
delta_t = y - x
secs = delta_t.seconds + 1
sched = BlockingScheduler()
root = tk.Tk()
def reading_APIfile(self):
# getting current time.
print("here is what is in " )
current_time = strftime("%H:%M:%S", gmtime())
# open and read from json file
# threading.Timer(10, self.reading_APIfile()).start()
with open('JSONresponse.json') as a:
json_dict = json.load(a)
# print(current_time)
self.locationDescription = json_dict['locationDescription']
self.location = json_dict['name']
for obj in json_dict['sessions']:
for key in obj:
if obj['stringStartTime'] <= current_time and obj['stringEndTime'] >= current_time:
self.qrCodeToken = obj['token']
self.startTime = obj['stringStartTime']
self.endTime = obj['stringEndTime']
elif obj['stringEndTime'] <= current_time:
self.qrCodeToken = obj['token']
self.startTime = obj['stringStartTime']
self.endTime = obj['stringEndTime']
for x in obj['courses']:
for key1 in x:
self.subject = x['subjectCode'] + " " + x['subject']
self.Name = x['className']
def __init__(self):
# self.gettingAPI()
self.reading_APIfile()
tk.Tk.__init__(self)
self.title("ODUATTAND")
self.geometry('{}x{}'.format(660, 350))
self.attributes('-zoomed', True) # This just maximizes it so we can see the window. It's nothing to do with fullscreen.
#self.frame = Frame(self.tk)
#self.frame.pack()
self.state = False
self.bind("<F11>", self.toggle_fullscreen)
self.bind("<Escape>", self.end_fullscreen)
#Generat Qr code.
big_code = pyqrcode.create(self.qrCodeToken, error='L')
#print(self.qrcodetoken)
big_code.png('code.png', scale=6, module_color=[0, 0, 0, 128], background=[0xff, 0xff, 0xff])
# Main container
# topleft = Frame(self, bg='white')
btm_left = Frame(self, bg='white')
top_right = Frame(self, bg='white')
btm_right = Frame(self, bg='white')
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(0,weight=1)
# old frame setting
for r in range(6):
self.grid_rowconfigure(r, weight=1)
for c in range(5):
self.grid_columnconfigure(c, weight=1)
# x= self.reading_APIfile.subject
# print ("here is X" + x)
#topleft.grid(row = 0, column = 0, rowspan = 4, columnspan = 4, sticky = W+E+N+S)
btm_left.grid(row = 0, column = 0, rowspan = 6, columnspan = 6, sticky = W+E+N+S)
top_right.grid(row = 0, column = 3, rowspan = 4, columnspan = 3,padx=0, sticky = W+E+N+S)
btm_right.grid(row = 2, column = 3, rowspan = 4, columnspan = 3, sticky = W+E+N+S)
btm_right.grid(row=2, column=3, rowspan=4, columnspan=3, sticky=W + E + N + S)
self.label1 = tk.Label(top_right, text= self.subject+ ": "+ self.Name +"\n" + self.location +"\n "+ self.startTime+ " "+ self.endTime)
self.label1.place(x=190,y=45,anchor="center")
self.label1.config(width=300, font=("Arial", 12), bg="white")
# reading the card
self.code = ''
self.label = tk.Label(btm_right, text="PLease swipe your card")
self.label.place(x=150, y=155, anchor="center")
self.label.config(width=200, font=("Arial", 12), bg="white",padx=0)
#if self.code !="":
self.canvas = Canvas(btm_right, width=250, height=200, highlightthickn=0)
self.canvas.place(x=120,y=40, anchor="center")
tk_img = Image.open('check.png')
check_img1=tk_img.resize((150,130), Image.ANTIALIAS)
check_img2=ImageTk.PhotoImage(check_img1)
panel = tk.Label(btm_right, image=check_img2)
panel.image = check_img2
self.x = self.canvas.create_image( 155, 125, image=check_img2)
self.canvas.itemconfigure(self.x, state=tk.HIDDEN)
self.canvas.configure(background='white')
# self.label1.grid(row=2, column=2)
self.bind('<Key>', self.get_key)
#insert QR code
Qrimage = Image.open('code.png')
Qrimage1 = Qrimage.resize((400, 400), Image.ANTIALIAS)
Qrimage2 = ImageTk.PhotoImage(Qrimage1)
# img=img.subsample(2,2)
panel = tk.Label(btm_left, image=Qrimage2)
panel.place(x=200, y=175, anchor="center")
# panel.grid(row=0, column=1, sticky="nw")
panel.image = Qrimage2
# insert a Logo
path = "/home/mohamedshaaban/PycharmProjects/untitled1/986.gif"
image = Image.open('986.gif')
image1 = image.resize((100, 50), Image.ANTIALIAS)
image2 = ImageTk.PhotoImage(image1)
# img=img.subsample(2,2)
panel = tk.Label(btm_right, image=image2)
panel.place(x=160, y=200, anchor="center" )
panel.config(highlightthickness=0)
# panel.grid(row=0, column=1, sticky="nw")
panel.image = image2
top_right.grid_rowconfigure(0, weight=1)
top_right.grid_columnconfigure(1, weight=1)
# self.th = threading.Timer(5.0,self.callinginit)
print("here is what is 222 ")
# self.callinginit()
def callinginit(self):
# self.__init__()
self.reading_APIfile()
print("it is working")
# self.after(150000,self.callinginit())
我试过:虽然是真的:
时间,睡觉
schedule.every.day.at06:00.docallinginit“现在是06:00”,但它不是第一次启动的问题,必须使用time.sleep等待到早上6点,否则一个紧密的while循环会阻止它不更新的GUI的执行
下面的示例每隔几秒钟执行一段代码
注释绑定允许您选择首先立即执行代码,然后等待间隔秒再次执行;默认值是在间隔秒后执行
如果需要在给定时间执行,可以修改它以首先计算从现在到该时间之间的时间,并使用间隔kwarg以秒为单位传递该值
对于长时间间隔,您可以调整root.after5000。。。每5秒调用一次xeq_以获得更大的值,可能每10分钟或每小时调用一次,这取决于准时运行的重要性
它故意使用具有可变默认值的kwarg,以根据您喜欢如何看待它属性来利用闭包/备忘录化
import tkinter as tk
import time
def xeq_every(event, interval=25, memo=[None]):
if memo[0] is None:
memo[0] = time.time() + interval
elif memo[0] > time.time():
print("waiting") # replace with `pass`
else:
print("=======>> executing")
memo[0] = time.time() + interval # set the time of the next execution
root.after(5000, xeq_every, 'dummy_event')
if __name__ == '__main__':
root = tk.Tk()
next_time = tk.IntVar()
next_time.set(f"will xeq at 25 seconds interval")
lbl = tk.Label(root, textvariable=next_time)
lbl.pack()
# root.bind('<Button-1>', lambda event, memo=[time.time()]: xeq_every(event, memo=memo))
root.bind('<Button-1>', xeq_every)
root.mainloop()
在init中使用room.after时出现的问题是,UI没有加载。为什么?对不起,我不明白房间的意思;这是什么意思?我加了root.after5000,init在def\uu init\uu self中为什么?是什么让你觉得每5秒调用一次init是个好主意?对不起,我没有读你的代码,它是杂乱无章的东西无用的问题,你问。也许您应该再次调用需要在多佛上执行的方法,而不是_init__;?如果我误解了您的意思,很抱歉,我所有的UI标签都在init中。这就是为什么我要这么做。
waiting
waiting
waiting
waiting
=======>> executing
waiting
waiting
waiting
waiting
=======>> executing
waiting
waiting
waiting
waiting
=======>> executing