2020-02-12 03:53:07 +00:00
|
|
|
|
---
|
|
|
|
|
category: tool
|
|
|
|
|
tool: PyQT
|
|
|
|
|
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-12 03:57:05 +00:00
|
|
|
|
lang: zh-cn
|
2020-02-12 03:53:07 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-02-12 04:03:24 +00:00
|
|
|
|
**Qt** 是一个著名的用C++实现的跨平台软件开发框架。只需更改少量甚至有时候不需要更改代码就能在多个软硬件平台上运行同时拥有原生应用程序的功能和速度。
|
2020-02-12 03:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-02-12 03:57:05 +00:00
|
|
|
|
这是[Aleksey Kholovchuk](https://github.com/vortexxx192)对QT的C++简介的改编,用pyqt实现了一些相同功能的代码!
|
2020-02-12 03:53:07 +00:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt4 import QtGui
|
2020-02-18 03:04:49 +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:04:49 +00:00
|
|
|
|
# 创建一个widget,label将会被放置在里面
|
2020-02-12 03:53:07 +00:00
|
|
|
|
w = QtGui.QWidget()
|
2020-02-18 03:04:49 +00:00
|
|
|
|
# 在widget中添加一个label
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b = QtGui.QLabel(w)
|
2020-02-18 03:04:49 +00:00
|
|
|
|
# 设置label的文字
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b.setText("Hello World!")
|
2020-02-18 03:04:49 +00:00
|
|
|
|
# 设置尺寸和位置
|
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:04:49 +00:00
|
|
|
|
# 将所有东西都显示出来
|
2020-02-12 03:53:07 +00:00
|
|
|
|
w.show()
|
2020-02-18 03:04:49 +00:00
|
|
|
|
# 完成所有设置后,执行我们要求的操作
|
2020-02-12 03:53:07 +00:00
|
|
|
|
sys.exit(app.exec_())
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
window()
|
|
|
|
|
```
|
|
|
|
|
|
2020-02-12 03:57:05 +00:00
|
|
|
|
为了获得pyqt中的一些更高级的功能,我们需要开始研究构建其他元素。在这里,我们展示了如何使用弹出对话框,该对话框对于要求用户确认操作或提供信息很有用。
|
2020-02-12 03:53:07 +00:00
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
|
def window():
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
w = QWidget()
|
2020-02-12 03:57:05 +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-12 03:57:05 +00:00
|
|
|
|
# 当按钮b被点击时调用下面这个函数
|
|
|
|
|
# 注意函数调用时没有“()”,这样函数就能以对象的方式传入而非调用它所得到的返回值
|
|
|
|
|
# 更多关于pyqt函数调用、传参等的内容见pyqt的信号机制
|
2020-02-12 03:53:07 +00:00
|
|
|
|
b.clicked.connect(showdialog)
|
|
|
|
|
w.setWindowTitle("PyQt Dialog")
|
|
|
|
|
w.show()
|
|
|
|
|
sys.exit(app.exec_())
|
2020-02-18 03:04:49 +00:00
|
|
|
|
|
2020-02-12 03:57:05 +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()
|
|
|
|
|
```
|