亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超7個贊

這樣做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 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號