用ttk写一个按钮,关联一个进度条,进度条开始是隐藏的,按钮按下后显示进度条
时间: 2023-05-28 10:03:46 浏览: 66
```python
import tkinter as tk
from tkinter import ttk
def start_progress():
progress_bar.start(10)
root = tk.Tk()
# 创建进度条
progress_bar = ttk.Progressbar(root, orient="horizontal", length=200, mode="indeterminate")
progress_bar.grid(row=1, column=0, padx=10, pady=10)
progress_bar.stop() # 隐藏进度条
# 创建按钮
start_button = ttk.Button(root, text="Start", command=start_progress)
start_button.grid(row=0, column=0, padx=10, pady=10)
root.mainloop()
```
说明:
- `ttk.Progressbar` 创建进度条,设置 `orient="horizontal"` 表示水平方向,`length=200` 表示长度为 200 像素,`mode="indeterminate"` 表示不确定模式(即不显示进度值)。
- `progress_bar.grid` 将进度条放置在窗口上,设置 `row=1` 表示放在第二行,`column=0` 表示放在第一列,`padx=10` 和 `pady=10` 表示横向和纵向的间距为 10 像素。
- `progress_bar.stop()` 隐藏进度条。
- `ttk.Button` 创建按钮,设置 `text="Start"` 表示按钮上显示的文字是 "Start",`command=start_progress` 表示按钮按下后执行 `start_progress` 函数。
- `start_button.grid` 将按钮放置在窗口上,设置 `row=0` 表示放在第一行,`column=0` 表示放在第一列,`padx=10` 和 `pady=10` 表示横向和纵向的间距为 10 像素。
- `start_progress` 函数会在按钮按下后执行,调用 `progress_bar.start(10)` 开始显示进度条,并设置进度条速度为 10(即每 10 毫秒更新一次)。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)