Home > front end >  my app is not being displayed in the screen
my app is not being displayed in the screen

Time:04-24

I am a beginner python programmer and I have been making this app that tells what time it is ,but when I run it, nothing happens. the program is running, but it's not being displayed in the screen.Here is my code for the app.

import time
import tkinter as tk
root = tk.Tk()
root.geometry('400x400 200-200')
root.title('clock')
lable = tk.Label(root , font=('unispace' , 30 , 'bold' ) , text = 'temporary' , bg = 'blue' , fg = 'white')
lable.grid(row = 0 , column = 0 , sticky = 'nsew')
l = 1
while l != 0:
    seconds = time.time()
    current_time = time.ctime(seconds)
    lable.config(text = current_time)
    lable.grid(row = 0 , column = 0 , sticky = 'nsew')
    time.sleep(1)
root.mainloop()

can someone please help me out with this?

CodePudding user response:

tkinter, like all GUI libraries, is event drive. When you create a graphical element, nothing actually happens. All that does is send messages. The messages don't get popped and processed until the root.mainloop. You need to think about these problems in an event driven way. You cannot use time.sleep nor can you make infinite loops.

You need to use root.after to request a callback after a second. That can update the UI and request another callback a second later.

def update():
    seconds = time.time()
    current_time = time.ctime(seconds)
    lable.config(text = current_time)
    lable.grid(row = 0 , column = 0 , sticky = 'nsew')
    root.after( 1000, update )
update()
root.mainloop()
  • Related