python绘制饼状图qml显示示例1

发布一下 0 0
python绘制饼状图qml显示示例1

运行需要安装pyside6。

pip install pyside6 -i https://pypi.douban.com/simple/

创建app.qml界面文件,代码如下:

import Chartsimport QtQuickItem {    width: 300; height: 200    PieChart {        id: aPieChart        anchors.centerIn: parent        width: 100; height: 100        name: "饼图"        color: "green"    }    Text {        anchors {            bottom: parent.bottom;            horizontalCenter: parent.horizontalCenter;            bottomMargin: 20        }        text: aPieChart.name    }}

创建main.py文件,python代码如下:

import osfrom pathlib import Pathimport sysfrom PySide6.QtCore import Property, Signal, QUrlfrom PySide6.QtGui import QGuiApplication, QPen, QPainter, QColorfrom PySide6.QtQml import qmlRegisterTypefrom PySide6.QtQuick import QQuickPaintedItem, QQuickViewclass PieChart (QQuickPaintedItem):#定义PieChart类    nameChanged = Signal()#定义信号    def __init__(self, parent=None):        QQuickPaintedItem.__init__(self, parent)        self._name = u''        self._color = QColor()    def paint(self, painter):#绘制图形        pen = QPen(self.color, 2)        painter.setPen(pen)        painter.setRenderHints(QPainter.Antialiasing, True)        painter.drawPie(self.boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16)    @Property(QColor)    def color(self):        return self._color    @color.setter    def color(self, value):        self._color = value    @Property(str, notify=nameChanged)    def name(self):        return self._name    @name.setter    def name(self, value):        self._name = valueif __name__ == '__main__':    app = QGuiApplication(sys.argv)    qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart')#注册PieChart类使qml文件可以使用    view = QQuickView()    view.setResizeMode(QQuickView.SizeRootObjectToView)    qml_file = os.fspath(Path(__file__).resolve().parent / 'app.qml')    view.setSource(QUrl.fromLocalFile(qml_file))    if view.status() == QQuickView.Error:        sys.exit(-1)    view.show()    res = app.exec()    del view    sys.exit(res)

版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

本文地址:http://0561fc.cn/69884.html