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_()