【问题标题】:Embedding matplotlib figure in QtDesigner GUI在 QtDesigner GUI 中嵌入 matplotlib 图
【发布时间】:2018-06-14 10:12:19
【问题描述】:

我试图通过在使用 Qt Designer 创建的 Qt GUI 中嵌入一个 matplotlib 图来混淆我的方式。我已经能够通过 Python 创建我想要的图形。我创建了一个带有小部件的基本 GUI,以允许我选择/加载输入文件,并将这些文件中的数据绘制在嵌入在 GUI 中的 matplotlib 图形中。为此,我在 GUI 中添加了一个名为 plotwidget 的空白小部件,然后使用该小部件作为输入调用 GraphInit 类。

我目前面临的问题是,虽然我的绘图在 GUI 内根据需要显示和更新,但它似乎并没有注意我的 Python 代码中为它定义的大小策略(在其中创建了 FigureCanvas ) 或 Qt Designer 中的 plotWidget 小部件。我一直使用this demo 等作为这个项目的起点。但是,所有这些示例都完全从 Python 中生成了 GUI。我怀疑我在将 matplotlib 图形分配给小部件时做错了,但我无法弄清楚是什么。

代码(移除了导入和绘图代码,但添加图形的核心在那里):

import sys
import os
import matplotlib
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QMessageBox, QFileDialog, QPushButton, QLabel, QRadioButton, QDoubleSpinBox, QSpinBox, QWidget, QSizePolicy, QMainWindow
import PyQt5.uic

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import json
from datetime import datetime
import numpy as np


class LogViewer(QApplication):
    def __init__(self):
        QApplication.__init__(self, sys.argv)
        self.ui = UI()

    @staticmethod
    def main():
        vwr = LogViewer()
        vwr.run()

    def run(self):
        self.ui.win.show()  # Show the UI
        self.ui.run()  # Execute the UI run script
        self.exec_()


class Graph_init(FigureCanvas):

    def __init__(self, parent=None):

        fig = Figure()
        self.axes = fig.add_subplot(111)
        self.compute_initial_figure()
        self.axes.grid()

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass

class UI(object):
    def __init__(self):
        ui_path = os.path.dirname(os.path.realpath(__file__))
        self.win = PyQt5.uic.loadUi(ui_path + '\\logview.ui')
        self.filename = ui_path + '\\test.txt'
        self.plotWin = Graph_init(self.win.plotWidget)

    def run(self):
        self.init_ui()  # get initial values from the controllers
        self.attach_ui_connections()

    def init_ui(self):
        w = self.win
        w.txtLogFilename.setText(self.filename.split('/')[-1])  # just the file (no path)

    def attach_ui_connections(self):
        w = self.win



if __name__ == '__main__':
    LogViewer.main()

图形用户界面可用here。任何建议表示赞赏!

【问题讨论】:

  • .ui 文件的链接已损坏。在接受的答案中进行更正后,能否发布更新的文件?谢谢。我正在尝试使用这篇文章来学习如何做到这一点。
  • GUI 链接不再有效。请问可以重新发帖吗?我遇到了类似的问题,并希望查看.ui 文件以了解我需要做什么。

标签: python qt matplotlib pyqt5


【解决方案1】:

我检查了你的 ui 文件,plotWidget 根本没有布局。尝试给它一个,我建议一个网格布局。

然后,在父级画布之后

self.setParent(parent)

尝试将其添加到父布局中:

parent.layout().addWidget(self)

【讨论】:

  • 太棒了!非常感谢您的帮助。我已经意识到我需要更早地给小部件一个布局,但是我错过了将画布添加到该布局的关键步骤。
猜你喜欢
  • 2017-10-16
  • 2019-04-08
  • 2019-09-27
  • 2021-12-04
  • 2011-05-03
  • 2018-06-22
  • 1970-01-01
  • 2016-07-13
  • 2017-04-20
相关资源
最近更新 更多