在 PyQT 和 Python 2.6 中使用 QTDesigner [英] Using QTDesigner with PyQT and Python 2.6

查看:49
本文介绍了在 PyQT 和 Python 2.6 中使用 QTDesigner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

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

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

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

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

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

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).

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

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?)

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

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

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

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!

推荐答案

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

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:

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)

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

or you can load .ui directly when container inits:

    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

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

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.

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

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.

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

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.

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

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..

这篇关于在 PyQT 和 Python 2.6 中使用 QTDesigner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆