2020-02-12 03:53:07 +00:00
|
|
|
|
---
|
|
|
|
|
filename: learnpyqt.py
|
|
|
|
|
contributors:
|
2020-02-12 03:57:05 +00:00
|
|
|
|
- ["Nathan Hughes", "https://github.com/sirsharpest"]
|
|
|
|
|
translators:
|
2020-02-12 03:53:07 +00:00
|
|
|
|
- ["kdxcxs", "https://github.com/kdxcxs"]
|
2020-02-18 04:16:41 +00:00
|
|
|
|
- ["lsvih", "https://github.com/lsvih"]
|
|
|
|
|
- ["imlonghao", "https://github.com/imlonghao"]
|
2020-02-12 03:53:07 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-02-18 03:36:42 +00:00
|
|
|
|
**Qt** 是一个用 C++ 实现的著名跨平台软件开发框架。只需少量更改有时候甚至不需要更改代码就能在多个软硬件平台上运行,同时拥有原生应用程序的功能和速度。
|
2020-02-12 03:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-02-18 03:58:59 +00:00
|
|
|
|
以下内容改编自 [Aleksey Kholovchuk](https://github.com/vortexxx192) 编写的 C++ 版 QT 简介,并用 pyqt 重构原文代码,实现了部分相同的功能。
|
2020-02-12 03:53:07 +00:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt4 import QtGui
|
2024-04-03 11:13:28 +00:00
|
|
|
|
|
2020-02-12 03:53:07 +00:00
|
|
|
|
def window():
|
2020-02-18 03:04:49 +00:00
|
|
|
|
# 创建应用对象
|
2020-02-12 03:53:07 +00:00
|
|
|
|
app = QtGui.QApplication(sys.argv)
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 创建一个 widget,作为 label 的父控件
|
2020-02-12 03:53:07 +00:00
|
|
|
|
w = QtGui.QWidget()
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 在 widget 中添加一个 label 子控件
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b = QtGui.QLabel(w)
|
2020-02-18 03:53:47 +00:00
|
|
|
|
# 设置 label 的文字
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b.setText("Hello World!")
|
2020-02-18 03:39:31 +00:00
|
|
|
|
# 设置 widget 的尺寸和位置
|
2020-02-12 03:53:07 +00:00
|
|
|
|
w.setGeometry(100, 100, 200, 50)
|
|
|
|
|
b.move(50, 20)
|
2020-02-18 03:04:49 +00:00
|
|
|
|
# 设置窗口的标题
|
2020-02-12 03:53:07 +00:00
|
|
|
|
w.setWindowTitle("PyQt")
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 显示 widget 及其所有子控件
|
2020-02-12 03:53:07 +00:00
|
|
|
|
w.show()
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 下面让程序跑起来,这行代码会启动事件循环并阻塞直到应用程序退出。
|
2020-02-12 03:53:07 +00:00
|
|
|
|
sys.exit(app.exec_())
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
window()
|
|
|
|
|
```
|
|
|
|
|
|
2020-02-18 03:58:59 +00:00
|
|
|
|
为了运用 pyqt 中一些更高级的功能,我们需要开始学习使用其他控件。下文演示了如何弹出对话框,该对话框在用户确认操作或输入信息等情况下经常用到。
|
2020-02-12 03:53:07 +00:00
|
|
|
|
|
2024-04-03 11:13:28 +00:00
|
|
|
|
```python
|
2020-02-12 03:53:07 +00:00
|
|
|
|
import sys
|
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
|
def window():
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
w = QWidget()
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 创建一个按钮并添加到 widget 控件 w
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b = QPushButton(w)
|
|
|
|
|
b.setText("Press me")
|
|
|
|
|
b.move(50, 50)
|
2020-02-18 03:58:59 +00:00
|
|
|
|
# 当按钮 b 被点击时调用 showdialog 函数
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 注意函数调用时没有“()”,这样函数就能以对象的方式传入而非传入执行它所得到的返回值
|
|
|
|
|
# 更多关于 pyqt 函数调用、传参等的内容见 pyqt 的信号机制
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b.clicked.connect(showdialog)
|
|
|
|
|
w.setWindowTitle("PyQt Dialog")
|
|
|
|
|
w.show()
|
|
|
|
|
sys.exit(app.exec_())
|
2024-04-03 11:13:28 +00:00
|
|
|
|
|
2020-02-18 03:36:42 +00:00
|
|
|
|
# 对话框窗口创建函数
|
|
|
|
|
# 当窗口中的按钮被点击时退出本程序
|
2020-02-12 03:53:07 +00:00
|
|
|
|
def showdialog():
|
|
|
|
|
d = QDialog()
|
|
|
|
|
b1 = QPushButton("ok", d)
|
|
|
|
|
b1.move(50, 50)
|
|
|
|
|
d.setWindowTitle("Dialog")
|
2020-02-12 03:57:05 +00:00
|
|
|
|
# 这里的模态实现了在对话框弹出时阻塞程序同时屏蔽父窗口
|
2020-02-12 03:53:07 +00:00
|
|
|
|
d.setWindowModality(Qt.ApplicationModal)
|
2020-02-12 03:57:05 +00:00
|
|
|
|
# 当按钮被点击时整个进程将会结束
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b1.clicked.connect(sys.exit)
|
|
|
|
|
d.exec_()
|
|
|
|
|
if __name__ == '__main__':
|
2020-02-12 03:57:05 +00:00
|
|
|
|
window()
|
2020-02-18 03:39:31 +00:00
|
|
|
|
```
|