Search code examples
pythonpyqt6

PyQt6-Charts: Donut Chart (aka circle graph) crashes on animation


This is my minimum reproducible example of a donut chart (aka circle graph)

I am trying to animate it but my code crashes on line 24 with error:

chart.setAnimationOptions(chart.SeriesAnimations) AttributeError: 'QChart' object has no attribute 'SeriesAnimations'

from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt6.QtCharts import QChart, QChartView, QPieSeries
from PyQt6.QtGui import QPainter
from PyQt6.QtCore import Qt

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setWindowTitle("Temperature Chart Example")
        self.setMinimumSize(800, 600)

        series = QPieSeries()
        # Current temperature
        series.append("Current Temperature", 30)
        # Remaining temperature
        series.append("Remaining Temperature", 70)

        # Set the hole size
        series.setHoleSize(0.6)

        chart = QChart()
        chart.addSeries(series)
        chart.setTitle("Current Temperature")
        chart.setAnimationOptions(chart.SeriesAnimations)

        chart.legend().setVisible(True)

        chart_view = QChartView(chart)
        chart_view.setRenderHint(QPainter.RenderHint.Antialiasing)

        # Create a layout to hold both the chart and the label
        layout = QVBoxLayout()
        layout.addWidget(chart_view)
        layout.addWidget(self.label)

        # Create a central widget to hold the layout
        central_widget = QWidget()
        central_widget.setLayout(layout)

        # Set the widget as the central widget of the window
        self.setCentralWidget(central_widget)


app = QApplication([])
window = MainWindow()
window.show()
app.exec()

Solution

  • SeriesAnimations is an enum value from QChart. What matters here is that it is located within AnimationOption.

    In order to use the values of this enum, you have to explicitly indicate where it comes from. Consequently, your line must either change to :

    chart.setAnimationOptions(chart.AnimationOption.SeriesAnimations)

    or

    chart.setAnimationOptions(QChart.AnimationOption.SeriesAnimations)