python - (Tkinter)如何循环程序以在 GUI 上显示新数据?
问题描述
我正在创建一个简单的 GUI 来显示巴士到达时间。如何每分钟刷新一次程序以获取更新的数据?我曾尝试使用 while 循环,但它会阻止窗口打开。
下面是代码
import json
from tkinter import *
window = Tk()
window.title("Welcome to the Smart Bus Stop")
window.configure(bg='black')
#Tried using while loop from here onwards but it didn't work
jsonFile = open('bus_arrival.json', 'r')
values = json.load(jsonFile)
z = len(values)
BusService = ['', '', '', '', '']
BusArr1 = ['', '', '', '', '']
BusArr2 = ['', '', '', '', '']
for x in range(z):
BusService[x] = values[x]["Bus Service"]
BusArr1[x] = values[x]["1st Bus"]
BusArr2[x] = values[x]["2st Bus"]
print("\n")
print(BusService[x])
print(BusArr1[x])
print(BusArr2[x])
BusNo = Label(window, text="Bus", font=("Arial Bold", 30), bg="black", fg="white")
BusNo.grid(row=x, column=0)
ServiceNo = Label(window, text=BusService[x], font=("Arial Bold", 30), bg="black", fg="white")
ServiceNo.grid(row=x, column=1)
Bus1 = Label(window, text=BusArr1[x], font=("Arial Bold", 30), bg="black", fg="white")
Bus1.grid(row=x, column=2)
Bus1 = Label(window, text=BusArr2[x], font=("Arial Bold", 30), bg="black", fg="white")
Bus1.grid(row=x, column=3)
window.mainloop()
这是 json 数据,我有另一个程序不断运行来更新这些数据
[
{
"Bus Service": "127",
"1st Bus": "13:21:17",
"2st Bus": "13:34:30"
},
{
"Bus Service": "168",
"1st Bus": "13:16:50",
"2st Bus": "13:30:35"
},
{
"Bus Service": "27",
"1st Bus": "13:12:38",
"2st Bus": "13:21:00"
},
{
"Bus Service": "72",
"1st Bus": "13:13:24",
"2st Bus": "13:20:45"
},
{
"Bus Service": "",
"1st Bus": "",
"2st Bus": ""
}
]
解决方案
在 Tkinter 中,执行循环/周期性任务的正确方法是使用after
,它将任务放入事件队列中,该队列将在您指定的时间段后定期执行。
import json
import tkinter as tk
window = tk.Tk()
window.title("Welcome to the Smart Bus Stop")
window.configure(bg='black')
with open('bus_arrival.json', 'r') as json_file:
values = json.load(json_file)
z = len(values)
BusService = ['', '', '', '', '']
BusArr1 = ['', '', '', '', '']
BusArr2 = ['', '', '', '', '']
labels = []
for x in range(z):
BusService[x] = values[x]["Bus Service"]
BusArr1[x] = values[x]["1st Bus"]
BusArr2[x] = values[x]["2st Bus"]
BusNo = tk.Label(window, text="Bus", font=("Arial Bold", 30), bg="black", fg="white")
BusNo.grid(row=x, column=0)
ServiceNo = tk.Label(window, text=BusService[x], font=("Arial Bold", 30), bg="black", fg="white")
ServiceNo.grid(row=x, column=1)
Bus1 = tk.Label(window, text=BusArr1[x], font=("Arial Bold", 30), bg="black", fg="white")
Bus1.grid(row=x, column=2)
Bus2 = tk.Label(window, text=BusArr2[x], font=("Arial Bold", 30), bg="black", fg="white")
Bus2.grid(row=x, column=3)
labels.append([ServiceNo, Bus1, Bus2])
def data():
with open('bus_arrival.json', 'r') as json_file:
values = json.load(json_file)
z = len(values)
for x in range(z):
BusService[x] = values[x]["Bus Service"]
BusArr1[x] = values[x]["1st Bus"]
BusArr2[x] = values[x]["2st Bus"]
labels[x][0]['text']=BusService[x]
labels[x][1]['text']=BusArr1[x]
labels[x][2]['text']=BusArr2[x]
window.after(3000, data) #Update labels after 3 seconds
data()
window.mainloop()
推荐阅读
- vuetify.js - 如何将 Vuetify 更新为使用 Vue-cli-2 创建的项目
- .net - 你如何在公共 git 上管理你的 Web.Release.config?
- database - VB - DataGridView Hour column shows as decimal and Update DataGridView
- django - 使用 Amazon S3 静态存储进行静态加载的 django-cms_text_ckeditor 错误
- akka - Constructor vs. preStart: when to use which?
- node.js - 应用程序日志 heroku 应用程序崩溃
- mysql - 从 MySQL 中的现有表创建表
- node.js - MongoDB/Mongoose 基于名称加入
- c++-cli - 如何摆脱在调试模式下确实正确处理的慢 SEHExceptions?
- python - In python adding empty list to dataframe column by using lambda raises valueError