使用 pyside-uic 生成 Python 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4442286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:45:43  来源:igfitidea点击:

Python code generation with pyside-uic

pythonpyside

提问by ArtDijk

How can I generate python code from a QtDesigner file ? I found pyside-uic but I can't find an example for the syntax. I run win7 and pythonxy with spyder.

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

回答by ArtDijk

Read the documentation. In this particular case, http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#pyuic4:

阅读文档。在这种特殊情况下,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.

回答by Neon22

QUiLoader class will do the job without making an intermediate file.

QUiLoader 类将在不制作中间文件的情况下完成这项工作。

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

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

回答by FrederikNS

pyside-uic is more or less identical to pyuic4, as such the man page specifies:

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)

I usually use it like this:

我通常这样使用它:

pyside-uic -o output.py input.ui

回答by Mike

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

is what I've been doing and it's working fine (as far as I know)

是我一直在做的,并且运行良好(据我所知)

回答by Sven

Just tried Pyside's QUILoader, works fine:

刚试过 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_()

I used Eclipse and QTDesigner to create the .ui file (right-click on module, "New -> Other..", choose "Qt Designer -> Qt Designer Form"). No explicit uic call is needed.

我使用 Eclipse 和 QTDesigner 创建 .ui 文件(右键单击模块,“新建 -> 其他..”,选择“Qt 设计器 -> Qt 设计器表单”)。不需要显式的 uic 调用。

回答by Max Ekman

Using QtUiTools (as suggested in another answer) is currently discouraged by the PySide team.

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

Read the full story here: https://groups.google.com/forum/?fromgroups=#!topic/pyside/_s1HPe6XTZs

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

回答by Max Ekman

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

You can use this way to load the UI and can also get form_class as well as the base class as return type... but if you do not want to convert, otherwise Yes the following is the correct way.

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

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

回答by steventaitinger

Look at C:\Python27\Lib\site-packages\PySide\scripts\uic.py (or wherever you have python installed). If you look at that script you can see the options labelled and described as in the man page (Which I don't know how to view properly on windows. Tips appreciated.) here http://manpages.ubuntu.com/manpages/precise/man1/pyside-uic.1.html

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

I got confused for a while trying to look at C:\Python27\Lib\site-packages\pysideuic\pyside-uic.1 as I thought that must be the file that is being called. Even trying to view that as a manual page is impossible for me because of all the extra characters. You can't learn syntax by trying to guess at which characters are extra and which ones aren't!

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

On windows you can automate this with a batch file of course by saving a text file with the above mentioned line(below for reference) with .bat extension like uic_generator.bat.

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

pyside-uic MyWindow.ui -o MyWindow.py

pyside-uic MyWindow.ui -o MyWindow.py