【问题标题】:Python code generation with pyside-uic使用 pyside-uic 生成 Python 代码
【发布时间】:2011-05-25 10:41:06
【问题描述】:

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

【问题讨论】:

    标签: python pyside


    【解决方案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.
    

    【讨论】:

    • 天啊!完全错过了 PySide 位。幸运的是,PyQt4 做了很多正确的事情,所以 PySide 不需要改变太多:)
    【解决方案2】:

    QUiLoader 类无需制作中间文件即可完成这项工作。

    http://www.pyside.org/docs/pyside/PySide/QtUiTools/QUiLoader.html

    【讨论】:

    • 截至 2010 年 12 月,pyside 开发人员同意 quiloader 尚未准备好。暂时使用 pyside-uic。
    • 来源?这个位置有变化吗?
    【解决方案3】:

    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
    

    【讨论】:

    • 我通常这样使用它(在 Windows 中):pyside-uic.exe useful_filename.ui &gt; useful_filename_ui.py 然后我可以使用这个简单的命名约定来跟踪。将来我希望不再使用它,而是使用 QUILoader 类(参见下面的@Sven)......
    • 我更喜欢通过 pyside-uic 创建 UI 代码而不是使用加载器,因为 1st:使用设计器和 xml 代码,您永远无法真正了解这些东西的实际放置方式出去。所以你一直在努力如何自己动态编码 Qt。 第 2 次​​b>:编译到 py 然后 pyc 发生一次,直到您更改 ui 而不是每次触发脚本时! 3rd:您避免了代码的大量复杂性。这是import 与所有这些加载程序的东西!我知道这不再是一个真正的时间问题,但我总是觉得在“加载”xml 时浪费了很多。
    【解决方案4】:
    pyside-uic.exe MyWindow.ui -o MyWindow.py 
    

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

    【讨论】:

      【解决方案5】:

      刚刚试用了 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 Form”)。不需要显式的 uic 调用。

      【讨论】:

      • +1 提供了良好、完整的示例。然而,我遇到的第一件事是 QFile 指向文件路径的包顶部目录,而不是该代码所在的目录。只为那些被困在未来的人。
      • 问题是,loader.load返回的是一个对象。但是在 uic 中,我们得到一个类,然后我们可以根据需要对类进行子类化。但是在 loader.load 中总是返回一个实例化的对象,所以没有子类化,没有自定义。即使它无法加载自定义小部件:请参阅此错误:bugreports.qt-project.org/browse/PYSIDE-77
      【解决方案6】:

      PySide 团队目前不鼓励使用 QtUiTools(如另一个答案中所建议的)。

      在此处阅读全文:https://groups.google.com/forum/?fromgroups=#!topic/pyside/_s1HPe6XTZs

      【讨论】:

        【解决方案7】:
        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
        

        你可以使用这种方式来加载UI,也可以获取form_class以及基类作为返回类型...但是如果你不想转换,否则是的,下面是正确的方法。

        pyside-uic.exe MyWindow.ui -o MyWindow.py
        

        【讨论】:

        • 你是从哪里获取 pysideuic 的?因为它不在 PySide 包中。
        【解决方案8】:

        查看 C:\Python27\Lib\site-packages\PySide\scripts\uic.py(或任何你安装了 python 的地方)。如果您查看该脚本,您可以在手册页中看到标记和描述的选项(我不知道如何在 Windows 上正确查看。提示赞赏。)在这里http://manpages.ubuntu.com/manpages/precise/man1/pyside-uic.1.html

        我在尝试查看 C:\Python27\Lib\site-packages\pysideuic\pyside-uic.1 时感到困惑,因为我认为这一定是被调用的文件。由于所有额外的字符,即使试图将其视为手册页对我来说也是不可能的。你不能通过猜测哪些字符是多余的,哪些不是来学习语法!

        在 Windows 上,您当然可以使用批处理文件自动执行此操作,方法是保存带有上述行(以下供参考)的文本文件,扩展名为 .bat,例如 uic_generator.bat。

        pyside-uic MyWindow.ui -o MyWindow.py

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多