Python 使用按钮循环时,如何停止progressbar?
这是一个基本的进度条 我刚试过这个密码。当我按下停止按钮时,进度条正在加载但没有停止,它只是继续 每当我按下停止按钮,应用程序就会锁定,直到进度条完成Python 使用按钮循环时,如何停止progressbar?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,这是一个基本的进度条 我刚试过这个密码。当我按下停止按钮时,进度条正在加载但没有停止,它只是继续 每当我按下停止按钮,应用程序就会锁定,直到进度条完成 import sys import time from PyQt5.QtWidgets import (QApplication, QDialog, QProgressBar, QPushButton) TIME_LIMIT = 100 class Actions(QDialog):
import sys
import time
from PyQt5.QtWidgets import (QApplication, QDialog,
QProgressBar, QPushButton)
TIME_LIMIT = 100
class Actions(QDialog):
"""
Simple dialog that consists of a Progress Bar and a Button.
Clicking on the button results in the start of a timer and
updates the progress bar.
"""
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Progress Bar')
self.progress = QProgressBar(self)
self.progress.setGeometry(0, 0, 300, 100)
self.progress.setMaximum(100)
self.button = QPushButton('Start', self)
self.button.move(0, 30)
self.button2 = QPushButton('Stop', self)
self.button2.move(30, 60)
self.show()
self.show()
self.button.clicked.connect(self.onButtonClick)
self.button2.clicked.connect(self.onButtonClick2)
def onButtonClick(self):
count = 0
while count < TIME_LIMIT:
count += 1
time.sleep(1)
self.progress.setValue(count)
print(count)
stop = 0
if stop == 1:
break
def onButtonClick2(self):
stop = 1
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Actions()
sys.exit(app.exec_())
导入系统
导入时间
从PyQt5.QtWidgets导入(QApplication、QDialog、,
QpressBar、QPushButton)
时限=100
集体诉讼(QDialog):
"""
由进度条和按钮组成的简单对话框。
单击该按钮会启动计时器,然后
更新进度条。
"""
定义初始化(自):
super()。\uuuu init\uuuuu()
self.initUI()
def initUI(self):
self.setWindowTitle(“进度条”)
self.progress=QProgressBar(self)
self.progress.setGeometry(0,0300100)
self.progress.setMaximum(100)
self.button=QPushButton('Start',self)
自动按钮移动(0,30)
self.button2=QPushButton(“停止”,self)
自动按钮2.移动(30,60)
self.show()
self.show()
self.button.clicked.connect(self.onButtonClick)
self.button2.clicked.connect(self.onButtonClick2)
def ON按钮单击(自身):
计数=0
计数<时间限制:
计数+=1
时间。睡眠(1)
self.progress.setValue(计数)
打印(计数)
停止=0
如果停止=1:
打破
def onButtonClick2(自身):
停止=1
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
窗口=操作()
sys.exit(app.exec_())
在您的示例中,
onButtonClick()方法中的stop
变量
onButtonClick2()
方法中的stop
变量是不同的局部变量
while
循环和sleep(1)
功能阻塞主界面
上述任务可能如下所示:
import sys
from PyQt5.QtWidgets import (QApplication, QDialog, QProgressBar, QPushButton)
from PyQt5.QtCore import QThread, pyqtSignal
class Thread(QThread):
update_signal = pyqtSignal(int)
def __init__(self, *args, **kwargs):
super(Thread, self).__init__(*args, **kwargs)
self.count = 0
self.running = True
def run(self):
while self.running and self.count < 100:
self.count += 1
self.update_signal.emit(self.count)
QThread.msleep(100)
def stop(self):
self.running = False
class Actions(QDialog):
"""
Simple dialog that consists of a Progress Bar and a Button.
Clicking on the button results in the start of a timer and
updates the progress bar.
"""
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Progress Bar')
self.progress = QProgressBar(self)
self.progress.setGeometry(0, 0, 300, 100)
self.progress.setMaximum(100)
self.progress.setValue(0)
self.button = QPushButton('Start', self)
self.button.move(0, 30)
self.button2 = QPushButton('Stop', self)
self.button2.setEnabled(False)
self.button2.move(30, 60)
self.button.clicked.connect(self.onButtonClick)
self.button2.clicked.connect(self.on_stop)
self.thread = Thread()
self.thread.update_signal.connect(self.update)
def onButtonClick(self):
self.button2.setEnabled(True)
self.progress.setValue(0)
self.thread.running = True
self.thread.count = 0
self.thread.start()
self.button.setEnabled(False)
def update(self, val):
self.progress.setValue(val)
if val == 100: self.on_stop()
def on_stop(self):
self.thread.stop()
self.button.setEnabled(True)
self.button2.setEnabled(False)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Actions()
window.show()
sys.exit(app.exec_())
导入系统
从PyQt5.QtWidgets导入(QApplication、QDialog、QProgressBar、QPushButton)
从PyQt5.QtCore导入QThread,pyqtSignal
类线程(QThread):
更新_信号=pyqtSignal(int)
定义初始化(self,*args,**kwargs):
超级(线程,自我)。\uuuuu初始化(*args,**kwargs)
self.count=0
self.running=True
def运行(自):
自运行和自计数<100时:
self.count+=1
self.update\u signal.emit(self.count)
QThread.msleep(100)
def停止(自):
self.running=False
集体诉讼(QDialog):
"""
由进度条和按钮组成的简单对话框。
单击该按钮会启动计时器,然后
更新进度条。
"""
定义初始化(自):
super()。\uuuu init\uuuuu()
self.initUI()
def initUI(self):
self.setWindowTitle(“进度条”)
self.progress=QProgressBar(self)
self.progress.setGeometry(0,0300100)
self.progress.setMaximum(100)
self.progress.setValue(0)
self.button=QPushButton('Start',self)
自动按钮移动(0,30)
self.button2=QPushButton(“停止”,self)
self.button2.setEnabled(False)
自动按钮2.移动(30,60)
self.button.clicked.connect(self.onButtonClick)
self.按钮2.点击.连接(self.on_stop)
self.thread=thread()
self.thread.update\u signal.connect(self.update)
def ON按钮单击(自身):
self.button2.setEnabled(真)
self.progress.setValue(0)
self.thread.running=True
self.thread.count=0
self.thread.start()
self.button.setEnabled(False)
def更新(自我,val):
self.progress.setValue(val)
如果val==100:self.on_stop()
def on_停止(自):
self.thread.stop()
self.button.setEnabled(真)
self.button2.setEnabled(False)
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
窗口=操作()
window.show()
sys.exit(app.exec_())