Python 是否有专门针对 PyQt5 的教程?

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

Is there a tutorial specifically for PyQt5?

pythonuser-interfaceqt5pyqt5

提问by WeGi

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

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

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

到目前为止,我只找到了一些 PyQt4 教程,并且由于从 Qt4 到 Qt5 发生了一些变化,例如事实SIGNAL并且SLOT在 Qt5 中不再受支持,因此最好有 PyQt5 的特定教程。

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

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

采纳答案by Inversus

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 进行快速介绍。我还将提供有用资源的链接。我也是这个框架的新手,我将详细说明我认为使用它的好策略,因为我想出了这个策略。可能还有其他好的策略,因此如果有人要添加任何内容,请发表评论。这是一项正在进行的工作。



Strategy

战略

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.

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

Luckily, it seems we have options:

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

  • 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 maynot be the panacea it seems.

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

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

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

  • QtDesigner:对于那些键盘着火的日子,安装包中有一个摇滚的 GUI-Builder。当您看到由此产生的代码时(也许仅在社区版本中?),您会明白为什么这可能不是看起来的灵丹妙药。

  • QML:另一个万能药的候选者:从格式化的 JSON 构建声明式 GUI。嗯。

  • Qt Quick:QML 框架。到目前为止,它可能看起来非常容易,但不要被这些东西所吸引。它似乎总是归结为手工学习。

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

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 isdealing with either the user, or data: it seems to make a certain sense, at least at a stratospheric level.

通过使用实现模型-视图设计模式的一组类,大大简化了 PyQt5 中的编码。模型-视图是模型-视图-控制器(MVC)的演变,其中控制器与视图重新统一。他们看起来像是奇怪的同床异梦,但是,程序的大部分逻辑要么处理用户,要么是数据:这似乎有一定的意义,至少在平流层的层面上是这样。

From a bird's eye:

从鸟瞰:

Architecture(s)

架构

Model-View-Controller

模型-视图-控制器

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

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

  1. Model~> Encapsulates the data. Notifies View and Controller of any changes to the underlying data. This causes updates to the display of output or available commands, respectively.
  2. View~> Displays the relevant output from the Model to the user.
  3. Controller~> Encapsulates user interaction, and notifies the Model and View of relevant events.
  1. 模型~> 封装数据。通知 View 和 Controller 对基础数据的任何更改。这会分别导致输出或可用命令的显示更新。
  2. 查看~> 将模型的相关输出显示给用户。
  3. Controller~> 封装用户交互,将相关事件通知Model和View。

Model-View

模型视图

  • The Graphics View Framework(1)~> Represent everything (including embedded QWidgets, etc) inside a QGraphicsScene as a QGraphicsItem (or derivative thereof), including proxy classes for embedding widgets. The items are supposedly highly optimized, and integrating OpenGL support is a one-liner, which is nice.
  • 图形视图框架(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.

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

User Interaction Management

用户交互管理

Callbacks

回调

Signals and Slots

信号和槽

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

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

Practical Example(s)

实际例子

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

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

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

因此,您将从 editabletreemodel 示例中获取代码:

headers = ("Title", "Description")

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

self.view.setModel(model)

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

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

Resources

资源

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

这是我在旅行中发现的一些宝石。希望他们能帮助你。

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

这是一个关于 Qt5 模型视图的教程。(1)这是来自官方 Qt5 文档的非常详细的文档。在 Qt5 站点上可以找到大量有用的文档。请记住,它适用于 Qt5(C++ 库),但通读的区别是微不足道的(无论如何 PyQt5 官方文档都指向那里)。

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.

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

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

我刚刚开始使用图形视图,我发现这个关于图形视图框架的教程非常有帮助。这与qtdemo示例代码中用于生成一些光滑效果的视图相同。稍后我会更新这个。

This is a complete list of all of the Qt5 Modules.

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

This is a complete list of all of the Qt5 Classes.

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

This is a complete list of all functions in the Qt5 API.

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

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

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

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

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

%PYTHON_HOME%\Lib\site-packages\PyQt5\examples

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

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

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

在我看来(并非如此),最好的一个是:

%PYTHON_HOME%\Lib\site-packages\PyQt5\examples\qtdemo\qtdemo.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.. ;)

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

Enjoy!

享受!

回答by user1801060

回答by Pete

Been looking for PyQt5 tutorials for some time? Look no further! You won't find many around the internet.

一直在寻找 PyQt5 教程?别再看了!你不会在互联网上找到很多。

Not really tutorials, but pretty self-explanatory basic scriptsunder the following path:

不是真正的教程,而是以下路径下非常不言自明的基本脚本

/python/lib/site-packages/PyQt5/examples

/python/lib/site-packages/PyQt5/examples

you will find about 100examples in 30folders ranging from beginner to advanced, covering basic windows, menus, tabs, layouts, network, OpenGL, etc.

您会10030从初级到高级的文件夹中找到有关示例,涵盖基本窗口、菜单、选项卡、布局、网络、OpenGL 等。