一、ui文件【designer产物,自行了解】直接调用
1 # -*- coding: utf-8 -*- 2 from PyQt4 import QtCore, QtGui,uic 3 # qtCreatorFile = "TestRunner.ui" 4 Ui_MainWindow, QtBaseClass = uic.loadUiType("TestRunner.ui") 5 6 class MyApp(QtGui.QTabWidget,Ui_MainWindow): 7 def __init__(self): 8 QtGui.QTabWidget.__init__(self) 9 Ui_MainWindow.__init__(self) 10 self.setupUi(self) 11 12 #接口:信号-槽 13 self.run.clicked.connect(self.slot_run)#执行XXX 14 15 16 def slot_run(self): 17 pass 18 19 20 if __name__ == "__main__": 21 app = QtGui.QApplication(sys.argv) 22 window = MyApp() 23 window.show() 24 sys.exit(app.exec_())
二、ui文件转化py
1、命令:python xxxsite-packagesPyQt4uicpyuic.py "ui文件路径" -o "py文件路径"
2、直接在生成的.py文件的后面添加以下代码
1 #方式1、Form窗口等需要先建一个MainWindow 2 class MainWindow(QtGui.QWidget): 3 def __init__(self,parent=None): 4 QtGui.QWidget.__init__(self,parent) 5 self.ui = Ui_Form() 6 self.ui.setupUi(self) 7 8 if __name__ == "__main__": 9 app = QtGui.QApplication(sys.argv) 10 myapp = MainWindow() 11 myapp.show() 12 app.exec_() 13 14 #方式2、MainWindow窗口直接这样【其实其他窗口也可以,就是要先有MainWindow再有ui】 15 if __name__ == "__main__": 16 import sys 17 app = QtGui.QApplication(sys.argv) 18 MainWindow = QtGui.QMainWindow() 19 ui = Ui_MainWindow() 20 ui.setupUi(MainWindow) 21 MainWindow.show() 22 sys.exit(app.exec_())