在 Python 的 Tkinter 库中,可以使用 Canvas 控件来绘制图形,包括圆表盘。下面是一个基本的圆表盘程序:

from tkinter import *
import math

def draw_clock():
    # 创建画布
    canvas = Canvas(root, width=400, height=400)
    canvas.pack()

    # 绘制表盘
    canvas.create_oval(50, 50, 350, 350, width=2)

    # 绘制刻度
    for i in range(12):
        x1 = 200 + 150 * math.sin(math.radians(i * 30))
        y1 = 200 - 150 * math.cos(math.radians(i * 30))
        x2 = 200 + 130 * math.sin(math.radians(i * 30))
        y2 = 200 - 130 * math.cos(math.radians(i * 30))
        canvas.create_line(x1, y1, x2, y2, width=2)

    # 绘制指针
    hour = 3
    minute = 45
    second = 30
    hour_x = 200 + 80 * math.sin(math.radians(hour * 30 + minute / 2))
    hour_y = 200 - 80 * math.cos(math.radians(hour * 30 + minute / 2))
    minute_x = 200 + 120 * math.sin(math.radians(minute * 6))
    minute_y = 200 - 120 * math.cos(math.radians(minute * 6))
    second_x = 200 + 140 * math.sin(math.radians(second * 6))
    second_y = 200 - 140 * math.cos(math.radians(second * 6))
    canvas.create_line(200, 200, hour_x, hour_y, width=4, fill='blue')
    canvas.create_line(200, 200, minute_x, minute_y, width=3, fill='green')
    canvas.create_line(200, 200, second_x, second_y, width=2, fill='red')

root = Tk()
draw_clock()
root.mainloop()

该程序使用 Canvas 控件创建了一个 400x400 的画布,并在画布上绘制了一个圆表盘。接着,在表盘上绘制了 12 个刻度,然后绘制了表示小时、分钟和秒钟的指针。指针的长度和颜色不同,以方便区分。

运行上述代码可以看到一个简单的圆表盘程序,但是这个程序还不够复杂,下面我们将对其进行改进。

首先,我们需要添加一个数字时钟,用于显示当前时间。可以使用 Label 控件来实现:

import time

clock_label = Label(root, font=('Arial', 30))
clock_label.pack()

def update_clock():
    current_time = time.strftime('%H:%M:%S')
    clock_label.config(text=current_time)
    root.after(1000, update_clock)

update_clock()

在程序中添加上述代码后,可以看到一个数字时钟出现在了窗口的上方。该时钟每秒钟更新一次,显示当前的时间。

接下来,我们需要让指针动起来。可以使用 after() 方法来实现指针的动态更新:

def update_clock():
    current_time = time.strftime('%H:%M:%S')
    clock_label.config(text=current_time)

    # 更新指针
    hour = int(time.strftime('%H'))
    minute = int(time.strftime('%M'))
    second = int(time.strftime('%S'))
    hour_x = 200 + 80 * math.sin(math.radians(hour * 30 + minute / 2))
    hour_y = 200 - 80 * math.cos(math.radians(hour * 30 + minute / 2))
    minute_x = 200 + 120 * math.sin(math.radians(minute * 6))
    minute_y = 200 - 120 * math.cos(math.radians(minute * 6))
    second_x = 200 + 140 * math.sin(math.radians(second * 6))
    second_y = 200 - 140 * math.cos(math.radians(second * 6))
    canvas.coords(hour_hand, 200, 200, hour_x, hour_y)
    canvas.coords(minute_hand, 200, 200, minute_x, minute_y)
    canvas.coords(second_hand, 200, 200, second_x, second_y)

    root.after(1000, update_clock)

# 绘制指针
hour_hand = canvas.create_line(200, 200, 200, 120, width=4, fill='blue')
minute_hand = canvas.create_line(200, 200, 200, 80, width=3, fill='green')
second_hand = canvas.create_line(200, 200, 200, 60, width=2, fill='red')

update_clock()

在上述代码中,我们用 after() 方法实现了每秒钟更新一次指针的位置。指针的坐标通过 sin() 和 cos() 函数计算得出。由于指针是 Line 对象,因此可以使用 coords() 方法来更新指针的位置。

最后,我们可以在程序中添加一些交互功能,例如添加一个按钮用于启动和停止时钟:

def start_stop_clock():
    global is_running
    is_running = not is_running
    if is_running:
        start_stop_button.config(text='停止')
        update_clock()
    else:
        start_stop_button.config(text='启动')

is_running = False
start_stop_button = Button(root, text='启动', command=start_stop_clock)
start_stop_button.pack()

在上述代码中,我们使用了一个全局变量 is_running 来判断时钟是否正在运行。当用户点击按钮时,该变量的值会被取反,并且按钮的文本也会随之改变。如果时钟正在运行,我们就调用 update_clock() 方法来更新指针的位置。

完整的代码如下:

from tkinter import *
import math
import time

root = Tk()

# 创建画布
canvas = Canvas(root, width=400, height=400)
canvas.pack()

# 绘制表盘
canvas.create_oval(50, 50, 350, 350, width=2)

# 绘制刻度
for i in range(12):
    x1 = 200 + 150 * math.sin(math.radians(i * 30))
    y1 = 200 - 150 * math.cos(math.radians(i * 30))
    x2 = 200 + 130 * math.sin(math.radians(i * 30))
    y2 = 200 - 130 * math.cos(math.radians(i * 30))
    canvas.create_line(x1, y1, x2, y2, width=2)

# 绘制指针
hour_hand = canvas.create_line(200, 200, 200, 120, width=4, fill='blue')
minute_hand = canvas.create_line(200, 200, 200, 80, width=3, fill='green')
second_hand = canvas.create_line(200, 200, 200, 60, width=2, fill='red')

# 数字时钟
clock_label = Label(root, font=('Arial', 30))
clock_label.pack()

# 更新时钟
def update_clock():
    current_time = time.strftime('%H:%M:%S')
    clock_label.config(text=current_time)

    # 更新指针
    hour = int(time.strftime('%H'))
    minute = int(time.strftime('%M'))
    second = int(time.strftime('%S'))
    hour_x = 200 + 80 * math.sin(math.radians(hour * 30 + minute / 2))
    hour_y = 200 - 80 * math.cos(math.radians(hour * 30 + minute / 2))
    minute_x = 200 + 120 * math.sin(math.radians(minute * 6))
    minute_y = 200 - 120 * math.cos(math.radians(minute * 6))
    second_x = 200 + 140 * math.sin(math.radians(second * 6))
    second_y = 200 - 140 * math.cos(math.radians(second * 6))
    canvas.coords(hour_hand, 200, 200, hour_x, hour_y)
    canvas.coords(minute_hand, 200, 200, minute_x, minute_y)
    canvas.coords(second_hand, 200, 200, second_x, second_y)

    root.after(1000, update_clock)

# 启动/停止按钮
def start_stop_clock():
    global is_running
    is_running = not is_running
    if is_running:
        start_stop_button.config(text='停止')
        update_clock()
    else:
        start_stop_button.config(text='启动')

is_running = False
start_stop_button = Button(root, text='启动', command=start_stop_clock)
start_stop_button.pack()

root.mainloop()

通过以上步骤,我们就成功地创建了一个复杂的圆形时钟程序,它包含了数字时钟、动态指针和启动/停止按钮。您可以根据自己的需求对该程序进行进一步的改进,例如添加背景颜色、自定义指针样式等等。

Python Tkinter GUI 时钟程序:打造复杂圆形表盘

原文地址: https://www.cveoy.top/t/topic/nMao 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录