龙空技术网

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

下自成蹊kk 87

前言:

今天看官们对“用python绘制饼状图”大致比较着重,各位老铁们都想要分析一些“用python绘制饼状图”的相关知识。那么小编在网摘上搜集了一些有关“用python绘制饼状图””的相关文章,希望看官们能喜欢,看官们一起来了解一下吧!

运行需要安装pyside6。

pip install pyside6 -i 

创建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)

标签: #用python绘制饼状图