Pyqtgraph 的 PlotWidget 无法正确显示

发布于 2025-01-15 14:37:24 字数 2573 浏览 3 评论 0原文

我一直在尝试使用 pyqtgraph 的 PlotWidget 将图形嵌入到我的应用程序中。遵循本教程看起来很简单。我已经设法很好地显示了图表,问题是图表看起来很损坏。这是我可以用来显示问题的最简单应用程序的图像:

有缺陷的图表应用程序

我使用了以下代码:

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>

I've been trying to embed a graph into my application using pyqtgraph's PlotWidget. It seemed simple enough while following this tutorial. I have managed to show a graph well enough, the problem is that the graph looks broken. Here is an image of the most simple app I could make to show the problem:

Bugged graph app

I used the following code:

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

The steps I followed in Qt Designer were:

  1. Add QWidget to the main window
  2. Promote it to PlotWidget, setting the header file to pyqtgraph
  3. Save test.ui file

When I tried embedding the graph into my program, the same bug appeared. That's why I have made this simple example to showcase it.

Some notes about my setup:

  • I'm using windows 11 (perhaps it's this, but I can't test it in another computer).
  • Python version 3.9.7
  • Qt version 5.9.7
  • Pyqtgraph version 0.11.0
  • I'm using Anaconda, and to test this I created a clean environment and installed just the necessary packages.

I would appreciate any help with this.

EDIT

The test.ui file contains the following:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没︽人懂的悲伤 2025-01-22 14:37:25

正如@titusjan 指出的,我确实有两个具有不同缩放系数的显示器。使用相同的缩放系数或仅使用一台显示器运行程序解决了问题。

@titusjan 提供的 问题线程 中还提供了一些解决方法。

As @titusjan pointed out, I do have two monitors with different scaling factors. Running the program with the same scaling factors or with just one monitor fixed the problem.

There also are some workarounds in the issue thread @titusjan provided.

梦旅人picnic 2025-01-22 14:37:25

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

QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)

之前

app = QApplication(sys.argv)

解决了这些问题...

I had the same problem with external monitors, but

QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)

before

app = QApplication(sys.argv)

solved them...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文