1 回答

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
這樣做self.graphicsView = QChartView(rightchart)不會(huì)替換 QChartView,但“graphicsView”變量現(xiàn)在指示新的 QChartView,因此您會(huì)收到錯(cuò)誤。解決辦法是在現(xiàn)有的QChartView中設(shè)置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_())
添加回答
舉報(bào)