PythonUI_QtDesigner的ui文件转为py文件

py文件这样生成:
在cmd控制台下,进入uic目录:

cd C:\Cocos\tools\Python27\Lib\site-packages\PyQt4\uic

输入命令

python pyuic.py "ui文件路径" -o "py文件路径" 

因为Qt生成的代码得要有其它东西调用才能出来界面
因此如果想运行出界面还需另外添加如下代码

if __name__ == "__main__":   
    app = QtGui.QApplication(sys.argv)   
    myapp = MyForm()   #MyForm是自己的窗体类名
    myapp.show()   
    sys.exit(app.exec_())  

另外窗体类中的部分代码需要修改,按如下示例修改:

import sys
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_MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.testBtn = QtGui.QPushButton(self.centralwidget)
        self.testBtn.setGeometry(QtCore.QRect(310, 300, 121, 51))
        self.testBtn.setObjectName(_fromUtf8("testBtn"))
        self.testBtnEdit = QtGui.QTextEdit(self.centralwidget)
        self.testBtnEdit.setGeometry(QtCore.QRect(240, 120, 271, 71))
        self.testBtnEdit.setObjectName(_fromUtf8("testBtnEdit"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        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.QObject.connect(self.testBtn, QtCore.SIGNAL(_fromUtf8("clicked()")), self.clickTestBtn)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def clickTestBtn(self):
        #self.testBtnEdit.hide()
        #self.setToolTip('This is a QWidget widget')
        self.testBtnEdit.setText('TestBtn \nClicked!!')



    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.testBtn.setText(_translate("MainWindow", "Test", None))
        self.testBtnEdit.setProperty("ShowAns", _translate("MainWindow", "BtnClicked!", None))

此时,运行由ui生成的py文件就可以运行出自己设计的窗口了.

接下来就要对窗体的各信号进行事件的编写.

你可能感兴趣的:(Python,Qt)