Created
February 10, 2019 14:29
-
-
Save 1f0/279506c8cec4433d2af8ea3b517d7e54 to your computer and use it in GitHub Desktop.
A simple clock under windows os, using python library 'keyboard' to detect hot-key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keyboard | |
import time | |
from tkinter import * | |
class Clock(Tk): | |
def __init__(self, master=None): | |
Tk.__init__(self, master) | |
self.wm_attributes("-topmost", 1) | |
self.createWidgets() | |
def tick(self): | |
newTime = time.strftime('%H:%M:%S') | |
if newTime != self.time: | |
self.time = newTime | |
self.timeLabel.config(text=self.time) | |
self.timeLabel.after(500, self.tick) | |
def createWidgets(self): | |
self.time = '' | |
self.timeLabel = Label(self, font=('times', 20, 'bold')) | |
self.timeLabel.pack() | |
self.tick() | |
clock = None | |
def invokeClock(): | |
global clock | |
print('invoke:', id(clock)) | |
if clock == None: | |
clock = Clock() | |
keyboard.add_hotkey('ctrl+alt+k', invokeClock) | |
keyboard.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in linux python2.7, tkinter should be Tkinter