pyqt QChart未显示任何结果

2024-06-28 11:35:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用PyQtGUI应用程序在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])

            rightchart = QChart()
            rightchart.addSeries(rightseries)
            rightchart.setTitle("title")
            rightchart.setAnimationOptions(QChart.SeriesAnimations)

            self.graphicsView = QChartView(rightchart)
            self.graphicsView.setRenderHint(QPainter.Antialiasing)


if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    window = MainApp()
    window.show()
    sys.exit(app.exec_())

这是我的用户界面

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QChartView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>150</y>
      <width>531</width>
      <height>361</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_113">
    <property name="geometry">
     <rect>
      <x>550</x>
      <y>72</y>
      <width>121</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Recherche</string>
    </property>
   </widget>
   <widget class="QDateEdit" name="dateEdit_19">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>70</y>
      <width>471</width>
      <height>41</height>
     </rect>
    </property>
    <property name="dateTime">
     <datetime>
      <hour>0</hour>
      <minute>0</minute>
      <second>0</second>
      <year>2020</year>
      <month>1</month>
      <day>1</day>
     </datetime>
    </property>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QChartView</class>
   <extends>QGraphicsView</extends>
   <header location="global">PyQt5.QtChart</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

当我运行此功能(draw_chart01)时,它不会显示任何错误,在终端中它只显示如下打印(输入)结果:

('doc01', Decimal('1400.00'))
('doc01', Decimal('14000.00'))
('doc01', Decimal('1500.00'))

在我的ui文件中,我提升了图形视图,就像这个问题的答案一样How to insert QChartView in form with Qt Designer?


Tags: namefromrectimportselfiduidate
1条回答
网友
1楼 · 发布于 2024-06-28 11:35:37

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

相关问题 更多 >