使用pyside-uic生成Python代码

Art*_*ijk 18 python pyside

如何从QtDesigner文件生成python代码?我找到了pyside-uic但我找不到语法的例子.我用spyder运行win7和pythonxy.

Fre*_*kNS 34

pyside-uic或多或少与pyuic4完全相同,因此man手册页指定:

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)
Run Code Online (Sandbox Code Playgroud)

我通常这样使用它:

pyside-uic -o output.py input.ui
Run Code Online (Sandbox Code Playgroud)


Sve*_*ven 20

刚试过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_()
Run Code Online (Sandbox Code Playgroud)

我使用Eclipse和QTDesigner创建.ui文件(右键单击模块,"New - > Other ..",选择"Qt Designer - > Qt Designer Form").不需要明确的uic调用.

  • 问题是,loader.load返回一个对象.但是在uic中,我们得到一个类,然后我们可以根据我们的需要对类进行子类化.但是在loader.load中总是返回一个实例化的对象,所以没有子类化,没有自定义.即使它无法加载自定义小部件:请参阅此错误:https://bugreports.qt-project.org/browse/PYSIDE-77 (4认同)

小智 6

import pysideuic
import xml.etree.ElementTree as xml
from cStringIO import StringIO

def loadUiType(uiFile):
    """
    Pyside "loadUiType" command like PyQt4 has one, so we have to convert the 
    ui file to py code in-memory first and then execute it in a special frame
    to retrieve the form_class.
    """
    parsed = xml.parse(uiFile)
    widget_class = parsed.find('widget').get('class')
    form_class = parsed.find('class').text

    with open(uiFile, 'r') as f:
        o = StringIO()
        frame = {}

        pysideuic.compileUi(f, o, indent=0)
        pyc = compile(o.getvalue(), '<string>', 'exec')
        exec pyc in frame

        # Fetch the base_class and form class based on their type
        # in the xml from designer
        form_class = frame['Ui_%s'%form_class]
        base_class = eval('QtGui.%s'%widget_class)

    return form_class, base_class
Run Code Online (Sandbox Code Playgroud)

您可以使用这种方式加载UI,也可以将form_class和基类作为返回类型...但如果您不想转换,否则以下是正确的方法.

pyside-uic.exe MyWindow.ui -o MyWindow.py
Run Code Online (Sandbox Code Playgroud)


Mik*_*ike 5

pyside-uic.exe MyWindow.ui -o MyWindow.py 
Run Code Online (Sandbox Code Playgroud)

是我一直在做的,而且工作正常(据我所知)


小智 1

阅读文档。在这种特殊情况下,http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#pyuic4

The pyuic4 utility is a command line interface to the uic module. The command has the following syntax:

pyuic4 [options] .ui-file

The full set of command line options is:
-h, --help  A help message is written to stdout.
--version   The version number is written to stdout.
-i N, --indent=N
    The Python code is generated using an indentation of N spaces. If N is 0 then a tab is used. The default is 4.
-o FILE, --output=FILE
    The Python code generated is written to the file FILE.
-p, --preview   The GUI is created dynamically and displayed. No Python code is generated.
-w, --pyqt3-wrapper
    The generated Python code includes a small wrapper that allows the GUI to be used in the same way as it is used in PyQt v3.
-x, --execute   The generated Python code includes a small amount of additional code that creates and displays the GUI when it is executes as a standalone application.
--from-imports  Resource modules are imported using from . import rather than a simple import.
Run Code Online (Sandbox Code Playgroud)