pyqt QChart 没有显示任何结果

我正在使用 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, QtPrintSupport

from PyQt5.QtCore import *

from PyQt5.QtWidgets import *

from PyQt5.QtGui import *

from PyQt5.QtCore import Qt

from PyQt5.QtSql import *

from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog

import sys, sqlite3

import psycopg2

import datetime

from datetime import timedelta

from PyQt5.QtCore import QDate


import sys


from PyQt5.QtChart import QChart, QLineSeries

from PyQt5.QtChart import *


from PyQt5.uic import loadUiType

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): #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])



白猪掌柜的
浏览 177回答 1
1回答

LEATH

这样做self.graphicsView = QChartView(rightchart)不会替换 QChartView,但“graphicsView”变量现在指示新的 QChartView,因此您会收到错误。解决办法是在现有的QChartView中设置QChart:import sysfrom PyQt5.QtWidgets import QApplication, QMainWindowfrom PyQt5.QtChart import QPieSeries, QChartimport psycopg2from 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):        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_())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python