How to change label text until pushbutton clicked in pyqt

血红的双手。 提交于 2021-02-04 08:40:24

问题


how do i change the text of a label until a push button is clicked in pyqt (how do i keep this in loop) here is the code i tried

       from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(376, 290)
        MainWindow.setMinimumSize(QtCore.QSize(376, 290))
        MainWindow.setMaximumSize(QtCore.QSize(376, 290))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(150, 210, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(140, 70, 91, 31))
        self.label.setObjectName(_fromUtf8("label"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.i = 1
        while  self.pushButton.clicked:
            self.count()
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def count(self):
        self.i+=1
        print self.i
        self.label.setText(str(self.i))

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "PushButton", None))
        self.label.setText(_translate("MainWindow", "TextLabel", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication([])
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I tried this code but the GUI doesn't show up and goes to a infinite loop.so, how do i loop this code properly

this is the loop part and function i kept

        self.i = 1
        while  self.pushButton.clicked:
            self.count()
def count(self):
        self.i+=1
        print self.i
        self.label.setText(str(self.i))

thanks in advance

raajvamsy


回答1:


The GUI is not friendly with loops since they have the default main loop: app.exec_(), the most recommended in your case is to use a QTimer that starts as soon as the application is shown and stops when you press the button.

I always recommend not to modify the code that generates Qt Designer, it is better to create a new class that implements the logic and use the generated view, all the above I implemented it in the following code:

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    counter = 0
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.onTimeout)
        timer.start(10) # 10 milliseconds
        self.pushButton.clicked.connect(timer.stop)

    def onTimeout(self):
        self.counter += 1
        self.label.setText(str(self.counter))


来源:https://stackoverflow.com/questions/45924498/how-to-change-label-text-until-pushbutton-clicked-in-pyqt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!