Python Digital Clock
There is already a clock displayed on the desktop of most operating systems but it’s always handy to have one on top of the currently open window. To that end, why not create a Python digital clock that can be a companion desktop widget for you.
The Tkinter box can be moved around without affecting the time, maximised or closed by the user at will. We haven’t given the Tkinter clock window a title, so you can add to that easily enough by snipping the code from other examples in this book.
Another area of improvement is to include this code when Windows or Linux starts, so it automatically pops up on the desktop. See also, if you’re able to improve its functionality by including different time zones: Rome, Paris, London, New York, Moscow and so on.
Another example, expanding on the original code, could be a digital stopwatch. For that you could use the following:
DIGCLOCK.PY
This is a surprisingly handy little script and one that we’ve used in the past instead of relying on a watch or even the clock in the system tray of the operating system.
import time import tkinter as tk def tick(time1=’’): # get the current time from the PC time2 = time.strftime(‘%H:%M:%S’) if time2 != time1: time1 = time2 clock.config(text=time2) clock.after(200, tick) root = tk.Tk() clock = tk.Label(root, font=(‘arial’, 20, ‘bold’), bg=’green’) clock.pack(fill=’both’, expand=1) tick() root.mainloop()
Tick Tock
This is a piece of code we’ve used many times in the past to keep track of time while working on multiple monitors and with just a quick glance to where we’ve placed it on the screen.The Tkinter box can be moved around without affecting the time, maximised or closed by the user at will. We haven’t given the Tkinter clock window a title, so you can add to that easily enough by snipping the code from other examples in this book.
Another area of improvement is to include this code when Windows or Linux starts, so it automatically pops up on the desktop. See also, if you’re able to improve its functionality by including different time zones: Rome, Paris, London, New York, Moscow and so on.
Another example, expanding on the original code, could be a digital stopwatch. For that you could use the following:
import tkinter import time class StopWatch(tkinter.Frame): @classmethod def main(cls): tkinter.NoDefaultRoot() root = tkinter.Tk() root.title(‘Stop Watch’) root.resizable(True, False) root.grid _ columnconfigure(0, weight=1) padding = dict(padx=5, pady=5) widget = StopWatch(root, **padding) widget.grid(sticky=tkinter.NSEW, **padding) root.mainloop() def _ _ init _ _ (self, master=None, cnf={}, **kw): padding = dict(padx=kw.pop(‘padx’, 5), pady=kw. pop(‘pady’, 5)) super(). _ _ init _ _ (master, cnf, **kw) self.grid _ columnconfigure(1, weight=1) self.grid _ rowconfigure(1, weight=1) self. _ _ total = 0 self. _ _ label = tkinter.Label(self, text=’Total Time:’) self. _ _ time = tkinter.StringVar(self, ‘0.000000’) self. _ _ display = tkinter.Label(self, textvariable=self. _ _ time) self. _ _ button = tkinter.Button(self, text=’Start’, command=self. _ _ click) self. _ _ label.grid(row=0, column=0, sticky=tkinter.E, **padding) self. _ _ display.grid(row=0, column=1, sticky=tkinter.EW, **padding) self. _ _ button.grid(row=1, column=0, columnspan=2,sticky=tkinter.NSEW, **padding) def _ _ click(self): if self. _ _ button[‘text’] == ‘Start’: self. _ _ button[‘text’] = ‘Stop’ self. _ _ start = time.clock() self. _ _ counter = self.after _ idle(self. _ _ update) else: self. _ _ button[‘text’] = ‘Start’ self.after _ cancel(self. _ _ counter) def _ _ update(self): now = time.clock() diff = now - self. _ _ start self. _ _ start = now self. _ _ total += diff self. _ _ time.set(‘{:.6f}’.format(self. _ _ total)) self. _ _ counter = self.after _ idle(self. _ _ update) if _ _ name _ _ == ‘_ _ main _ _’: StopWatch.main()
the code has many errors
ReplyDeletethere may be an error in the version
ReplyDeleteThankyou for this wondrous post, I am glad I observed this website on yahoo. watches
ReplyDelete