-1

I'm trying to make this clock on Python, but something goes wrong. After I activate it, Python Shell just does nothing basically. Here's my code:

#Time

import time
from tkinter import *

tk = Tk()
canvas = Canvas(tk,width = 500,height = 500)
tk.title('Clock')
tk.resizable = (0,0)
year_text = canvas.create_text(20,15,text = 'Today is the unknown day in unkown',font = ('Comic Sans MS',20),state = 'normal')
month_day_text = canvas.create_text(430,15,text = 'Unknown',font = ('Comic Sans MS',20),state = 'normal')
time_text = canvas.create_text(200,230,text = 'Unknown:Unknown:Unknown',font = ('Comic Sans MS',35),state = 'normal')
apm_text = canvas.create_text(235,300,text = 'Unknown',font = ('Comic Sans MS',25),state = 'normal')
activate = False


months = ['January','February','March','April','May','June','July','August','September','October','November','December']
week_days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

activate = True


while activate == True:
    _timenow = time.localtime()
    __year = _timenow[0]
    __month = _timenow[1]
    __day = _timenow[2]
    __week_day = _timenow[6]
    __hour = _timenow[3]
    __minute = _timenow[4]
    __second = _timenow[5]
    __year_day = _timenow[6]


    _year = __year
    _month = months[__month - 1]
    _day = __day
    lday = str(_day)[-1]
    _week_day = week_days[__week_day]
    _hour = __hour
    _minute = __minute
    _second = __second
    _year_day = __year_day
    yday = str(_year_day)[-1]


    if lday == '1':
        day = str(_day) + 'st'

    elif lday == '2':
        day = str(_day) + 'nd'

    elif lday == '3':
        day = str(_day) + 'rd'

    elif lday not in ['1','2','3']:
        day = str(_day) + 'th'


    if _hour == 0:
        hour = '12'
        apm = 'A.M.'

    elif _hour < 12:
        hour = str(_hour)
        apm = 'A.M.'

    elif _hour == 12:
        hour = str(_hour)
        apm = 'P.M.'

    elif _hour >= 13:
        hour = str(_hour - 12)
        apm = 'P.M.'

    if yday == '1':
        year_day = str(_year_day) + 'st'

    elif yday == '2':
        year_day = str(_year_day) + 'nd'

    elif yday == '3':
        year_day = str(_year_day) + 'rd'

    elif yday not in ['1','2','3']:
        year_day = str(_year_day) + 'th'


    canvas.itemconfig(year_text,text = f'Today is the {year_day} day in {_year}')
    canvas.itemconfig(month_day_text,text = f'{_month} {day}')
    canvas.itemconfig(time_text,text = f'{hour}:{_minute}:{_second}')
    canvas.itemconfig(apm_text,text = f'{apm}')

It doesn't give me the tkinter canvas though. I think it has something to do with the while loop because when I close out it says:

Your program is still running!
Do you want to kill it?

I don't know what I got wrong.

2 Answers2

0

Your Tkinter Window will not appear if you don't add

tk.mainloop()

at the end.

When you close the app you get a message that your program is running because your while loop is running in the backround.In Tkinter Mainloop will make sure that your application will Update each Frame.I would also suggest that you rename "tk" to "root" or "master" because those names are the most commonly used ones.

RafD
  • 65
  • 1
  • 8
0

The canvas was never pack/grid/place in the tkinter window.

There needs to be a .mainloop() call for tkinter to manage graphical updating, but .mainloop() is a blocking call.

You can use .after(time, function) to setup a function call after an amount of time in milliseconds.

Give this a try:

import time
from tkinter import *

def update():
    _timenow = time.localtime()
    __year = _timenow[0]
    __month = _timenow[1]
    __day = _timenow[2]
    __week_day = _timenow[6]
    __hour = _timenow[3]
    __minute = _timenow[4]
    __second = _timenow[5]
    __year_day = _timenow[7] # I think you wanted index 7 not 6


    _year = __year
    _month = months[__month - 1]
    _day = __day
    lday = str(_day)[-1]
    _week_day = week_days[__week_day]
    _hour = __hour
    _minute = __minute
    _second = __second
    _year_day = __year_day
    yday = str(_year_day)[-1]


    if lday == '1':
        day = str(_day) + 'st'

    elif lday == '2':
        day = str(_day) + 'nd'

    elif lday == '3':
        day = str(_day) + 'rd'

    elif lday not in ['1','2','3']:
        day = str(_day) + 'th'


    if _hour == 0:
        hour = '12'
        apm = 'A.M.'

    elif _hour < 12:
        hour = str(_hour)
        apm = 'A.M.'

    elif _hour == 12:
        hour = str(_hour)
        apm = 'P.M.'

    elif _hour >= 13:
        hour = str(_hour - 12)
        apm = 'P.M.'

    if yday == '1':
        year_day = str(_year_day) + 'st'

    elif yday == '2':
        year_day = str(_year_day) + 'nd'

    elif yday == '3':
        year_day = str(_year_day) + 'rd'

    elif yday not in ['1','2','3']:
        year_day = str(_year_day) + 'th'


    canvas.itemconfig(year_text,text = f'Today is the {year_day} day in {_year}')
    canvas.itemconfig(month_day_text,text = f'{_month} {day}')
    canvas.itemconfig(time_text,text = f'{hour}:{_minute}:{_second}')
    canvas.itemconfig(apm_text,text = f'{apm}')
    tk.after(1000, update)

tk = Tk()
canvas = Canvas(tk,width = 500,height = 500)
canvas.pack()
tk.title('Clock')
tk.resizable(0,0)
year_text = canvas.create_text(20,15,text = 'Today is the unknown day in unkown',font = ('Comic Sans MS',20),state = 'normal')
month_day_text = canvas.create_text(430,15,text = 'Unknown',font = ('Comic Sans MS',20),state = 'normal')
time_text = canvas.create_text(200,230,text = 'Unknown:Unknown:Unknown',font = ('Comic Sans MS',35),state = 'normal')
apm_text = canvas.create_text(235,300,text = 'Unknown',font = ('Comic Sans MS',25),state = 'normal')

months = ['January','February','March','April','May','June','July','August','September','October','November','December']
week_days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

tk.after(10, update)
tk.mainloop()
tgikal
  • 1,667
  • 1
  • 13
  • 27