有专门针对PyQt5的教程吗?

时间:2021-09-13 23:03:21

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 SIGNAL and SLOT are no more supported in Qt5, it would be nice to have specific tutorials for PyQt5.

到目前为止,我只找到了一些PyQt4教程,而且由于从Qt4到Qt5发生了一些变化,例如,Qt5不再支持事实信号和插槽,所以为PyQt5提供一些特定的教程是很好的。

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

有人能提供关于如何使用PyQt5启动GUI开发的教程吗?

3 个解决方案

#1


63  

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 may not be the panacea it seems.

    QtDesigner:在你的键盘着火的时候,安装包中有一个rockin' GUI-Builder。当您看到它生成的代码(可能只在社区版本中?)时,您就会明白为什么这可能不是万能药。

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

    QML: panacea(万灵药)的另一种候选:声明式GUI构建,使用格式化的JSON。百胜。

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

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

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

    模型-视图框架(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 is dealing 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:

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

  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. 模型~>封装数据。通知视图和控制器对底层数据的任何更改。这将导致对输出或可用命令的显示的更新。
  3. View ~> Displays the relevant output from the Model to the user.
  4. 视图~>将模型的相关输出显示给用户。
  5. Controller ~> Encapsulates user interaction, and notifies the Model and View of relevant events.
  6. Controller ~>封装用户交互,并通知相关事件的模型和视图。

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中的所有内容(包括嵌入的QWidgets等)表示为QGraphicsItem(或其衍生物),包括用于嵌入widget的代理类。这些项目被认为是高度优化的,整合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/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.

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

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:

…在模型中从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的模型视图教程。在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 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.

我刚刚开始使用图形视图,我发现这个关于图形视图框架的教程非常有用。这与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 =:>运行为=:> Python运行中右键单击示例的主模块文件来运行示例

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!

享受吧!

#2


26  

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 scripts under the following path:

并不是真正的教程,而是在以下路径下的非常自我解释的基本脚本:

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

/ python / lib /网站/ PyQt5 /例子

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

您将在30个文件夹中找到大约100个示例,范围从初学者到高级,包括基本的窗口、菜单、选项卡、布局、网络、OpenGL等等。

#3


8  

Have a look at http://www.thehackeruniversity.com/2014/01/23/pyqt5-beginner-tutorial/ This is a newbie friendly tutorial

看看http://www.thehackeruniversity.com/2014/01/23/pyqt5- beginnertutorial/这是一个新手友好的教程吗

#1


63  

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 may not be the panacea it seems.

    QtDesigner:在你的键盘着火的时候,安装包中有一个rockin' GUI-Builder。当您看到它生成的代码(可能只在社区版本中?)时,您就会明白为什么这可能不是万能药。

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

    QML: panacea(万灵药)的另一种候选:声明式GUI构建,使用格式化的JSON。百胜。

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

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

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

    模型-视图框架(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 is dealing 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:

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

  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. 模型~>封装数据。通知视图和控制器对底层数据的任何更改。这将导致对输出或可用命令的显示的更新。
  3. View ~> Displays the relevant output from the Model to the user.
  4. 视图~>将模型的相关输出显示给用户。
  5. Controller ~> Encapsulates user interaction, and notifies the Model and View of relevant events.
  6. Controller ~>封装用户交互,并通知相关事件的模型和视图。

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中的所有内容(包括嵌入的QWidgets等)表示为QGraphicsItem(或其衍生物),包括用于嵌入widget的代理类。这些项目被认为是高度优化的,整合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/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.

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

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:

…在模型中从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的模型视图教程。在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 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.

我刚刚开始使用图形视图,我发现这个关于图形视图框架的教程非常有用。这与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 =:>运行为=:> Python运行中右键单击示例的主模块文件来运行示例

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!

享受吧!

#2


26  

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 scripts under the following path:

并不是真正的教程,而是在以下路径下的非常自我解释的基本脚本:

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

/ python / lib /网站/ PyQt5 /例子

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

您将在30个文件夹中找到大约100个示例,范围从初学者到高级,包括基本的窗口、菜单、选项卡、布局、网络、OpenGL等等。

#3


8  

Have a look at http://www.thehackeruniversity.com/2014/01/23/pyqt5-beginner-tutorial/ This is a newbie friendly tutorial

看看http://www.thehackeruniversity.com/2014/01/23/pyqt5- beginnertutorial/这是一个新手友好的教程吗