【问题标题】:Pyqtgraph's PlotWidget doesn't show correctlyPyqtgraph 的 PlotWidget 无法正确显示
【发布时间】:2022-07-11 05:06:24
【问题描述】:

我一直在尝试使用 pyqtgraph 的 PlotWidget 将图表嵌入到我的应用程序中。关注this tutorial 时似乎很简单。我已经设法很好地展示了一个图表,问题是图表看起来已经损坏了。这是我可以用来显示问题的最简单应用程序的图像:

Bugged graph app

我使用了以下代码:

from PyQt5.QtWidgets import (QMainWindow, QApplication)
from pyqtgraph import PlotWidget
from PyQt5 import uic
import sys

class UI(QMainWindow):
            
    def __init__(self):
        super(UI, self).__init__()
        
        # Load the ui file
        uic.loadUi("test.ui", self)
        
        self.GraphWidget = self.findChild(PlotWidget,"GraphWidget")

        self.GraphWidget.showGrid(x=True, y=True)
        
        # Show The App
        self.show()

            
            
# Initialize The App
def main():
    app = QApplication(sys.argv)
    UIWindow = UI()
    app.exec_()
      
    
if __name__ == '__main__':
   main()

我在 Qt Designer 中遵循的步骤是:

  1. 将 QWidget 添加到主窗口
  2. 提升为PlotWidget,设置头文件为pyqtgraph
  3. 保存 test.ui 文件

当我尝试将图表嵌入到我的程序中时,出现了同样的错误。这就是为什么我做了这个简单的例子来展示它。

关于我的设置的一些注意事项:

  • 我使用的是 windows 11(可能是这个,但我无法在另一台计算机上测试它)。
  • Python 版本 3.9.7
  • Qt 版本 5.9.7
  • Pyqtgraph 版本 0.11.0
  • 我正在使用 Anaconda,为了对此进行测试,我创建了一个干净的环境并只安装了必要的软件包。

我将不胜感激。

编辑

test.ui 文件包含以下内容:

<?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>480</width>
    <height>419</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="PlotWidget" name="GraphWidget" native="true"/>
    </item>
   </layout>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

  • test.ui 文件中有什么
  • 您是否有两台具有不同比例因子的显示器?可能同PyQtGraph issue #756
  • @titusjan 你是绝对正确的!那就是问题所在。非常感谢您发现问题并将我指向另一个线程。

标签: python plot pyqt pyqtgraph


【解决方案1】:

正如@titusjan 指出的那样,我确实有两台具有不同比例因子的显示器。使用相同的比例因子或仅使用一台显示器运行程序可以解决问题。

issue thread@titusjan 提供的也有一些解决方法。

【讨论】:

    【解决方案2】:

    我在使用外接显示器时遇到了同样的问题,但是

    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    

    之前

    app = QApplication(sys.argv)
    

    解决了它们...

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2018-05-20
      • 2017-12-19
      • 2020-11-20
      • 2019-04-26
      • 2016-12-07
      • 2020-10-19
      • 2014-12-06
      • 2016-12-12
      相关资源
      最近更新 更多