19

QtDesigner ファイルから Python コードを生成するにはどうすればよいですか? pyside-uic を見つけましたが、構文の例が見つかりません。スパイダーでwin7とpythonxyを実行しています。

4

8 に答える 8

38

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)

私は通常、次のように使用します。

pyside-uic -o output.py input.ui
于 2011-05-04T11:33:14.387 に答える
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 ファイルを作成しました (モジュールを右クリックし、"New -> Other.." を選択し、"Qt Designer -> Qt Designer Form" を選択します)。明示的な uic 呼び出しは必要ありません。

于 2012-01-03T19:52:36.130 に答える
6
pyside-uic.exe MyWindow.ui -o MyWindow.py 

私がやってきたことであり、それはうまく機能しています(私が知る限り)

于 2011-12-02T21:45:14.263 に答える
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

この方法を使用してUIをロードし、フォームクラスと基本クラスを戻り値の型として取得することもできます...しかし、変換したくない場合は、そうでなければはい、次が正しい方法です。

pyside-uic.exe MyWindow.ui -o MyWindow.py
于 2013-01-07T11:48:41.547 に答える
2

QUiLoader クラスは、中間ファイルを作成せずにジョブを実行します。

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

于 2011-01-14T17:00:02.710 に答える
1

QtUiTools の使用 (別の回答で提案されているように) は、現在 PySide チームによって推奨されていません。

ここで全文を読む: https://groups.google.com/forum/?fromgroups=#!topic/pyside/_s1HPe6XTZs

于 2012-11-09T14:18:30.883 に答える
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.
于 2010-12-14T19:15:56.207 に答える
0

C:\Python27\Lib\site-packages\PySide\scripts\uic.py (または Python がインストールされている場所) を見てください。そのスクリプトを見ると、manページのようにラベルが付けられ、説明されているオプションを見ることができます(Windowsで適切に表示する方法がわかりません。ヒントをいただければ幸いです。)ここhttp://manpages.ubuntu.com/manpages/正確な/man1/pyside-uic.1.html

C:\Python27\Lib\site-packages\pysideuic\pyside-uic.1 を見ようとしてしばらく混乱しました。これは、呼び出されているファイルに違いないと思ったからです。余分な文字がすべてあるため、それをマニュアルページとして表示しようとしても不可能です。どの文字が余分で、どの文字がそうでないかを推測しようとしても、構文を学ぶことはできません!

Windowsでは、もちろん、上記の行(参照用に以下)を含むテキストファイルをuic_generator.batのような.bat拡張子で保存することにより、バッチファイルでこれを自動化できます。

pyside-uic MyWindow.ui -o MyWindow.py

于 2014-11-28T22:51:51.373 に答える