PyQt5 使用 geometry 库绘制图形界面
使用 geometry 库制作界面需要以下步骤:
- 引入 geometry 库
from PyQt5.QtGui import QPolygonF, QBrush, QColor, QPainter
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene
- 创建 QGraphicsScene 和 QGraphicsView 对象
scene = QGraphicsScene()
view = QGraphicsView(scene)
- 创建图形项并添加到场景中
triangle = QPolygonF([QPointF(-50, -50), QPointF(0, 50), QPointF(50, -50)])
brush = QBrush(QColor(255, 0, 0))
triangle_item = scene.addPolygon(triangle, brush)
- 显示界面
view.show()
完整代码示例:
from PyQt5.QtGui import QPolygonF, QBrush, QColor, QPainter
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene
scene = QGraphicsScene()
view = QGraphicsView(scene)
triangle = QPolygonF([QPointF(-50, -50), QPointF(0, 50), QPointF(50, -50)])
brush = QBrush(QColor(255, 0, 0))
triangle_item = scene.addPolygon(triangle, brush)
view.show()
此代码将创建一个包含红色三角形的窗口。您可以使用类似的方法添加其他图形项,例如矩形、椭圆等。
原文地址: https://www.cveoy.top/t/topic/n4iw 著作权归作者所有。请勿转载和采集!