在 PyQT 和 Python 2.6 中使用 QTDesigner

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2489643/
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-11-04 00:46:23  来源:igfitidea点击:

Using QTDesigner with PyQT and Python 2.6

pythonqt4pyqt4qt-designer

提问by PyNewbie27

I'm new to Python and am starting to teach myself GUI programming (hopefully) using PyQT4.7 and Python 2.6

我是 Python 新手,开始自学 GUI 编程(希望如此)使用 PyQT4.7 和 Python 2.6

I just downloaded the whole PyQT/QT4 package (including QTDesigner) from the PyQT website, however it seems QTDesigner, which looks amazing to use as a newbie (since you can see all the attributes/properties/defaults etc) of each widget and visually edit the properties is great, but PyQT seems not to set QTDesigner to integrate directly with with PyQT and PyQTs python code generation scripts:

我刚刚从 PyQT 网站下载了整个 PyQT/QT4 包(包括 QTDesigner),但是看起来 QTDesigner,作为新手使用它看起来很棒(因为你可以看到每个小部件的所有属性/属性/默认值等)和视觉效果编辑属性很棒,但 PyQT 似乎没有将 QTDesigner 设置为直接与 PyQT 和 PyQTs python 代码生成脚本集成:

i.e.: Hitting "View Code", tries to run the Designer->C++ script called (uic) instead of the pyuic.py script, etc.

即:点击“查看代码”,尝试运行名为 (uic) 的 Designer->C++ 脚本而不是 pyuic.py 脚本等。

Is there a way to get QTDesigner to integrate closely with PyQT for code generation on the fly like it does with C++?

有没有办法让 QTDesigner 与 PyQT 紧密集成,以便像使用 C++ 那样即时生成代码?

If not, does that mean I have to code the entire QT GUI within my Python IDE and lookup all the documentation and boilerplate code for every widget? (seems very inefficient and inelegant compared to just having QTDesigner+Python integration).

如果不是,这是否意味着我必须在我的 Python IDE 中编写整个 QT GUI 并查找每个小部件的所有文档和样板代码?(与仅具有 QTDesigner+Python 集成相比,似乎非常低效和不优雅)。

What is the customary toolchain//production flow of using Designer with PyQT? (If no direct integration is possible -- do python+pyQT users just skip using QTDesigner all together and manually write all the QT GUI code in python?)

使用 Designer 和 PyQT 的惯用工具链/生产流程是什么?(如果没有直接集成是可能的——python+pyQT 用户是否只是跳过一起使用 QTDesigner 并在 python 中手动编写所有 QT GUI 代码?)

Any additional tips/suggestions for a PyQT newbie would be appreciated. Thanks!

对于 PyQT 新手的任何其他提示/建议将不胜感激。谢谢!

ps I know a bunch of you are probably going to tell me to just suck it up and code all the QT UI by hand, but if I use Designer while learning as asked above, please provide a way to do that so I can learn it more easily, thanks!

ps 我知道你们中的一些人可能会告诉我只是把它搞定并手工编写所有 QT UI,但是如果我在按照上面的要求学习时使用 Designer,请提供一种方法来做到这一点,以便我可以学习它更容易,谢谢!

采纳答案by Maxim Popravko

I started to write my first PyQT application (PyQT is used only to handle GUI), and it seems, that good toolchain is: QtDesigner to generate .ui s and handle resources and some IDE, that can set QtDesigner to edit those. I use Eclipse, cause it is highly customisable. You can compile your .qrc and .ui by demand by doing somewhat like this at application start, or at setup, or any other time:

我开始编写我的第一个 PyQT 应用程序(PyQT 仅用于处理 GUI),看起来,好的工具链是:QtDesigner 生成 .ui 并处理资源和一些 IDE,可以设置 QtDesigner 来编辑它们。我使用 Eclipse,因为它是高度可定制的。您可以通过在应用程序启动、设置或任何其他时间执行以下操作来按需编译 .qrc 和 .ui:

os.system("pyrcc4.exe -o ui/images_rc.py ui/images/images.qrc")
uic.compileUiDir(appDir + '/ui', True)

and then using generated classes this way:

然后以这种方式使用生成的类:

class MyMainWindow(QtGui.QMainWindow):

    def __init__(self, owner):
        QtGui.QMainWindow.__init__(self)
        # 'Ui_MyMainWindow' is the class, that was generated by uic, 
        # naming convention is: 'Ui_' plus the name of root widget in designer
        self.ui = Ui_MyMainWindow()
        self.ui.setupUi(self)

or you can load .ui directly when container inits:

或者您可以在容器初始化时直接加载 .ui:

    QtGui.QMainWindow.__init__(self)
    self.ui = None
    uic.loadUi('MyMainWindowUI.ui', self.ui)
    #now you have the instance of Ui_MyMainWindow in self.ui too, as above

note, that I have added UI suffix to .ui file's name, it was done to avoid name intersection , cause name of .py file, generated by uic, is not class name starting with 'Ui_', but just root widget's one.

请注意,我在 .ui 文件的名称中添加了 UI 后缀,这样做是为了避免名称交叉,因为 uic 生成的 .py 文件的名称不是以“Ui_”开头的类名,而只是根小部件的名称。

And another one note: at the end of generated file uic places 'import %.qrc name%_rc' (by default is import images_rc) string, so you must aware this when using pyrcc4.

还有一点要注意:在生成的文件 uic 的末尾放置了 'import %.qrc name%_rc'(默认情况下是 import images_rc)字符串,因此在使用 pyrcc4 时必须注意这一点。

The whole approach is flexible enough, it takes all dummy ui coding work from you; but you still can do some fine tuning in MyMainWindow.ui attribute, where the instance of Ui_MyMainWindow lays; and it avoids unneeded inheritance. Personally, I make _connectSlots and _initIntefrace methods in MyMainWindow to do some work designer cannot.

整个方法足够灵活,它需要您进行所有虚拟的 ui 编码工作;但是你仍然可以在 Ui_MyMainWindow 的实例所在的 MyMainWindow.ui 属性中做一些微调;它避免了不必要的继承。就我个人而言,我在 MyMainWindow 中制作了 _connectSlots 和 _initIntefrace 方法来做一些设计师不能做的工作。

Still I have to note that writing interface code by yourself is good approach, cause the code, generated by uic, is UGLY. I prefer to load .ui in place by loadUi() because of this :) And if you have a lot of custom PyQT controls, it is so booooring to bring them into QtDesigner..

还是要注意,自己写接口代码是个好办法,因为uic生成的代码很丑。我更喜欢通过 loadUi() 将 .ui 加载到位,因为这样:) 如果你有很多自定义 PyQT 控件,将它们带入 QtDesigner 真是太糟糕了..

回答by WithMetta

What I did was I made my own uic executable that's a wrapper for the pyuic.py script.

我所做的是我制作了自己的 uic 可执行文件,它是 pyuic.py 脚本的包装器。

You'll need...

你需要...

  1. To get and install py2exe

  2. Replace PYTHON_PATH in the uic.py code below

  3. Edit the qt.conf file in the site-packages\PyQt4 and set Binaries=Path to folder containing the uic exe once it's made.

  4. Put uic.py and setup.py in the same folder

  5. In the command prompt navigate to to the folder where setup.py and uic.py are then run the following command "python setup.py py2exe".

  1. 获取并安装py2exe

  2. 替换下面 uic.py 代码中的 PYTHON_PATH

  3. 编辑 site-packages\PyQt4 中的 qt.conf 文件并将 Binaries=Path 设置为包含 uic exe 的文件夹。

  4. 将 uic.py 和 setup.py 放在同一个文件夹中

  5. 在命令提示符中导航到 setup.py 和 uic.py 所在的文件夹,然后运行以下命令“python setup.py py2exe”。

//----uic.py

//----uic.py

#uic.py
import subprocess
import sys

args = ""

for arg in sys.argv: 
    if sys.argv[0] != arg:
        args += arg + " "


# Start pyuic.py script
command = 'python %PYTHON_PATH%\PyQt4\uic\pyuic.py '+ args
out = ''


child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
complete = False
while True:
    out = child.stderr.read(1)
    if out == '' and child.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()

//----- setup.py

//----- setup.py

#setup.py    
from distutils.core import setup
import py2exe

setup(windows=[{"script":"uic.py"}], options={"py2exe" : {"includes" : ["sip", "PyQt4.QtCore"]}})

回答by gruszczy

You can run pyuic(or rather pyuic4) from hand, in your console. You can make your GUI using designer with Python, just as you would do it with C++.

您可以在控制台中手动运行pyuic(或者更确切地说pyuic4)。您可以使用带有 Python 的设计器制作 GUI,就像使用 C++ 一样。

By the way: I have written all GUI stuff for my app (10k of code) by hand. I don't really like GUI designers and working with generated code, when you need to tweak something.

顺便说一句:我已经为我的应用程序(10k 代码)手工编写了所有 GUI 内容。当您需要调整某些内容时,我真的不喜欢 GUI 设计师和使用生成的代码。

回答by sorush-r

QtDesigner uses uic.exeto generate on the fly C++ code. uic.exeis a command-line tool, it gets file names as input parameters. For integration with python, you can write a python program that uses pyuic.py, match it inputs with uic.exe's input (order, switchs etc.), convert it to an executable (using py2exe) and rename the executable to uic.exe. then replace it with uic.exe in Qt's bin directory (uic.exe dont exist in PyQt as I know). You should just be careful about input arguments of uic.exeand your hand-written macro (they should match together).

QtDesigner 用于动态uic.exe生成 C++ 代码。uic.exe是一个命令行工具,它获取文件名作为输入参数。为了与 python 集成,您可以编写一个 python 程序,使用pyuic.py,将其输入与uic.exe的输入(顺序、开关等)匹配,将其转换为可执行文件(使用 py2exe)并将可执行文件重命名为uic.exe. 然后用Qt的bin目录中的uic.exe替换它(据我所知,PyQt中不存在uic.exe)。你应该小心输入参数uic.exe和你的手写宏(它们应该匹配在一起)。

I think your program will need to know python's path (in order to use pyuic). if you are using windows, you could obtain the path from registry.

我认为你的程序需要知道 python 的路径(为了使用 pyuic)。如果您使用的是 Windows,则可以从注册表中获取路径。