-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()
Jon
  • 113
  • 1
  • 1
  • 10

1 Answers1

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()
Stevo Mitric
  • 1,570
  • 18
  • 19
  • 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 – Jon May 14 '16 at 18:36
  • i'm sure you'll manage. BTW. if you like the answer, accept it. =) – Stevo Mitric May 14 '16 at 19:00
  • I think Ive accepted it. First time Ive had to swallow my pride and use stackoverflow... – Jon May 14 '16 at 19:08
  • Can't do it now when its marked as duplicate, and there is nothing wrong with asking for help. We all need it. – Stevo Mitric May 14 '16 at 19:10
  • True, but I only ask for help when Ive bashed my head against a brick wall for so long that I can hear a squishy noise...Thanks for the help mate. – Jon May 14 '16 at 19:13
  • FWIW, this has a slight problem for me on Python 2.6 on a Linux system running KDE 4.5.3. It doesn't quit when I hit the Esc key, I had to use Ctrl-Alt-Esc to kill the window. It does respond to Esc if I comment out the `.overrideredirect(1)` call, and it works fine on Python 3.6. – PM 2Ring May 15 '16 at 06:30
  • BTW, doing `import *` is not good practice since it pollutes your namespace with all the imported names (Tkinter alone imports well over 100 names), and when you `import *` from multiple modules there's the risk of hidden name collisions, where the later imported names mask the earlier names. In this code, your `clock` masks the `clock` from the `time` module. Sure, that doesn't really matter here since this program doesn't use the `clock` from `time`, but if someone decides to adapt this code it could cause a mysterious bug. – PM 2Ring May 15 '16 at 06:36
  • Oops! I somehow messed up my earlier Python 3 test: it also won't close with Esc if `.overrideredirect(1)` has been called. Also, I just noticed your `time` variable clobbers the name of the `time` module. – PM 2Ring May 15 '16 at 06:44
  • @Jon: I guess Stevo's code does those star imports because he inherited them from your code. So you should also read my comments warning of the dangers of `import *`. – PM 2Ring May 15 '16 at 06:52
  • 1
    It appears that the `.overrideredirect(1)` thing is a known Tcl/Tk bug. See http://stackoverflow.com/questions/5886280/python-tkinter-overrideredirect-cannot-receive-keystrokes-linux – PM 2Ring May 15 '16 at 07:07