0

I am creating a simple GUI to display bus arrival time. How can I refresh the program every minute to get updated data? I have tried using while loops but it prevents the window to open.

Below is the code

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()

This is the json data, I have another program that is constantly running to update this data

[
    {
        "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": ""
    }
]
  • Does this answer your question? [making-python-tkinter-label-widget-update](https://stackoverflow.com/questions/1918005) and [tkinter: how to use after method](https://stackoverflow.com/a/25753719/7414759) – stovfl Jun 05 '20 at 07:20

1 Answers1

0

In Tkinter, the correct way to do a recurring/periodic task is to use after, which puts the task in the event queue which will be executed periodically after the time period that you specify.

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()
Miraj50
  • 4,257
  • 1
  • 21
  • 34