在新窗口中显示 matplotlib 图形,第二个窗口不显示结果 pyqt

我正在使用 picot GUI 应用程序来显示一些图形和图表,我尝试使用 pyqtgraph 来实现,但它不适用于日期或时间,所以我切换到 matplotlib

它工作完美,但结果显示在同一个窗口中,我无法返回主窗口

我正在寻找一种在另一个窗口中显示相同结果的方法,因为我尝试使用像本例这样的小部件来完成此操作https://github.com/swharden/Python-GUI-examples/tree/master/2016-07 -30_qt_matplotlib_sine_scroll,在ui内部但有很多问题

我尝试在第二个窗口 pyqt5 中使用这个例子 matplotlib 它工作完美并且它显示另一个窗口但它是空的

这是我的用户界面

<?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="QPushButton" name="pushButton_113">

    <property name="geometry">

     <rect>

      <x>610</x>

      <y>90</y>

      <width>161</width>

      <height>41</height>

     </rect>

    </property>

    <property name="text">

     <string>Recherche</string>

    </property>

   </widget>

   <widget class="QDateEdit" name="dateEdit_20">

    <property name="geometry">

     <rect>

      <x>340</x>

      <y>90</y>

      <width>251</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 class="QDateEdit" name="dateEdit_19">

    <property name="geometry">

     <rect>

      <x>40</x>

      <y>90</y>

      <width>271</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>

BIG阳
浏览 127回答 1
1回答

一只甜甜圈

我找到了一个在新的单独窗口中显示 matplotlib 图的解决方案import sysfrom PyQt5.QtWidgets import QApplication, QMainWindowfrom pyqtgraph import PlotWidget, plotimport pyqtgraph as pgimport pandas as pdfrom pandas import *from datetime import datetime&nbsp;import matplotlib.pyplot as pltimport matplotlib.dates as mdatesimport matplotlibmatplotlib.use('Qt5Agg')from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbarfrom matplotlib.figure import Figureimport psycopg2from admin import Ui_MainWindow as uiclass MainApp(QMainWindow, ui):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; QMainWindow.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.setupUi(self)&nbsp; &nbsp; &nbsp; &nbsp; self.Handel_Buttons()&nbsp; &nbsp; def Handel_Buttons(self):&nbsp; &nbsp; &nbsp; &nbsp; self.pushButton_113.clicked.connect(self.draw_graph_all)&nbsp; &nbsp; def draw_graph_all(self): #pushButton_113&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.connection = psycopg2.connect(user="postgres",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password="password",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; host="localhost",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; database="database")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.cur = self.connection.cursor()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date_0 = str(self.dateEdit_19.text())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date_1 = str(self.dateEdit_20.text())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.cur.execute( '''SELECT date_d, SUM(montant) FROM transactions WHERE date_d BETWEEN %s AND %s&nbsp; GROUP BY date_d ''', (date_0, date_1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows = self.cur.fetchall()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; montant = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for row in rows:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date.append(row[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; montant.append(row[1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plt.figure()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plt.plot(date, montant)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plt.show(block=False)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python