pyqt:如何在外部变量值更改时动态更新小部件属性? [英] pyqt: How to dynamically update widget property on outer variable value change?
问题描述
我有 class Ui_MainWindow(object)
创建一个带有进度条的窗口,class OtherClass(object)
包含局部 int 变量循环递增的方法.
I have class Ui_MainWindow(object)
that creates a window with a progress bar and class OtherClass(object)
that contains method in which the local int variable increments in cycle.
如何将局部变量值变化与进度条值变化联系起来?
How to connect local variable value change to progres bar value change?
import sys
from PyQt4.uic.Compiler.qtproxies import QtGui
from PyQt4 import QtGui
from Ui_MainWindow import Ui_MainWindow
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Ui_MainWindow.py
from PyQt4 import QtCore, QtGui
from MainGui.OtherClass import OtherClass
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def myButtonSlot(self):
objVar=OtherClass()
objVar.method()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(389, 332)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.myButtonSlot)
self.verticalLayout.addWidget(self.pushButton)
self.progressBar = QtGui.QProgressBar(self.centralwidget)
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.verticalLayout.addWidget(self.progressBar)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 389, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
其他类.py
class OtherClass(object):
def method(self):
for i in range(100): # i want to connect variable i to progress bar value
print i
for j in range(100500):
pass
推荐答案
您需要稍微重新组织一下代码.
You need to re-organize your code a little bit.
首先,您应该永远编辑pyuic
生成的UI模块中的代码.相反,将其导入您的主模块并在那里实现您的所有应用程序逻辑.
Firstly, you should never edit the code in the UI module generated by pyuic
. Instead, import it into your main module and implement all your application logic there.
其次,您应该在主模块中创建一个主窗口类,并在其 __init__
方法中进行所有设置.
Secondly, you should create a main-window class in your main module, and do all the setup inside its __init__
method.
解决将循环变量连接到进度条的问题的一种方法是使 OtherClass
成为 QObject
的子类并发出自定义信号:
One way to solve your problem of connecting the loop variable to the progress bar, is to make OtherClass
a subclass of QObject
and emit a custom signal:
from PyQt4 import QtCore
class OtherClass(QtCore.QObject):
valueUpdated = QtCore.pyqtSignal(int)
def method(self):
# i want to connect variable i to progress bar value
for i in range(100):
print i
self.valueUpdated.emit(i)
for j in range(100500):
pass
设置好之后,您可以将 pushButton
的设置及其插槽移动到mainGUI.py",并使用 pyuic
.然后将添加一个槽来处理自定义 valueChanged
信号,该信号将更新进度条并处理任何挂起的 GUI 事件.
With that in place, you would then move the setup for pushButton
and its slot to "mainGUI.py", and re-generate "Ui_MainWindow.py" with pyuic
. A slot would then be added to handle the custom valueChanged
signal, which would update the progress bar and also process any pending GUI events.
所以mainGUI.py"最终看起来像这样:
So "mainGUI.py" would end up looking something like this:
import sys
from PyQt4 import QtGui
from Ui_MainWindow import Ui_MainWindow
from OtherClass import OtherClass
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.myButtonSlot)
self.otherclass = OtherClass(self)
self.otherclass.valueUpdated.connect(self.handleValueUpdated)
def myButtonSlot(self):
self.otherclass.method()
def handleValueUpdated(self, value):
self.progressBar.setValue(value)
QtGui.qApp.processEvents()
def main():
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这篇关于pyqt:如何在外部变量值更改时动态更新小部件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!