用python中的tkinter制作一个简易的可以控制开始和结束的随机点名
时间: 2024-05-15 21:12:24 浏览: 157
好的,下面是一个简单的随机点名程序,使用 Python 自带的 Tkinter 模块来构建 GUI:
```python
import tkinter as tk
import random
class App:
def __init__(self, master):
self.master = master
master.title("随机点名")
self.label = tk.Label(master, text="")
self.label.pack()
self.button_start = tk.Button(master, text="开始", command=self.start)
self.button_start.pack(side="left", padx=10)
self.button_stop = tk.Button(master, text="停止", command=self.stop)
self.button_stop.pack(side="right", padx=10)
self.is_running = False
def start(self):
self.is_running = True
self.update_label()
def stop(self):
self.is_running = False
def update_label(self):
if self.is_running:
names = ["张三", "李四", "王五", "赵六", "钱七"]
self.label.config(text=random.choice(names))
self.master.after(100, self.update_label)
root = tk.Tk()
app = App(root)
root.mainloop()
```
这个程序包含一个 `App` 类,该类包含一个标签和两个按钮:一个用于开始点名,另一个用于停止点名。当用户点击“开始”按钮时,程序将开始随机点名。当用户点击“停止”按钮时,程序将停止随机点名。点名程序将从一个预定义的名字列表中随机选择一个名字,并将其显示在标签上。点名程序将每 100 毫秒更新一次标签,直到用户点击“停止”按钮为止。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)