《开始使用PyQT》 第01章 PyQT入门 01 PyQT框架概述
The PyQt toolkit is a set of Python bindings for the Qt cross-platform widget toolkit and application framework. What does that mean?
PyQt 工具包是 Qt 跨平台 widget 工具包和应用程序框架的一套 Python 绑定。这意味着什么?
First, Qt is used for the development of user interfaces and other applications and is being developed by The Qt Company. The framework is significant because it can run on numerous software and hardware systems such as Windows, macOS, Linux, Android, or embedded systems with little to no change to the underlying code and is still able to maintain capabilities and speed of the system on which it is being run. So in general, you’ll be able to make some amazing cross-platform applications without having to worry about the user’s platform.
首先,Qt 用于开发用户界面和其他应用程序,由 Qt 公司开发。该框架之所以重要,是因为它可以在 Windows、macOS、Linux、Android 或嵌入式系统等众多软件和硬件系统上运行,几乎不需要修改底层代码,而且仍能保持运行系统的功能和速度。因此,总的来说,您可以制作一些令人惊叹的跨平台应用程序,而不必担心用户的平台。
Second, this all means that PyQt combines all the advantages of the Qt C++cross-platform widget toolkit with Python, the powerful and simple, cross-platform interpreted language. It’s worth noting that while PyQt has its own documentation, the Qt documentation tends to be more complete. If you have never used C++ before, this can seem like a daunting task. We’ll discuss this topic a little further in this chapter’s first application.
其次,这意味着 PyQt 将 Qt C++ 跨平台 widget 工具包的所有优点与 Python(一种强大而简单的跨平台解释型语言)结合在一起。值得注意的是,虽然 PyQt 有自己的文档,但 Qt 文档往往更完整。如果您以前从未使用过 C++,这似乎是一项艰巨的任务。我们将在本章的第一个应用程序中进一步讨论这个话题。
For more information about Qt, have a look at www.qt.io.
有关 Qt 的更多信息,请访问 www.qt.io。
For more information about PyQt, check out www.riverbankcomputing.com/news.
有关 PyQt 的更多信息,请访问 www.riverbankcomputing.com/news。
PyQt is capable of more than just creating graphical interfaces, as it also has access to Qt classes that cover mechanics such as XML handling, SQL databases, network communication, graphics and animations, and many other technologies. Take the capabilities of Qt and combine them with the number of extension modules that Python provides and you have the ability to create new applications that can build upon these preexisting libraries.
PyQt 不仅能创建图形界面,还能访问 Qt 类,这些类涵盖了 XML 处理、SQL 数据库、网络通信、图形和动画等机制以及许多其他技术。将 Qt 的功能与 Python 提供的大量扩展模块相结合,您就能在这些已有库的基础上创建新的应用程序。
PyQt also includes Qt Designer, which allows for anyone to assemble a GUI much faster using a simple drag-and-drop graphical interface designer. Qt Designer is discussed in detail in Chapter 8.
PyQt 还包括 Qt Designer,它允许任何人使用简单的拖放图形界面设计器更快地组装图形用户界面。第 8 章将详细讨论 Qt Designer。
Using PyQt’s signal and slot mechanism, you can create an extremely interactive interface and customize how different PyQt components interact. This will be covered in more detail in Chapters 3 and 7.
使用 PyQt 的信号和槽机制,您可以创建一个交互性极强的界面,并自定义不同 PyQt 组件的交互方式。这将在第 3 章和第 7 章中详细介绍。
There are, of course, other toolkits available for creating applications with GUIs using Python, such as Tkinter, wxPython, and PySide, and they all have their own advantages. For example, Tkinter comes bundled with Python, meaning that you can find an abundance of helpful resources by doing a quick search on the Internet. PySide is the Python binding of Qt managed by The Qt Company itself. Despite this, PyQt still has a larger following thanks to its age and awesome community of developers.
当然,还有其他工具包可用于使用 Python 创建图形用户界面应用程序,如 Tkinter、wxPython 和 PySide,它们都有各自的优势。例如,Tkinter 与 Python 绑定,这意味着只要在互联网上快速搜索,就能找到大量有用的资源。PySide 是 Qt 公司自己管理的 Qt Python 绑定。尽管如此,PyQt 凭借其悠久的历史和令人敬畏的开发者社区,仍然拥有更多的追随者。
It is worth noting that if you choose to use PyQt to create commercial applications, you will need to get a license. Applications created in this book will leverage the GNU General Public License (GPL).
值得注意的是,如果您选择使用 PyQt 创建商业应用程序,则需要获得许可证。在本书中创建的应用程序将使用 GNU 通用公共许可证 (GPL)。
Ultimately, it all comes down to choosing the toolkit that works the best for your project.
归根结底,还是要选择最适合自己项目的工具包。
The latest version of Qt has brought more focus on 2D and 3D capabilities. There are also quite a few changes in the latest version of PyQt. Since this book’s aim is to introduce the fundamentals of developing applications with PyQt, we will take a look at the changes that will impact you the most at this stage of your journey:
最新版本的 Qt 更加注重 2D 和 3D 功能。最新版本的 PyQt 也有不少变化。由于本书的目的是介绍使用 PyQt 开发应用程序的基础知识,因此我们将介绍现阶段对您影响最大的变化:
As you dive more into PyQt, you’ll definitely use enums, flags, andother identifiers in the Qt Namespace. They are useful for settingthe properties of the different classes in PyQt. What’s importantto understand now is that the fully qualified names are now usedfor these items rather than the shorthand ones used in PyQt5. Forexample, PyQt5 used Qt.AlignCenter to center text, but PyQt6 usesthe full name, Qt.AlignmentFlag.Center. You will see numerousexamples of this throughout the book, and we’ll discuss this more inlater chapters.
随着对 PyQt 的深入研究,您肯定会用到 Qt 命名空间中的枚举、标志和其他标识符。它们对于设置 PyQt 中不同类的属性非常有用。现在需要了解的是,这些项目现在使用的是完全限定的名称,而不是 PyQt5 中使用的速记名称。例如,PyQt5 使用 Qt.AlignCenter 使文本居中,但 PyQt6 使用了全名 Qt.AlignmentFlag.Center。您将在本书中看到许多这样的例子,我们将在后面的章节中详细讨论。
Some classes and methods have been deprecated, such asQDesktopWidget, while others have been moved, such as the QActionclass is now located in the QtGui module.
一些类和方法已被弃用,如 QDesktopWidget,而另一些则被移动,如 QAction 类现在位于 QtGui 模块中。
The exec() method is now used in PyQt6 to start the event loop ofyour application rather than exec_().
在 PyQt6 中,exec() 方法现在用来启动应用程序的事件循环,而不是 exec_()。
This is not an exhaustive list, and you will find out more about these and other topics as you follow along. If you are interested to see more of the new changes in PyQt6, have a look at www.riverbankcomputing.com/static/Docs/PyQt6/pyqt5_differences.html.
这并不是一个详尽的列表,您将在学习过程中发现更多关于这些主题和其他主题的信息。如果您有兴趣了解 PyQt6 的更多新变化,请访问 www.riverbankcomputing.com/static/Docs/PyQt6/pyqt5_differences.html。