Pyqtgraph 的 PlotWidget 无法正确显示
我一直在尝试使用 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 中遵循的步骤是:
- 将 QWidget 添加到主窗口
- 将其提升为 PlotWidget,将头文件设置为 pyqtgraph
- 保存 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:
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:
- Add QWidget to the main window
- Promote it to PlotWidget, setting the header file to pyqtgraph
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@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.
我在使用外接显示器时也遇到了同样的问题,但
之前
解决了这些问题...
I had the same problem with external monitors, but
before
solved them...