Nicholas chua
Nicholas chua

Reputation: 25

(Tkinter) How to display time in GUI without duplicating to occur?

I am creating a simple GUI to display bus arrival time and the current time. However, when I try to display time and refresh every second the time duplicates when I do window.after(1000, time)

Below is the code

import json
import datetime
from tkinter import *

window = Tk()
window.title("Welcome to the Smart Bus Stop test")
window.configure(bg='black')

frame = LabelFrame(window,bg='black',bd = 0)
frame.pack(anchor = NW, side = LEFT)

timeWidget = LabelFrame(window, bg ='black', bd =0)
timeWidget.pack(anchor = NE, side = RIGHT)

def time():
    now = datetime.datetime.now()
    Time = now.strftime("%H:%M")
    Day = now.strftime("%A")
    Date = now.strftime("%b %d, %Y")


    displayTime = Label(timeWidget, text= Time, font=("Helvetica", 50), bg="black", fg="white")
    displayTime.pack(anchor = E, side = TOP)
    displayDay = Label(timeWidget, text= Day, font=("Helvetica", 15), bg="black", fg="white")
    displayDay.pack(anchor = E, side = TOP)
    displayDate = Label(timeWidget, text= Date, font=("Helvetica", 15), bg="black", fg="white")
    displayDate.pack(anchor = E, side = TOP)

    window.after(1000, time)



def data():
    with open('bus_arrival.json', 'r') as json_file:
        values = json.load(json_file)

    z = len(values)

    BusService = ['', '', '', '', '']
    BusArr1 = ['', '', '', '', '']
    BusArr2 = ['', '', '', '', '']

    BusNo = Label(frame, text="   Bus   ", font=("Helvetica", 30), bg="black", fg="white")
    BusNo.grid(row=0, column=0)
    NextBus = Label(frame, text=" Next Bus ", font=("Helvetica", 30), bg="black", fg="white")
    NextBus.grid(row=0, column=1)
    SubBus = Label(frame, text=" Subsequent Bus ", font=("Helvetica", 30), bg="black", fg="white")
    SubBus.grid(row=0, column=2)

    for x in range(z):
        BusService[x] = values[x]["Bus Service"]
        BusArr1[x] = values[x]["1st Bus"]
        BusArr2[x] = values[x]["2st Bus"]


        ServiceNo = Label(frame, text=BusService[x], font=("Helvetica", 30), bg="black", fg="white")
        ServiceNo.grid(row=x+1, column=0)
        Bus1 = Label(frame, text=BusArr1[x], font=("Helvetica", 30), bg="black", fg="white")
        Bus1.grid(row=x+1, column=1)
        Bus1 = Label(frame, text=BusArr2[x], font=("Helvetica", 30), bg="black", fg="white")
        Bus1.grid(row=x+1, column=2)

    window.after(1000, data)


time()
data()
window.mainloop()

After a few seconds, the time displays multiple times

Upvotes: 1

Views: 559

Answers (1)

Tomπ
Tomπ

Reputation: 195

Everytime you are calling time() or date() you are also creating and packing widgets. So you need to create them only once and then just change text variable by using widget_label.configure(text=UpdatedValue) or by using StringVar().

Example 1:

from tkinter import *
import datetime

window = Tk()


def time():

    now = datetime.datetime.now()
    Time = now.strftime("%H:%M:%S")
    Day = now.strftime("%A")
    Date = now.strftime("%b %d, %Y")
    # set new values to variables
    time_var.set(Time)
    day_var.set(Day)
    date_var.set(Date)


# Firstly create and pack widgets outside function:
time_var = StringVar()
day_var = StringVar()
date_var = StringVar()
displayTime = Label(window, textvariable=time_var, font=("Helvetica", 50), bg="black", fg="white")
displayTime.pack(anchor=E, side=TOP)
displayDay = Label(window, textvariable=day_var, font=("Helvetica", 15), bg="black", fg="white")
displayDay.pack(anchor=E, side=TOP)
displayDate = Label(window, textvariable=date_var, font=("Helvetica", 15), bg="black", fg="white")
displayDate.pack(anchor=E, side=TOP)

time()

window.mainloop()

Example 2:

from tkinter import *
import datetime

window = Tk()


def time():

    now = datetime.datetime.now()
    Time = now.strftime("%H:%M:%S")
    Day = now.strftime("%A")
    Date = now.strftime("%b %d, %Y")
    # configure text in widget
    displayTime.configure(text=Time)
    displayDay.configure(text=Day)
    displayDate.configure(text=Date)
    window.after(1000, time)


# Firstly create and pack widgets outside function:   
displayTime = Label(window, text='', font=("Helvetica", 50), bg="black", fg="white")
displayTime.pack(anchor=E, side=TOP)
displayDay = Label(window, text='', font=("Helvetica", 15), bg="black", fg="white")
displayDay.pack(anchor=E, side=TOP)
displayDate = Label(window, text='', font=("Helvetica", 15), bg="black", fg="white")
displayDate.pack(anchor=E, side=TOP)

time()

window.mainloop()

Upvotes: 1

Related Questions