为了账号安全,请及时绑定邮箱和手机立即绑定

pyqt QChart 没有显示任何结果

pyqt QChart 没有显示任何结果

白猪掌柜的 2023-10-26 10:50:00
我正在使用 pyqt GUI 应用程序在 postgres 数据库的图表中显示一些结果,但它不起作用这是我使用的代码这就是我创建表格的方式create_table_transaction = ''' CREATE TABLE IF NOT EXISTS transactions (            id SERIAL PRIMARY KEY  UNIQUE NOT NULL,            montant DECIMAL(100,2),             medecin VARCHAR,            date_d DATE,             time_d TIME,            users_id INTEGER,             FOREIGN KEY(users_id) REFERENCES users(id)) '''这是在 Qchart 小部件中绘制图表的功能from PyQt5 import *from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupportfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import Qtfrom PyQt5.QtSql import *from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialogimport sys, sqlite3import psycopg2import datetimefrom datetime import timedeltafrom PyQt5.QtCore import QDateimport sysfrom PyQt5.QtChart import QChart, QLineSeriesfrom PyQt5.QtChart import *from PyQt5.uic import loadUiTypefrom admin import Ui_MainWindow as uiclass 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): #pushButton_113                    self.connection = psycopg2.connect(user="postgres",                                            password="password",                                            host="localhost",                                            database="database")            self.cur = self.connection.cursor()            date = str(self.dateEdit_19.text())             self.cur.execute( '''SELECT medecin, montant FROM transactions WHERE date_d = %s ''',(date,))            rows = self.cur.fetchall()            rightseries = QPieSeries()            for entry in rows:                print(entry)                rightseries.append(entry[0], entry[1])
查看完整描述

1 回答

?
LEATH

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_())


查看完整回答
反对 回复 2023-10-26
  • 1 回答
  • 0 关注
  • 83 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信