-2

I am trying to create a fullscreen clock in python that will display fullscreen using tkinter.

so far, I have 2 bits of code which work, to create a fullscreen tkinter window, and to print the time using strftime.

Trying to merge the 2 bits of code together is doing my head in - I can either get a clock running in the shell, or a tkinter window that displays time but does not update.

I need to display the time in a tkinter window AND have the time update every 0.5 seconds.

What I have is below - it is far from finished. Any help greatly appreciated. Jon

import sys
if sys.version_info[0] == 2:
    from Tkinter import *
    import Tkinter as tk
else:
    from tkinter import *
    import tkinter as tk

import time
from time import *
import os
from os import *

clock = tk.Tk() # make it cover the entire screen
w, h = clock.winfo_screenwidth(), clock.winfo_screenheight()
clock.overrideredirect(1)
clock.geometry("%dx%d+0+0" % (w, h))
clock.focus_set() # <-- move focus to this widget
clock.bind("<Escape>", lambda e: e.widget.quit())

###############this is the problem...

while True:
    time = strftime("%A, %d %B %Y %X")
    sleep (0.5)
    #print (time)

###############this is the problem...

text = Text(clock)
text.insert(INSERT, time)
text.pack()
clock.mainloop()
1

1 Answer 1

0

Use after function.

import sys
if sys.version_info[0] == 2:
    from Tkinter import *
    import Tkinter as tk
else:
    from tkinter import *
    import tkinter as tk

from time import *
import os
from os import *

clock = tk.Tk() # make it cover the entire screen
w, h = clock.winfo_screenwidth(), clock.winfo_screenheight()
clock.overrideredirect(1)
clock.geometry("%dx%d+0+0" % (w, h))
clock.focus_set() # <-- move focus to this widget
clock.bind("<Escape>", lambda e: e.widget.quit())

time = strftime("%A, %d %B %Y %X")

def getTime():
    time = strftime("%A, %d %B %Y %X")
    text.delete('1.0', END)                 #delete everything
    text.insert(INSERT, time)               #inser new time
    clock.after(500, getTime)               #wait 0.5 sec and go again

text = Text(clock)
text.insert(INSERT, time)
text.pack()
clock.after(500, getTime)
clock.mainloop()
Sign up to request clarification or add additional context in comments.

10 Comments

oooh shiny. I like it. Thanks. Next stage is to mess about with formatting and a few other bits and pieces such as displaying a week number based on the date. Might be finished by christmas... :-p
i'm sure you'll manage. BTW. if you like the answer, accept it. =)
I think Ive accepted it. First time Ive had to swallow my pride and use stackoverflow...
Can't do it now when its marked as duplicate, and there is nothing wrong with asking for help. We all need it.
It appears that the .overrideredirect(1) thing is a known Tcl/Tk bug. See stackoverflow.com/questions/5886280/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.