У меня есть код приложения PyQt5, который при нажатии на кнопку запускает бесконечный цикл, и приложение перестаёт работать при запуске цикла, а я хочу сделать кнопку остановки этого цикла, помогите, пожалуйста, решить проблему
from PyQt5 import QtCore, QtGui, QtWidgets
import pyautogui
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(794, 607)
MainWindow.setStyleSheet("background-color: rgb(0, 0, 0);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setLayoutDirection(QtCore.Qt.LeftToRight)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setEnabled(True)
self.label.setGeometry(QtCore.QRect(-4, -40, 801, 151))
self.label.setMaximumSize(QtCore.QSize(801, 16777215))
self.label.setStyleSheet("background-color: rgb(152, 152, 152);\n"
"font: 48pt \"MS Shell Dlg 2\";\n"
"")
self.label.setLineWidth(1)
self.label.setMidLineWidth(-2)
self.label.setScaledContents(True)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setWordWrap(False)
self.label.setIndent(-2)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(270, 160, 241, 91))
self.pushButton.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(108, 108, 108);")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 794, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.add_functions()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "SOFT"))
self.pushButton.setText(_translate("MainWindow", "Автосвап"))
def add_functions(self):
self.pushButton.clicked.connect(self.autoswap)
def autoswap(self):
while True:
x = 459
y = 1402
Color = pyautogui.screenshot().getpixel((x, y))
print(Color)
if Color == (185, 185, 185):
pyautogui.press("2", presses=4, interval=0.01)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())