Pyside QPushButton和matplotlib的连接 [英] connection of Pyside QPushButton and matplotlib

查看:51
本文介绍了Pyside QPushButton和matplotlib的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib开发一个非常简单的pyside/Qt程序.我希望在按下按钮时绘制图形.到目前为止,我可以在构造函数上绘制一些东西,但是我无法将 Pyside 事件与 matplotlib 连接起来.有办法吗?

I'm trying to develop a very simple pyside/Qt program using matplotlib. I want that a graph is draw when a button is pressed. So far, I can draw something on the constructor, but I can't connect the Pyside event with matplotlib. Is there a way to do that?

import sys
import platform

import numpy as np
import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit,\
                         QPushButton,  QMessageBox, QWidget, QVBoxLayout
from PySide import QtCore



__version__ = '0.0.1'


import matplotlib

matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'

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


class MainWindow(QMainWindow):


    def __init__(self, parent=None):

        super(MainWindow, self).__init__(parent)

        self.main_frame = QWidget()
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.canvas.setParent( self.main_frame )
        self.axes = self.figure.add_subplot(111)
        vbox = QVBoxLayout( )
        vbox.addWidget( self.canvas )
        self.main_frame.setLayout( vbox )
        self.setCentralWidget( self.main_frame )
        self.button = QPushButton('Run')

    def button_pressed(self):
        data1 = np.loadtxt('FStream.dat')
        data2 = np.loadtxt('FShield.dat')
        self.axes.plot(data1[0],data1[1],data2[0],data2[1])
        print 'pressed'
        self.canvas.draw()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = MainWindow()

    frame.button.clicked.connect(frame.button_pressed)
    frame.button.show()  


    frame.show()
    app.exec_()

非常感谢!

编辑代码以放置draw().

edited the code to put the draw().

编辑 2:分离不同的功能现在看起来像这样:

EDIT 2: Separating in different functions is now looking like this:

import sys
import platform

import numpy as np
import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit,\
                         QPushButton,  QMessageBox, QWidget, QVBoxLayout
from PySide import QtCore



__version__ = '0.0.1'

from ui_pygradient_uni import Ui_MainWindow
import matplotlib
import widgets.matplotlibwidget

matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'

#from matplotlib.figure import Figure
#from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


class MainWindow(QMainWindow):


    def __init__(self, parent=None):

        super(MainWindow, self).__init__(parent)

        self.main_frame = widgets.matplotlibwidget.MatplotlibWidget()

        self.button = QPushButton('Run')

        vbox = QVBoxLayout( )
        vbox.addWidget( self.main_frame.canvas )
        self.main_frame.setLayout( vbox )
        self.setCentralWidget( self.main_frame )

    def button_pressed(self):
        data1 = np.loadtxt('FStream.dat')
        data2 = np.loadtxt('FShield.dat')
        self.axes.plot(data1[0],data1[1],data2[0],data2[1])
        self.canvas.draw()
        print 'pressed'


if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = MainWindow()

    frame.button.clicked.connect(frame.main_frame.Plot)
    frame.button.show()  


    frame.show()
    app.exec_()

而 matplotlibwidget 是这样的:

And the matplotlibwidget is like this:

import matplotlib
import numpy as np


matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'


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



class MatplotlibWidget(FigureCanvas):

    def __init__(self, parent=None):
        super(MatplotlibWidget, self).__init__(Figure())

        self.setParent(parent)
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.axes = self.figure.add_subplot(111)


    def Plot(self):


        data1 = np.loadtxt('FStream.dat')
        data2 = np.loadtxt('FShield.dat')

        self.axes.plot(data1[0],data1[1],data2[0],data2[1])
        self.canvas.draw()

推荐答案

尝试放入

self.canvas.draw()

之后

self.axes.plot(data1[0],data1[1],data2[0],data2[1])

您的GUI代码看起来正常,并且 button_pressed 被正确调用,但是图形未重绘.

Your GUI code looks ok and button_pressed is being called correctly but the graph isn't being redrawn.

这篇关于Pyside QPushButton和matplotlib的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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