如何在pyqt中单击按钮之前更改标签文本 [英] How to change label text until pushbutton clicked in pyqt
问题描述
我如何更改标签的文本,直到在pyqt中单击按钮(我如何保持循环)这是我试过的代码
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_())
我尝试了这段代码,但 GUI 没有出现并进入无限循环.所以,我该如何正确循环这段代码
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))
提前致谢
raajvamsy
推荐答案
GUI 对循环不友好,因为它们具有默认的主循环:app.exec_()
,这是您最推荐的案例是使用一个 QTimer
,它会在应用程序显示后立即启动,并在您按下按钮时停止.
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.
我一直建议不要修改生成Qt Designer的代码,最好新建一个实现逻辑的类并使用生成的视图,以上都是我在下面代码中实现的:
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))
这篇关于如何在pyqt中单击按钮之前更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!