如何更改标签的文字,直到在 pyqt 中点击按钮(我如何保持此循环) 这是我试过的代码
class
我尝试了这段代码,但是 GUI 并没有出现并进入无限循环。所以,我如何正确循环此代码
这是我保留的循环部分和功能
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_())
提前致谢
raajvamsy
答案 0 :(得分:1)
GUI对循环不友好,因为它们具有默认的主循环:<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="inputKeyUp">keyup</textarea>
<div id="outputKeyUp">Key Up Key:</div>
<b/>
<textarea id="inputKeyPress">keypress</textarea>
<div id="outputKeyPress">Key Press Key:</div>
,在您的情况下最推荐的是使用app.exec_()
,它会在应用程序显示后立即启动并停止当你按下按钮时。
我总是建议不要修改生成Qt Designer的代码,最好创建一个实现逻辑并使用生成的视图的新类,以上所有我在下面的代码中实现了它:
QTimer