使用pyside-uic生成Python代码
我该如何从一个QtDesigner文件生成Python代码呢?我找到了pyside-uic这个工具,但找不到它的用法示例。我使用的是Windows 7和Pythonxy,并且在用Spyder这个编辑器。
8 个回答
6
pyside-uic.exe MyWindow.ui -o MyWindow.py
我一直在这样做,效果很好(就我所知)
21
我刚试了Pyside的QUILoader,效果不错:
from PySide import QtGui
from PySide import QtCore
from PySide import QtUiTools
class MyWidget(QtGui.QMainWindow):
def __init__(self, *args):
apply(QtGui.QMainWindow.__init__, (self,) + args)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
file.open(QtCore.QFile.ReadOnly)
self.myWidget = loader.load(file, self)
file.close()
self.setCentralWidget(self.myWidget)
if __name__ == '__main__':
import sys
import os
print("Running in " + os.getcwd() + " .\n")
app = QtGui.QApplication(sys.argv)
win = MyWidget()
win.show()
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
app, QtCore.SLOT("quit()"))
app.exec_()
我用Eclipse和QTDesigner创建了.ui文件(在模块上右键点击,选择“新建 -> 其他..”,然后选择“Qt Designer -> Qt Designer表单”)。不需要特别调用uic。
39
pyside-uic 和 pyuic4 基本上是一样的,所以使用说明书上是这么写的:
Usage:
pyside-uic [options] <ui-file>
Options:
--version
show program's version number and exit
-h,--help
show this help message and exit
-oFILE,--output=FILE
write generated code to FILE instead of stdout
-x,--execute
generate extra code to test and display the class
-d,--debug
show debug output
-iN,--ident=N
set indent width to N spaces, tab if N is 0 (default: 4)
我通常这样使用它:
pyside-uic -o output.py input.ui