網(wǎng)站首頁 編程語言 正文
以下是一個(gè)使用 PySide6 創(chuàng)建兩個(gè)按鈕的示例,一個(gè)按鈕用于控制子線程的開始和暫停,另一個(gè)按鈕用于控制子線程的結(jié)束:
import time
from PySide6.QtCore import Qt, QThread, Signal, Slot
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget, QPushButton
# 自定義子線程類
class WorkerThread(QThread):
status_changed = Signal(str) # 自定義信號(hào),用于更新狀態(tài)
def __init__(self, parent=None):
super().__init__(parent)
self.paused = False
self.stopped = False
def run(self):
count = 0
while True:
if not self.paused:
count += 1
self.status_changed.emit(f"Count: {count}")
time.sleep(1)
if self.stopped:
break
def pause(self):
self.paused = True
def resume(self):
self.paused = False
def stop(self):
self.stopped = True
# 主窗口類
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.thread = None
self.start_pause_button = QPushButton("Start")
self.stop_button = QPushButton("Stop")
layout = QVBoxLayout()
layout.addWidget(self.start_pause_button)
layout.addWidget(self.stop_button)
self.setLayout(layout)
self.setWindowTitle("Thread Example")
self.start_pause_button.clicked.connect(self.start_pause_thread)
self.stop_button.clicked.connect(self.stop_thread)
self.update_button_state()
def start_pause_thread(self):
if not self.thread or not self.thread.isRunning():
self.thread = WorkerThread()
self.thread.status_changed.connect(self.update_status)
self.thread.started.connect(self.update_button_state)
self.thread.finished.connect(self.update_button_state)
self.thread.start()
self.update_status("Thread started.")
elif self.thread.paused:
self.thread.resume()
self.update_status("Thread resumed.")
else:
self.thread.pause()
self.update_status("Thread paused.")
def stop_thread(self):
if self.thread and self.thread.isRunning():
self.thread.stop()
self.thread.wait()
self.update_button_state()
self.update_status("Thread stopped.")
@Slot(str)
def update_status(self, text):
print(text) # 輸出到控制臺(tái)
# 在此處更新你的界面的狀態(tài)標(biāo)簽、日志等
def update_button_state(self):
is_running = self.thread and self.thread.isRunning()
self.start_pause_button.setEnabled(True)
self.stop_button.setEnabled(is_running)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
在這個(gè)示例中,我們創(chuàng)建了兩個(gè)按鈕:一個(gè)按鈕用于控制子線程的開始和暫停,另一個(gè)按鈕用于控制子線程的結(jié)束。當(dāng)點(diǎn)擊 "Start" 按鈕時(shí),將會(huì)啟動(dòng)子線程并開始計(jì)數(shù)。如果線程已經(jīng)在運(yùn)行,則點(diǎn)擊按鈕將使線程進(jìn)入暫停狀態(tài),并再次點(diǎn)擊按鈕將恢復(fù)線程。
點(diǎn)擊 "Stop" 按鈕將結(jié)束子線程并停止計(jì)數(shù)。在狀態(tài)更新時(shí)(通過 update_status
函數(shù)),你可以根據(jù)需要進(jìn)行適當(dāng)?shù)奶幚砗驮诮缑嫔系恼故尽?/p>
運(yùn)行代碼后,將顯示一個(gè)具有控制子線程開始、暫停和結(jié)束功能的窗口。請(qǐng)根據(jù)你的需求,更新示例中的按鈕文本、界面元素和狀態(tài)更新函數(shù)來滿足你的實(shí)際需求。請(qǐng)注意,在這個(gè)示例中,輸出狀態(tài)信息到控制臺(tái),你可以根據(jù)需要調(diào)整狀態(tài)信息的展示方式。
原文鏈接:https://blog.csdn.net/qq_21041371/article/details/131316394
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-09-12 在CMD窗口中調(diào)用python函數(shù)的實(shí)現(xiàn)_python
- 2023-03-15 Pandas中字符串和時(shí)間轉(zhuǎn)換與格式化的實(shí)現(xiàn)_python
- 2022-06-23 巧妙使用python?opencv庫玩轉(zhuǎn)視頻幀率_python
- 2022-03-28 Python中三種條件語句示例介紹_python
- 2022-08-01 混淆矩陣Confusion?Matrix概念分析翻譯_其它綜合
- 2022-05-25 python教程之利用pyautogui圖形自動(dòng)化擊敗重復(fù)性辦公任務(wù)_python
- 2022-05-04 Django點(diǎn)贊的實(shí)現(xiàn)示例_python
- 2021-12-03 Android消息機(jī)制Handler深入理解_Android
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支