如何用python实现倒计时_python 实现倒计时功能(gui界面)

运行效果:

20201111112722506.gif

完整源码:

##import library

from tkinter import *

import time

from playsound import playsound

## display window

root = Tk()

root.geometry('400x300')

root.resizable(0,0)

root.config(bg ='blanched almond')

root.title('TechVidvan - Countdown Clock And Timer')

Label(root, text = 'Countdown Clock and Timer' , font = 'arial 20 bold', bg ='papaya whip').pack()

#display current time#######################

Label(root, font ='arial 15 bold', text = 'current time :', bg = 'papaya whip').place(x = 40 ,y = 70)

####fun to display current time

def clock():

clock_time = time.strftime('%H:%M:%S %p'

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要安装 Python 的 GUI 库 tkinter,可以使用以下命令: ```python pip install tkinter ``` 然后,我们可以开始实现绘制七段数码管的功能。以下是代码实现: ```python import tkinter as tk class SevenSegment(tk.Canvas): def __init__(self, master=None, cnf={}, **kw): super().__init__(master=None, cnf={}, **kw) self.configure(width=60, height=100, bg='black', highlightthickness=0) self.draw_segments([0, 0, 0, 0, 0, 0, 0]) def draw_segments(self, segments): self.delete(tk.ALL) self.create_polygon(5, 5, 55, 5, 50, 10, 10, 10, fill='red' if segments[0] else 'grey', outline='') self.create_polygon(5, 5, 10, 10, 10, 50, 5, 55, fill='red' if segments[1] else 'grey', outline='') self.create_polygon(55, 5, 50, 10, 50, 50, 55, 55, fill='red' if segments[2] else 'grey', outline='') self.create_polygon(5, 55, 10, 50, 10, 90, 5, 95, fill='red' if segments[3] else 'grey', outline='') self.create_polygon(55, 55, 50, 50, 50, 90, 55, 95, fill='red' if segments[4] else 'grey', outline='') self.create_polygon(5, 95, 10, 90, 50, 90, 55, 95, fill='red' if segments[5] else 'grey', outline='') self.create_polygon(10, 50, 50, 50, 55, 55, 10, 55, fill='red' if segments[6] else 'grey', outline='') ``` 这个类实现了一个七段数码管的绘制,接下来我们实现秒表倒计时功能。 ```python import time class Stopwatch: def __init__(self, master): self.master = master self.seven_segment = SevenSegment(master, bg='black') self.seven_segment.grid(row=0, column=0, columnspan=3, padx=10, pady=10) self.start_button = tk.Button(master, text='Start', command=self.start) self.start_button.grid(row=1, column=0, padx=10, pady=10) self.stop_button = tk.Button(master, text='Stop', command=self.stop, state='disabled') self.stop_button.grid(row=1, column=1, padx=10, pady=10) self.reset_button = tk.Button(master, text='Reset', command=self.reset, state='disabled') self.reset_button.grid(row=1, column=2, padx=10, pady=10) self.running = False self.count = 0 self.start_time = 0 self.update() def start(self): self.running = True self.start_button.config(state='disabled') self.stop_button.config(state='normal') self.reset_button.config(state='disabled') self.start_time = time.time() def stop(self): self.running = False self.start_button.config(state='normal') self.stop_button.config(state='disabled') self.reset_button.config(state='normal') def reset(self): self.count = 0 self.update() def update(self): if self.running: self.count = int(time.time() - self.start_time) if self.count < 0: self.count = 0 self.running = False self.start_button.config(state='normal') self.stop_button.config(state='disabled') self.reset_button.config(state='normal') else: self.master.after(100, self.update) else: self.master.after(100, self.update) minutes = self.count // 60 seconds = self.count % 60 self.seven_segment.draw_segments([minutes // 10, minutes % 10, seconds // 10, seconds % 10, 0, 0, 0]) ``` 这个类实现了秒表倒计时功能,并且生成了一个 GUI 界面,包含“Start”、“Stop”、“Reset”按钮和一个七段数码管。接下来我们可以将这个类实例化,生成一个秒表倒计时的窗口。 ```python root = tk.Tk() stopwatch = Stopwatch(root) root.mainloop() ``` 完整代码如下: ```python import tkinter as tk import time class SevenSegment(tk.Canvas): def __init__(self, master=None, cnf={}, **kw): super().__init__(master=None, cnf={}, **kw) self.configure(width=60, height=100, bg='black', highlightthickness=0) self.draw_segments([0, 0, 0, 0, 0, 0, 0]) def draw_segments(self, segments): self.delete(tk.ALL) self.create_polygon(5, 5, 55, 5, 50, 10, 10, 10, fill='red' if segments[0] else 'grey', outline='') self.create_polygon(5, 5, 10, 10, 10, 50, 5, 55, fill='red' if segments[1] else 'grey', outline='') self.create_polygon(55, 5, 50, 10, 50, 50, 55, 55, fill='red' if segments[2] else 'grey', outline='') self.create_polygon(5, 55, 10, 50, 10, 90, 5, 95, fill='red' if segments[3] else 'grey', outline='') self.create_polygon(55, 55, 50, 50, 50, 90, 55, 95, fill='red' if segments[4] else 'grey', outline='') self.create_polygon(5, 95, 10, 90, 50, 90, 55, 95, fill='red' if segments[5] else 'grey', outline='') self.create_polygon(10, 50, 50, 50, 55, 55, 10, 55, fill='red' if segments[6] else 'grey', outline='') class Stopwatch: def __init__(self, master): self.master = master self.seven_segment = SevenSegment(master, bg='black') self.seven_segment.grid(row=0, column=0, columnspan=3, padx=10, pady=10) self.start_button = tk.Button(master, text='Start', command=self.start) self.start_button.grid(row=1, column=0, padx=10, pady=10) self.stop_button = tk.Button(master, text='Stop', command=self.stop, state='disabled') self.stop_button.grid(row=1, column=1, padx=10, pady=10) self.reset_button = tk.Button(master, text='Reset', command=self.reset, state='disabled') self.reset_button.grid(row=1, column=2, padx=10, pady=10) self.running = False self.count = 0 self.start_time = 0 self.update() def start(self): self.running = True self.start_button.config(state='disabled') self.stop_button.config(state='normal') self.reset_button.config(state='disabled') self.start_time = time.time() def stop(self): self.running = False self.start_button.config(state='normal') self.stop_button.config(state='disabled') self.reset_button.config(state='normal') def reset(self): self.count = 0 self.update() def update(self): if self.running: self.count = int(time.time() - self.start_time) if self.count < 0: self.count = 0 self.running = False self.start_button.config(state='normal') self.stop_button.config(state='disabled') self.reset_button.config(state='normal') else: self.master.after(100, self.update) else: self.master.after(100, self.update) minutes = self.count // 60 seconds = self.count % 60 self.seven_segment.draw_segments([minutes // 10, minutes % 10, seconds // 10, seconds % 10, 0, 0, 0]) root = tk.Tk() stopwatch = Stopwatch(root) root.mainloop() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值