python等待转圈
时间: 2023-08-25 18:18:43 浏览: 146
python下载
5星 · 资源好评率100%
以下是一个Python控制台中的等待转圈代码示例:
```python
import time
def loading():
chars = ['⣾', '⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
i = 0
while True:
print('\r%s' % chars[i], end='', flush=True)
i = (i + 1) % len(chars)
time.sleep(0.25)
try:
loading_thread = threading.Thread(target=loading)
loading_thread.start()
# 在这里执行你想要等待的操作
time.sleep(10)
loading_thread.join() # 等待loading线程结束
except Exception as e:
print(e)
print('\n操作完成!')
```
这段代码使用了一个线程来实现等待转圈的效果。在主线程中,我们首先创建一个名为`loading`的函数,该函数负责打印转圈的字符。然后,我们创建一个线程对象,将`loading`函数作为目标传入,并启动线程。接着,我们可以在主线程中执行我们想要等待的操作,例如`time.sleep(10)`表示等待10秒。最后,我们使用`loading_thread.join()`等待`loading`线程结束,并在操作完成后打印出提示信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python控制台加载中转圈动画](https://blog.csdn.net/dscn15848078969/article/details/121993926)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Python中print的骚操作(倒计时、转圈显示、进度条)](https://blog.csdn.net/qq_44168690/article/details/104116406)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文