Convert pyQt UI to python

后端 未结 10 1688
谎友^
谎友^ 2020-11-30 20:51

Is there a way to convert a ui formed with qtDesigner to a python version to use without having an extra file?

I\'m using Maya for this UI, and converting this UI fi

相关标签:
10条回答
  • 2020-11-30 21:10

    I'm not sure if PyQt does have a script like this, but after you install PySide there is a script in pythons script directory "uic.py". You can use this script to convert a .ui file to a .py file:

    python uic.py input.ui -o output.py -x
    
    0 讨论(0)
  • 2020-11-30 21:11

    You don't have to install PyQt4 with all its side features, you just need the PyQt4 package itself. Inside the package you could use the module pyuic.py ("C:\Python27\Lib\site-packages\PyQt4\uic") to convert your Ui file.

    C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py -help

    update python3: use pyuic5 -help # add filepath if needed. pyuic version = 4 or 5.

    You will get all options listed:

    Usage: pyuic4 [options] <ui-file>
    
    Options:
      --version             show program's version number and exit
      -h, --help            show this help message and exit
      -p, --preview         show a preview of the UI instead of generating code
      -o FILE, --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
      -i N, --indent=N      set indent width to N spaces, tab if N is 0 [default:
                            4]
      -w, --pyqt3-wrapper   generate a PyQt v3 style wrapper
    
      Code generation options:
        --from-imports      generate imports relative to '.'
        --resource-suffix=SUFFIX
                            append SUFFIX to the basename of resource files
                            [default: _rc]
    

    So your command will look like this:

    C:\test>python C:\Python27x64\Lib\site-packages\PyQt4\uic\pyuic.py test_dialog.ui -o test.py -x
    

    You could also use full file paths to your file to convert it.

    Why do you want to convert it anyway? I prefer creating widgets in the designer and implement them with via the *.ui file. That makes it much more comfortable to edit it later. You could also write your own widget plugins and load them into the Qt Designer with full access. Having your ui hard coded doesn't makes it very flexible.

    I reuse a lot of my ui's not only in Maya, also for Max, Nuke, etc.. If you have to change something software specific, you should try to inherit the class (with the parented ui file) from a more global point of view and patch or override the methods you have to adjust. That saves a lot of work time. Let me know if you have more questions about it.

    0 讨论(0)
  • 2020-11-30 21:21

    You can use pyuic4 command on shell: pyuic4 input.ui -o output.py

    0 讨论(0)
  • 2020-11-30 21:22

    Update for anyone using PyQt5 with python 3.x:

    1. Open terminal (eg. Powershell, cmd etc.)
    2. cd into the folder with your .ui file.
    3. Type: "C:\python\Lib\site-packages\PyQt5\pyuic5.bat" -x Trial.ui -o trial_gui.py for cases where PyQt5 is not a path variable. The path in quotes " " represents where the pyuic5.bat file is.

    This should work!

    0 讨论(0)
  • 2020-11-30 21:24

    If you are using windows, the PyQt4 folder is not in the path by default, you have to go to it before trying to run it:

    c:\Python27\Lib\site-packages\PyQt4\something> pyuic4.exe full/path/to/input.ui -o full/path/to/output.py
    

    or call it using its full path

    full/path/to/my/files> c:\Python27\Lib\site-packages\PyQt4\something\pyuic4.exe input.ui -o output.py
    
    0 讨论(0)
  • 2020-11-30 21:25

    For Ubuntu it works for following commands; If you want individual files to contain main method to run the files individually, may be for testing purpose,

    pyuic5 filename.ui -o filename.py -x
    

    No main method in file, cannot run individually... try

    pyuic5 filename.ui -o filename.py
    

    Consider, I'm using PyQT5.

    0 讨论(0)
提交回复
热议问题