在QtDesigner GUI中嵌入matplotlib图 [英] Embedding matplotlib figure in QtDesigner GUI

查看:56
本文介绍了在QtDesigner GUI中嵌入matplotlib图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to muddle my way through embedding a matplotlib figure inside of a Qt GUI created using Qt Designer. I am already able to create the figure I want just through Python. I've created a basic GUI with widgets to allow me to select/load input files, and plot the data in those files in a matplotlib figure that is embedded in the GUI. I accomplish this by adding a blank widget to the GUI called plotwidget, and then calling the GraphInit class with this widget as input.

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

The problem I am currently facing is that while my plot shows up and updates as desired inside the GUI, it doesn't seem to pay attention to the size policy defined for it in either my Python code (where the FigureCanvas is created) or for the plotWidget widget in Qt Designer. I have been using this demo, among others, as a starting point for this project. However, all of these examples have generated the GUI entirely from within Python. I suspect I am doing something wrong in assigning the matplotlib figure to the widget, but I haven't been able to figure out what.

代码(已删除导入和绘图代码,但已存在添加图形的核心):

Code (import & plotting code removed, but the core where figure is added is there):

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

GUI 可在此处使用.任何建议表示赞赏!

GUI is available here. Any suggestions appreciated!

推荐答案

我检查了您的ui文件,并且 plotWidget 根本没有布局.尝试给它一个,我建议采用网格布局.

I checked your ui file and plotWidget has no layout at all. Try to give it one, I suggest a grid layout.

然后,在为画布做父母之后

Then, after parenting the canvas

self.setParent(parent)

尝试将其添加到父版式:

try adding it to the parent layout:

parent.layout().addWidget(self)

这篇关于在QtDesigner GUI中嵌入matplotlib图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆