生成的UiConnect.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainBtn</class> <widget class="QWidget" name="MainBtn"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QPushButton" name="okBtn"> <property name="geometry"> <rect> <x>280</x> <y>190</y> <width>75</width> <height>23</height> </rect> </property> <property name="text"> <string>确定</string> </property> </widget> <widget class="QPushButton" name="cancelBtn"> <property name="geometry"> <rect> <x>280</x> <y>230</y> <width>75</width> <height>23</height> </rect> </property> <property name="text"> <string>取消</string> </property> </widget> </widget> <resources/> <connections/> </ui>
使用命令pyUIC:
C:\Python27\python.exe C:\Python27\Lib\site-packages\PyQT4\uic\pyuic.py UiConnect.ui -o UiConnect.py
转换后生成的UiConnect.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'UiConnect.ui' # # Created: Tue Sep 23 14:28:00 2014 # by: PyQt4 UI code generator 4.10.3 # # WARNING! All changes made in this file will be lost! 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_MainBtn(object): def setupUi(self, MainBtn): MainBtn.setObjectName(_fromUtf8("MainBtn")) MainBtn.resize(400, 300) self.okBtn = QtGui.QPushButton(MainBtn) self.okBtn.setGeometry(QtCore.QRect(280, 190, 75, 23)) self.okBtn.setObjectName(_fromUtf8("okBtn")) self.cancelBtn = QtGui.QPushButton(MainBtn) self.cancelBtn.setGeometry(QtCore.QRect(280, 230, 75, 23)) self.cancelBtn.setObjectName(_fromUtf8("cancelBtn")) self.retranslateUi(MainBtn) QtCore.QMetaObject.connectSlotsByName(MainBtn) def retranslateUi(self, MainBtn): MainBtn.setWindowTitle(_translate("MainBtn", "Form", None)) self.okBtn.setText(_translate("MainBtn", "确定", None)) self.cancelBtn.setText(_translate("MainBtn", "取消", None))
新建文件:PyLogic.py,内容如下
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui1btn.ui' # # Created: Mon Sep 22 10:16:26 2014 # by: PyQt4 UI code generator 4.10.3 # # WARNING! All changes made in this file will be lost! import sys from PyQt4.QtCore import * from UiConnect import * class PyLogic(QtGui.QMainWindow): def __init__(self, parent=None): super(PyLogic, self).__init__(parent) self.uiuibtn = Ui_MainBtn() self.uiuibtn.setupUi(self) self.connect(self.uiuibtn.okBtn, QtCore.SIGNAL("clicked()"),self, SLOT("close()")) self.connect(self.uiuibtn.cancelBtn, QtCore.SIGNAL("clicked()"),self, SLOT("hide()")) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = PyLogic() myapp.show() sys.exit(app.exec_())
运行效果: