每当我按下“取消”按钮时,我都希望pyqt应用程序停止。 例如,此应用应该是来自两个表的循环功能。我可以设置一个触发器,该触发器将始终会根据按下“取消”按钮来检查其状况(对/错)。但是有什么方法可以设置全局触发器,该触发器将在任何代码位置停止应用程序进程?
import time
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import (QApplication, QWidget, QToolTip, QPushButton, QMessageBox, QTreeWidget, QTreeWidgetItem, QProgressBar, QLabel)
class Pio(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.launch = False
self.pbar = QProgressBar(self)
self.pbar.setGeometry(20,20,400,50)
self.okButton = QPushButton(self)
self.okButton.setGeometry(80, 20, 100, 50)
self.okButton.move(20, 80)
self.okButton.setText('launch')
self.cncl = QPushButton(self)
self.cncl.setGeometry(300, 20, 100, 50)
self.cncl.move(320, 80)
self.cncl.setText('cancel')
self.okButton.clicked.connect(self.start_loop)
self.cncl.clicked.connect(self.break_loop)
self.show()
def start_loop(self):
if not self.launch:
self.launch = True
print ('started!')
self.loop_numbers()
def break_loop(self):
print ('canceled!')
if self.launch:
print ('work in progress....')
self.launch = False
def loop_numbers(self):
print ('running')
'action #1 that takes a lot of time '
'action #2 that also takes a lot of time'
for n in range(101):
if self.launch:
self.pbar.setValue(n)
time.sleep(0.01)
QtCore.QCoreApplication.processEvents()
for n in range(11):
if self.launch:
self.pbar.setValue(n*10)
time.sleep(0.05)
QtCore.QCoreApplication.processEvents()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Pio()
sys.exit(app.exec_())
如果我将self.launch
移到loop_numbers()
的开头并开始循环,它将在整个过程中运行。
答案 0 :(得分:1)
当最后一个窗口关闭时,应用程序将自动关闭,因此您可以尝试将QWidget
的{{1}}更改为“ QMainWindow
”,只需从取消按钮中调用close。
import sys
import time
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QProgressBar
class Pio(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.launch = False
self.pbar = QProgressBar(self)
self.pbar.setGeometry(20, 20, 400, 50)
self.okButton = QPushButton(self)
self.okButton.setGeometry(80, 20, 100, 50)
self.okButton.move(20, 80)
self.okButton.setText('launch')
self.cncl = QPushButton(self)
self.cncl.setGeometry(300, 20, 100, 50)
self.cncl.move(320, 80)
self.cncl.setText('cancel')
self.okButton.clicked.connect(self.start_loop)
self.cncl.clicked.connect(self.break_loop)
def start_loop(self):
if not self.launch:
self.launch = True
print('started!')
self.loop_numbers()
def break_loop(self):
print('canceled!')
if self.launch:
print('work in progress....')
self.launch = False
# Close window and exit application
self.close()
def loop_numbers(self):
print('running')
'action #1 that takes a lot of time '
'action #2 that also takes a lot of time'
for n in range(101):
if self.launch:
self.pbar.setValue(n)
time.sleep(0.01)
QtCore.QCoreApplication.processEvents()
for n in range(11):
if self.launch:
self.pbar.setValue(n * 10)
time.sleep(0.05)
QtCore.QCoreApplication.processEvents()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = Pio()
mainWindow.show()
sys.exit(app.exec_())