Python tkinter编写时钟gui

使用tkinter编写gui,threading处理指针动画,源码如下

  1. clock.py
import tkinter as tk # Python 3
import math
import get_time as gt
import re
import threading
root = tk.Tk()


def createWidgets():
    CANVAS_WIDTH = 200
    CANVAS_HEIGHT = 200
    root.canvas = tk.Canvas(root,width=CANVAS_WIDTH, height=CANVAS_HEIGHT, highlightthickness=0,bg='green')
    root.canvas.pack()
    circle(root.canvas, CANVAS_WIDTH / 2, CANVAS_WIDTH / 2, CANVAS_WIDTH / 2 - 10, 'SlateGray')
    circle(root.canvas, CANVAS_WIDTH / 2, CANVAS_WIDTH / 2, CANVAS_WIDTH / 2 - 16, 'DarkSlateGray')
    circle(root.canvas, CANVAS_WIDTH / 2, CANVAS_WIDTH / 2, CANVAS_WIDTH / 2 - 18, 'white')
    time = gt.get_time()
    root.h = int(re.match('\d+-\d+-\d+ (\d+):\d+:\d+.*', time['datetime']).group(1))
    root.m = int(re.match('\d+-\d+-\d+ \d+:(\d+):\d+.*', time['datetime']).group(1))
    root.s = int(re.match('\d+-\d+-\d+ \d+:\d+:(\d+).*', time['datetime']).group(1))
    timer_h = threading.Timer(1, paint_h)
    timer_h.start()
    root.canvas.bind(sequence="", func=processMouseEvent)
    root.canvas.bind(sequence="", func=getMouseLocation)
    root.canvas.bind(sequence="", func=resetMouseLocation)
    root.canvas.bind(sequence="", func=closeClock)
    for deg in range(0, 360, 5):
        r = CANVAS_WIDTH / 2 - 18
        x = CANVAS_WIDTH / 2 + r * math.cos(deg / 180 * math.pi)
        y = CANVAS_WIDTH / 2 - r * math.sin(deg / 180 * math.pi)
        if deg % 30 == 0:
            end_x = CANVAS_WIDTH / 2 + (r - 10) * math.cos(deg / 180 * math.pi)
            end_y = CANVAS_WIDTH / 2 - (r - 10) * math.sin(deg / 180 * math.pi)
            root.canvas.create_line(x, y, end_x, end_y, fill='black', width=1.5)
            if int(((360 - deg) / 30 + 3) % 12) == 0:
                text = 12
            else:
                text = int(((360 - deg) / 30 + 3) % 12)
            end_text_x = CANVAS_WIDTH / 2 + (r - 14) * math.cos(deg / 180 * math.pi)
            end_text_y = CANVAS_WIDTH / 2 - (r - 14) * math.sin(deg / 180 * math.pi)
            root.canvas.create_text(end_text_x, end_text_y, text=text)
        else:
            end_x = CANVAS_WIDTH / 2 + (r - 5) * math.cos(deg / 180 * math.pi)
            end_y = CANVAS_WIDTH / 2 - (r - 5) * math.sin(deg / 180 * math.pi)
            root.canvas.create_line(x, y, end_x, end_y, fill='grey', width=1)


def circle(canvas, x, y, r, color):
    id = canvas.create_oval(x - r, y - r, x + r, y + r, fill=color, width=0)
    return id


def processMouseEvent(me):
    root.geometry("+%d+%d" %(me.x_root-root.x, me.y_root-root.y))


def getMouseLocation(me):
    root.x = me.x
    root.y = me.y


def resetMouseLocation(me):
    root.x=0
    root.y=0


def closeClock(me):
    root.destroy()


def paint_h():
    CANVAS_WIDTH = 200
    r = CANVAS_WIDTH / 2 - 18
    root.canvas.delete('h')
    root.canvas.delete('m')
    root.canvas.delete('s')
    h_deg = (90 - (root.h + root.m / 60 + root.s / 3600) % 12 * 30) / 180 * math.pi
    end_x_h = CANVAS_WIDTH / 2 + (r - 40) * math.cos(h_deg)
    end_y_h = CANVAS_WIDTH / 2 - (r - 40) * math.sin(h_deg)
    root.canvas.create_line(CANVAS_WIDTH / 2, CANVAS_WIDTH / 2, end_x_h, end_y_h, width=3.2, tag='h')
    m_deg = (90 - (root.m + root.s / 60) * 6) / 180 * math.pi
    end_x_m = CANVAS_WIDTH / 2 + (r - 30) * math.cos(m_deg)
    end_y_m = CANVAS_WIDTH / 2 - (r - 30) * math.sin(m_deg)
    root.canvas.create_line(CANVAS_WIDTH / 2, CANVAS_WIDTH / 2, end_x_m, end_y_m, width=1, tag='m')
    s_deg = (90 - root.s * 6) / 180 * math.pi
    end_x_s = CANVAS_WIDTH / 2 + (r - 20) * math.cos(s_deg)
    end_y_s = CANVAS_WIDTH / 2 - (r - 20) * math.sin(s_deg)
    root.canvas.create_line(CANVAS_WIDTH / 2, CANVAS_WIDTH / 2, end_x_s, end_y_s, width=1, fill='red', tag='s')
    if root.s + 1 < 60:
        root.s = root.s + 1
    else:
        if root.m + 1 < 60:
            root.m = root.m + 1
            root.s = 0
        else:
            if root.h + 1 < 24:
                root.h = root.h + 1
                root.m = 0
                root.s = 0
            else:
                root.h = 0
                root.m = 0
                root.s = 0
    global timer
    timer = threading.Timer(1, paint_h)
    timer.start()


root.overrideredirect(True)
root.geometry("-50+50")
root.lift()
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled",False)
root.wm_attributes("-transparentcolor", "green")
createWidgets(root)
root.mainloop()
  1. get_time.py
# coding:UTF-8
import requests
import datetime,time


def get_time():
    time_url = 'http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json'
    try:
        response = requests.get(time_url)
        if response.status_code == 200:
            present_time = {
                'datetime': response.json()['result']['datetime_1'],
                'dayOfWeek': response.json()['result']['week_2']
            }
            return present_time
        else:
            get_local_time()
    except:
        get_local_time()


def get_local_time():
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    day_of_week = get_week_day(datetime.datetime.now())
    present_time = {
        'datetime': now_time,
        'dayOfWeek': day_of_week
    }
    return present_time

def get_week_day(date):
   week_day_dict = {
        0: '星期一',
        1: '星期二',
        2: '星期三',
        3: '星期四',
        4: '星期五',
        5: '星期六',
        6: '星期天',
   }
   day = date.weekday()
   return week_day_dict[day]

get_time()

你可能感兴趣的:(python)