問題描述
我正在尋找 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 不再支持 SIGNAL
和 SLOT
的事實,它要是有 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:
- 模型 ~> 封裝數據.通知 View 和 Controller 對基礎數據的任何更改.這會分別更新輸出或可用命令的顯示.
- 查看 ~> 將模型的相關輸出顯示給用戶.
- 控制器 ~> 封裝用戶交互,并通知模型和視圖相關事件.
模型-視圖
- 圖形視圖框架(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 的教程嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!