ボタンをクリックすると中断する

2020-02-14 python python-3.x pyside2

以下のコードで基本的なインターフェイスを作成しました。開始ボタンをクリックすると、ループを使用して単語が表示/非表示になります。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gui.ui',
# licensing of 'gui.ui' applies.
#
# Created: Fri Feb 14 11:07:45 2020
#      by: pyside2-uic  running on PySide2 5.13.2
#
# WARNING! All changes made in this file will be lost!

from PySide2 import QtCore, QtGui, QtWidgets
import time

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        spacerItem = QtWidgets.QSpacerItem(20, 117, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem, 0, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(45)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(20, 117, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout.addItem(spacerItem1, 2, 0, 1, 1)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setObjectName("lineEdit")
        self.horizontalLayout.addWidget(self.lineEdit)
        self.horizontalLayout_4.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_2.addWidget(self.label_3)
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.horizontalLayout_2.addWidget(self.lineEdit_2)
        self.horizontalLayout_4.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setObjectName("label_4")
        self.horizontalLayout_3.addWidget(self.label_4)
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.horizontalLayout_3.addWidget(self.lineEdit_3)
        self.horizontalLayout_4.addLayout(self.horizontalLayout_3)
        self.gridLayout.addLayout(self.horizontalLayout_4, 3, 0, 1, 1)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout_5.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_5.addWidget(self.pushButton_2)
        self.gridLayout.addLayout(self.horizontalLayout_5, 4, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))
        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.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.start)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("MainWindow", "Bonjour", None, -1))
        self.label_2.setText(QtWidgets.QApplication.translate("MainWindow", "Mot", None, -1))
        self.lineEdit.setText(QtWidgets.QApplication.translate("MainWindow", "Bonjour", None, -1))
        self.label_3.setText(QtWidgets.QApplication.translate("MainWindow", "Temps ON (s)", None, -1))
        self.lineEdit_2.setText(QtWidgets.QApplication.translate("MainWindow", "1", None, -1))
        self.label_4.setText(QtWidgets.QApplication.translate("MainWindow", "Temps OFF (s)", None, -1))
        self.lineEdit_3.setText(QtWidgets.QApplication.translate("MainWindow", "1", None, -1))
        self.pushButton.setText(QtWidgets.QApplication.translate("MainWindow", "Start", None, -1))
        self.pushButton_2.setText(QtWidgets.QApplication.translate("MainWindow", "Stop", None, -1))


    def start(self):
        mot = self.lineEdit.text()
        print(mot)
        temps_on = float(self.lineEdit_2.text())
        temps_off = float(self.lineEdit_3.text())

        stop = 0

        while True:
            self.label.setText(mot)
            time.sleep(temps_on)
            QtGui.qApp.processEvents()
            self.label.setText('')
            time.sleep(temps_off)
            QtGui.qApp.processEvents()


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_())

しかし、停止ボタンをクリックするとループを停止したいと思います。それを行う方法についてのアイデアはありますか?

通常、関数をguiファイルに直接統合することはありませんが、ここでは重要ではありません。

Answers

GUIでは、GUIをブロックするため、一般に遅延を発生させたい場合は、QTimerを使用するのが適切であるため、sleepを使用しないでください。一方、GUIから数値を取得する場合は、QSpinBox、QDoubleSpinBox、または適切なQValidatorを含むQLineEditを使用して、ユーザーが非数値要素を入力できないようにすることをお勧めします。次の解決策:

from PySide2 import QtCore, QtGui, QtWidgets


class Timer(QtCore.QObject):
    timeout = QtCore.Signal(bool)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._time_on = 1000
        self._time_off = 1000

        self._state = False
        self._is_running = False
        self._timer = QtCore.QTimer(singleShot=True, timeout=self._on_timeout)

    @property
    def state(self):
        return self._state

    @property
    def time_on(self):
        return self._time_on

    @time_on.setter
    def time_on(self, time):
        self._time_on = time

    @property
    def time_off(self):
        return self._time_off

    @time_off.setter
    def time_off(self, time):
        self._time_off = time

    def start(self):
        self._state = True
        self._on_timeout()

    def stop(self):
        self._timer.stop()

    def _on_timeout(self):
        self.timeout.emit(self.state)
        self._timer.start(self.time_on if self.state else self.time_off)
        self._state = not self._state


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.label = QtWidgets.QLabel()
        self.lineedit = QtWidgets.QLineEdit(text="Bonjour")
        self.on_spinbox = QtWidgets.QDoubleSpinBox(minimum=0)
        self.off_spinbox = QtWidgets.QDoubleSpinBox(minimum=0)
        self.start_button = QtWidgets.QPushButton("Start")
        self.stop_button = QtWidgets.QPushButton("Stop")

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QGridLayout(central_widget)
        lay.addWidget(self.label, 0, 0, 1, 6)
        lay.addWidget(QtWidgets.QLabel("Mot"), 1, 0)
        lay.addWidget(self.lineedit, 1, 1)
        lay.addWidget(QtWidgets.QLabel("Temps ON (s)"), 1, 2)
        lay.addWidget(self.on_spinbox, 1, 3)
        lay.addWidget(QtWidgets.QLabel("Temps OFF (s)"), 1, 4)
        lay.addWidget(self.off_spinbox, 1, 5)
        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(self.start_button)
        hlay.addWidget(self.stop_button)
        lay.addLayout(hlay, 2, 0, 1, 6)
        self.resize(640, 480)

        font = QtGui.QFont()
        font.setPointSize(45)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)

        self.start_button.clicked.connect(self.start)
        self.stop_button.clicked.connect(self.stop)

        self.timer = Timer()
        self.timer.timeout.connect(self.onTimeout)

    @QtCore.Slot()
    def start(self):
        self.timer.time_on = 1000 * self.on_spinbox.value()
        self.timer.time_off = 1000 * self.off_spinbox.value()
        self.timer.start()

    @QtCore.Slot()
    def stop(self):
        self.timer.stop()

    @QtCore.Slot(bool)
    def onTimeout(self, state):
        if state:
            self.label.setText(self.lineedit.text())
        else:
            self.label.clear()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Related