1 回答
TA贡献1936条经验 获得超6个赞
这样做self.graphicsView = QChartView(rightchart)不会替换 QChartView,但“graphicsView”变量现在指示新的 QChartView,因此您会收到错误。解决办法是在现有的QChartView中设置QChart:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtChart import QPieSeries, QChart
import psycopg2
from admin import Ui_MainWindow as ui
class MainApp(QMainWindow, ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.Handel_Buttons()
def Handel_Buttons(self):
self.pushButton_113.clicked.connect(self.draw_chart01)
def draw_chart01(self):
connection = psycopg2.connect(
user="postgres", password="password", host="localhost", database="database"
)
cur = connection.cursor()
date = str(self.dateEdit_19.text())
cur.execute(
"""SELECT medecin, montant FROM transactions WHERE date_d = %s """, (date,)
)
rows = cur.fetchall()
rightseries = QPieSeries()
for medecin, montant in rows:
rightseries.append(medecin, montant)
rightchart = QChart()
rightchart.addSeries(rightseries)
rightchart.setTitle("title")
rightchart.setAnimationOptions(QChart.SeriesAnimations)
self.graphicsView.setChart(rightchart)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle("Fusion")
window = MainApp()
window.show()
sys.exit(app.exec_())
添加回答
举报