-->

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.





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.

  1. import time
  2. import tkinter as tk
  3. def tick(time1=’’):
  4.     # get the current time from the PC
  5.     time2 = time.strftime(‘%H:%M:%S’)
  6.     if time2 != time1:
  7.         time1 = time2
  8.         clock.config(text=time2)
  9.  
  10.     clock.after(200, tick)
  11.  
  12. root = tk.Tk()
  13. clock = tk.Label(root, font=(‘arial’, 20, ‘bold’),
  14. bg=’green’)
  15. clock.pack(fill=’both’, expand=1)
  16. tick()
  17. 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:

  1. import tkinter
  2. import time
  3. class StopWatch(tkinter.Frame):
  4. @classmethod
  5. def main(cls):
  6.     tkinter.NoDefaultRoot()
  7.     root = tkinter.Tk()
  8.     root.title(‘Stop Watch’)
  9.     root.resizable(True, False)
  10.     root.grid _ columnconfigure(0, weight=1)
  11.     padding = dict(padx=5, pady=5)
  12.     widget = StopWatch(root, **padding)
  13.     widget.grid(sticky=tkinter.NSEW, **padding)
  14.     root.mainloop()
  15. def _ _ init _ _ (self, master=None, cnf={}, **kw):
  16.     padding = dict(padx=kw.pop(‘padx’, 5), pady=kw.
  17.     pop(‘pady’, 5))
  18.     super(). _ _ init _ _ (master, cnf, **kw)
  19.     self.grid _ columnconfigure(1, weight=1)
  20.     self.grid _ rowconfigure(1, weight=1)
  21.     self. _ _ total = 0
  22.     self. _ _ label = tkinter.Label(self,
  23.     text=’Total Time:’)
  24.     self. _ _ time = tkinter.StringVar(self, ‘0.000000’)
  25.     self. _ _ display = tkinter.Label(self,
  26.     textvariable=self. _ _ time)
  27.     self. _ _ button = tkinter.Button(self,
  28.     text=’Start’, command=self. _ _ click)
  29.     self. _ _ label.grid(row=0, column=0,
  30.     sticky=tkinter.E, **padding)
  31.     self. _ _ display.grid(row=0, column=1,
  32.     sticky=tkinter.EW, **padding)
  33.     self. _ _ button.grid(row=1, column=0,
  34.     columnspan=2,sticky=tkinter.NSEW, **padding)
  35. def _ _ click(self):
  36.     if self. _ _ button[‘text’] == ‘Start’:
  37.         self. _ _ button[‘text’] = ‘Stop’
  38.         self. _ _ start = time.clock()
  39.         self. _ _ counter = self.after _ idle(self. _ _ update)
  40.     else:
  41.         self. _ _ button[‘text’] = ‘Start’
  42.         self.after _ cancel(self. _ _ counter)
  43. def _ _ update(self):
  44.     now = time.clock()
  45.     diff = now - self. _ _ start
  46.     self. _ _ start = now
  47.     self. _ _ total += diff
  48.     self. _ _ time.set(‘{:.6f}’.format(self. _ _ total))
  49.     self. _ _ counter = self.after _ idle(self. _ _ update)
  50.  
  51. if _ _ name _ _ == ‘_ _ main _ _’:
  52.         StopWatch.main()

3 Responses to "Python Digital Clock"

  1. there may be an error in the version

    ReplyDelete
  2. Thankyou for this wondrous post, I am glad I observed this website on yahoo. watches

    ReplyDelete

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel