pyqt5+QWebEngine窗口图形无法显示解决方案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt5+QWebEngine窗口图形无法显示解决方案相关的知识,希望对你有一定的参考价值。

参考技术A 附上网页: https://doc.qt.io/qt-5/qtwebengine-platform-notes.html

解决方法:
1 系统高级设置-添加环境变量 QTWEBENGINE_DISABLE_SANDBOX = 1.
2 声明窗口程序时,输入参数添加"--no-sandbox",例如:
app = QApplication(sys.argv+["--no-sandbox"])

解决办法:
https://blog.csdn.net/euler1983/article/details/120031945

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

【中文标题】在新窗口中显示 matplotlib 图,第二个窗口不显示结果 pyqt【英文标题】:show matplotlib graph in a new window , second window dont show result pyqt 【发布时间】:2020-10-21 16:18:08 【问题描述】:

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

效果很好,但结果显示在同一个窗口中,我无法返回主窗口

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

我试试这个例子matplotlib in second window pyqt5 它工作得很好,它显示了另一个窗口,但它是空的

这是我的用户界面

<?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>
    <property name="calendarPopup">
     <bool>true</bool>
    </property>
   </widget>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

这是运行 matplotlib 图形的函数

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import pandas as pd
from pandas import *
from datetime import datetime 

import matplotlib.pyplot as plt

import matplotlib.dates as mdates
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import psycopg2

from admin import Ui_MainWindow as ui

class MplCanvas(FigureCanvasQTAgg):
    
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)

class SecondWindow(QMainWindow):
    def __init__(self):
         super(SecondWindow, self).__init__()
         self.main_widget = QtWidgets.QWidget()
         self.setCentralWidget(self.main_widget)

         layout = QtWidgets.QVBoxLayout(self.main_widget)
         sc = MplCanvas(self.main_widget, width = 300, height = 300)
         layout.addWidget(sc)

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_graph_all)

    def draw_graph_all(self): #pushButton_113
            self.connection = psycopg2.connect(user="postgres",
                                            password="password",
                                            host="localhost",
                                            database="database")
            self.cur = self.connection.cursor()

            date_0 = str(self.dateEdit_19.text())
            date_1 = str(self.dateEdit_20.text())


            self.cur.execute( '''SELECT date_d, SUM(montant) FROM transactions WHERE date_d BETWEEN %s AND %s  GROUP BY date_d ''', (date_0, date_1))
            rows = self.cur.fetchall()

            date = []
            montant = []

            for row in rows:
                date.append(row[0])
                montant.append(row[1])

            self.SW = SecondWindow()
            sc = MplCanvas(self, width=5, height=4, dpi=100)
            sc.axes.plot(date, montant)
        
            self.SW.resize(300,300)
            self.SW.show()

【问题讨论】:

【参考方案1】:

我找到了在新的分隔窗口中显示 matplotlib 图形的解决方案

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import pandas as pd
from pandas import *
from datetime import datetime 

import matplotlib.pyplot as plt

import matplotlib.dates as mdates
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

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_graph_all)

    def draw_graph_all(self): #pushButton_113
            self.connection = psycopg2.connect(user="postgres",
                                            password="password",
                                            host="localhost",
                                            database="database")
            self.cur = self.connection.cursor()

            date_0 = str(self.dateEdit_19.text())
            date_1 = str(self.dateEdit_20.text())


            self.cur.execute( '''SELECT date_d, SUM(montant) FROM transactions WHERE date_d BETWEEN %s AND %s  GROUP BY date_d ''', (date_0, date_1))
            rows = self.cur.fetchall()

            date = []
            montant = []

            for row in rows:
                date.append(row[0])
                montant.append(row[1])

            plt.figure()
            plt.plot(date, montant)
            plt.show(block=False)
            

【讨论】:

以上是关于pyqt5+QWebEngine窗口图形无法显示解决方案的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 图形界面 - 配置界面跟随窗口大小调整灵活伸缩,设置页面控件居中显示实例演示

PyQt5编程(9):窗口在屏幕的位置与管理

使用QWebEngine在同一窗口中打开任何链接(甚至_blank)

当我在 PyQt5 窗口中嵌入 Matplotlib 图形时,为啥有两个重复的轴标签?

主窗口前的PyQt5启动画面[重复]

我的绘图未显示在我的 pyqt5.QtWidgets