有专门针对 PyQt5 的教程吗? [英] Is there a tutorial specifically for PyQt5?

查看:15
本文介绍了有专门针对 PyQt5 的教程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 PyQt5 教程.在没有教程的情况下,第一次使用 Python 开始 GUI 开发是相当复杂的.

I am looking for a PyQt5 tutorial. It is rather complicated to start GUI development with Python for the first time without a tutorial.

到目前为止,我只找到了一些 PyQt4 教程,并且由于从 Qt4 到 Qt5 发生了一些变化,例如 Qt5 不再支持 SIGNALSLOT 的事实,它要是有 PyQt5 的具体教程就好了.

I only found some PyQt4 tutorials so far, and since something changed from Qt4 to Qt5, for example the fact SIGNAL and SLOT are no more supported in Qt5, it would be nice to have specific tutorials for PyQt5.

有人可以提供一个关于如何使用 PyQt5 开始 GUI 开发的教程吗?

Can someone please provide a tutorial on how to start GUI development with PyQt5?

推荐答案

随着我继续深入 PyQt5 的深度,我是否应该继续用我发现的一些更闪亮的宝藏来更新这个答案.

As my travels into the depths of PyQt5 continue, so shall I continue to update this answer with some of the shinier treasures I find.

话虽如此,我现在正在快速介绍 PyQt5.我还将提供有用资源的链接.我也是这个框架的新手,我将详细说明我认为使用它的好策略,因为我想出了这个策略.可能还有其他好的策略,所以如果有人有什么要补充的,请发表评论.这是一项正在进行中的工作.

That being said, I am now taking a "rough draft" stab at a quick intro to PyQt5. I will also provide links to helpful resources. I am new to this framework as well, and I will elaborate on what I believe to be a good strategy for using it, as I figure that strategy out. There are likely other good strategies, so if anyone has anything to add, then please leave a comment. This is very much a work in progress.

我从另一个答案中建议的示例代码中学到了很多东西,但是这些示例没有帮助的是 PyQt5 的深层魔力.具有很多魔力的框架(PyQt5、Django、SQLAlchemy 等)非常棒,因为大量的苦差事从你身上抽离出来.另一方面,并​​不总是清楚到底发生了什么,或者你应该怎么做.

I've learned much from the example code as suggested in the other answer, but something the examples don't help with is PyQt5's deep magic. Frameworks with a lot of magic in them (PyQt5, Django, SQLAlchemy, ...) are great because an enormous amount of drudgery is abstracted away from you. On the flip side, it is not always clear what the hell is going on, or what you're supposed to do about it.

幸运的是,我们似乎有选择:

Luckily, it seems we have options:

  • QtDesigner:对于那些键盘着火的日子,在安装包中调用了一个摇滚的 GUI-Builder.当您看到它生成的代码时(可能只在社区版本中?),您就会明白为什么这个可能不是它看起来的灵丹妙药.

  • QtDesigner: For those days when your keyboard catches fire, there's a rockin' GUI-Builder called in the installation package. When you see the code this produces (perhaps only in the community version?), you'll see why this may not be the panacea it seems.

QML:另一种灵丹妙药:从格式化的 JSON 构建声明式 GUI.嗯.

QML: Another candidate for panacea: declarative GUI building from formatted JSON. Yum.

Qt Quick:QML 框架.在这一点上,它可能看起来非常容易,但不要被这些东西所吸引.它似乎总是归结为手工学习.

Qt Quick: The framework for QML. By this point, it may seem tantalizingly easy, but don't get sucked in by this stuff just yet. It always seems to come down to learning it by hand.

模型-视图框架(1):模型视图(不是 MVC)将处理表示/交互的代码与管理数据的代码分开,目的是提供模块化.

The Model-View Framework(1): Model-View (not MVC) separates the code that deals with presentation/interaction from the code that manages the data, with the aim of providing modularity.

PyQt5 中的编码通过使用实现模型-视图设计模式的一组类大大简化.Model-View 是 Model-View-Controller (MVC) 的演进,其中 Controller 与 View 重新统一.他们看起来像是奇怪的伙伴,但是,程序的大部分逻辑处理用户或数据:这似乎有一定的意义,至少在平流层层面.

Coding in PyQt5 is greatly simplified by using the set of classes that implement the Model-View design pattern. Model-View is an evolution of Model-View-Controller (MVC), in which the Controller has been reunited with the View. They seem like strange bedfellows, but, most of the program's logic is dealing with either the user, or data: it seems to make a certain sense, at least at a stratospheric level.

鸟瞰:

模型-视图-控制器

这种广泛使用的设计模式将应用程序分为 3 层:

This widely-used design pattern separates the application into 3 layers:

  1. 模型 ~> 封装数据.通知 View 和 Controller 对基础数据的任何更改.这会分别更新输出或可用命令的显示.
  2. 查看 ~> 将模型的相关输出显示给用户.
  3. 控制器 ~> 封装用户交互,并通知模型和视图相关事件.

模型-视图

  • 图形视图框架(1) ~> 表示QGraphicsScene 中的所有内容(包括嵌入的 QWidget 等)作为 QGraphicsItem(或其派生项),包括用于嵌入小部件的代理类.这些项目据说是高度优化的,并且集成 OpenGL 支持是单行的,这很好.

这种设计模式将控制器放在视图中.这样,视图就能够处理整个用户交互.具体来说,就是信号和槽机制.

This design pattern puts the Controller inside the View. This way, the view is capable of handling the entirety of the user's interaction. In concrete terms, these are the Signals and Slots mechanisms.

回调

信号和槽

..... ** 对不起,我现在必须签字.我会回来继续添加这个.**

..... ** I'm sorry, but I must sign off now. I'll be back to continue to add to this. **

例如,您可以从 itemviews/editabletreemodel 示例中获取树视图,然后从 中换入文件系统模型 (QFileSystemModel)itemviews/dirview 示例,您已经获得了目录树的完整(工作)视图.相当时髦.

Like, for instance, you can take a tree view from the itemviews/editabletreemodel example, then swap in a file system model (QFileSystemModel) from the itemviews/dirview example and you've got a full (working) view of your directory tree. Pretty snazzy.

因此,您将从可编辑树模型示例中获取代码:

So, you would take the code from the editabletreemodel example:

headers = ("Title", "Description")

file = QFile(':/default.txt')
file.open(QIODevice.ReadOnly)
model = TreeModel(headers, file.readAll())
file.close()

self.view.setModel(model)

...并从 dirview 中交换模型:

...and swap in the model from dirview:

model = QFileSystemModel()
model.setRootPath('')
self.view.setModel(model)

...它只是工作.惊人.

...and it just works. Amazing.

下一步(就我而言)(*我认为)是实现一个自定义模型,然后我将同时使用多个视图,但我不知道这是否适合您的用例.

The next step (in my case) (*I think) is implementing a custom model which I will then use several views concurrently, but I don't know if that kinda thing fits your use case.

这是我在旅行中发现的一些宝石.希望他们对您有所帮助.

Here are some gems I found on my travels. Hopefully they help you on yours.

这是 Qt5 的 Model-View 教程.(1) 这是来自官方 Qt5 文档的非常详细的文档.在 Qt5 站点上可以找到大量有用的文档.请记住,它是针对 Qt5(C++ 库)的,但是读起来差别不大(无论如何 PyQt5 官方文档都指向那里).

This is a tutorial on Model-View for Qt5.(1) It is a very detailed document from the official Qt5 docs. A good deal of useful documentation can be found at the Qt5 site. Keep in mind, it's for Qt5 (the C++ library), but the difference is trivial to read through (and the PyQt5 official docs point there anyway).

此 PDF 包含 PyQt4 模型视图框架的快速高级. 请注意,它适用于 PyQt4(不是 PyQt5),但它实际上适用于 Python(而不是 C++),我发现它很快教会了我很多东西.

This PDF contains a quick high-level to PyQt4's Model-View framework. Note that is it for PyQt4 (not PyQt5), but it is actually for Python (as opposed to C++), and I found it very quickly taught me a lot.

我刚刚开始使用 Graphics View,并且正在寻找 this tutorial on the Graphics View Framework 很有帮助.这与 qtdemo 示例代码中用于生成一些流畅效果的 View 相同.我稍后会更新这个.

I am just starting to play with the Graphics View, and am finding this tutorial on the Graphics View Framework very helpful. This is the same View that is used in the qtdemo example code to generate some slick effects. I'll be updating this in a bit.

这是所有 Qt5 模块的完整列表.

这是所有 Qt5 类的完整列表.

这是 Qt5 API 中所有函数的完整列表.

正如 katsh 在另一个答案的评论中指出的那样,这里是 GitHub 上 PyQt5.2.1 示例代码的链接

As katsh pointed out in another answer's comments, here is a link to the example code for PyQt5.2.1 on GitHub

此外,示例代码的副本随您的发行版一起提供,可以在以下位置找到:

Additionally, a copy of the example code comes packaged with your distribution and can be found at:

%PYTHON_HOME%Libsite-packagesPyQt5examples

如果您使用的是 PyDev (Eclipse),您只需在 PyDev Package Explorer 或 Navigator 中右键单击示例的主模块文件即可运行示例 =:> Run As =:> Python Run

If you're using PyDev (Eclipse), you can run examples by simply right-clicking an example's main module file in PyDev Package Explorer or Navigator =:> Run As =:> Python Run

在我(不是那么)拙见中,最好的一个是:

The best one, in my (not so) humble opinion, is:

%PYTHON_HOME%Libsite-packagesPyQt5examplesqtdemoqtdemo.py

在我当前的项目中,我正在对这个示例进行逆向工程.如果你检查一下,你就会明白为什么.待续.. ;)

Among my current projects, I'm in the process of reverse engineering this example. If you check it out, you'll see why. To be continued.. ;)

享受吧!

这篇关于有专门针对 PyQt5 的教程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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