1.使用qt designer设计界面,保存为test1.ui:
2.使用pyuic4 test1.ui -o ui.py生成ui代码。
3.程序载入。
import sys import ui from PyQt4 import QtCore, QtGui class MyWidget( QtGui.QWidget ): def __init__(self): super(MyWidget, self).__init__() ui.Ui_Form().setupUi(self) app=QtGui.QApplication(sys.argv) wi = MyWidget() wi.show() app.exec_()
结果:
修改后:
import sys import ui from PyQt4 import QtCore, QtGui class MyWidget( QtGui.QWidget ): def __init__(self): super(MyWidget, self).__init__() self.the_ui=ui.Ui_Form(); self.the_ui.setupUi(self) self.the_ui.progressBar.setValue(0) self.the_ui.progressBar.setMaximum(100) self.the_ui.pushButton.setText("sdfs") self.connect(self.the_ui.pushButton, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('onclick()')) def add(self): for i in range(0, 100): self.the_ui.progressBar.setValue(i) self.the_ui.listWidget.addItem("sdfsdf") @QtCore.pyqtSlot() def onclick(self): self.add() app=QtGui.QApplication(sys.argv) wi = MyWidget() wi.show() app.exec_()