用python绘制七段数码管并实现秒表倒计时功能
时间: 2024-04-30 21:19:40 浏览: 223
首先,需要安装 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()
```
阅读全文