0

I am trying to make a QT interface. I am quite new to qt and python. I used QT creator to make a matplotlib widget (python(x,y)) and a slider, and and I would like it to change with the value of the slider. I tried the different solution I found (here for exemple) and it didn't work. Could someone explain me what I am doing wrong ? Here is my code:

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 
from numpy import *
import os,sys 
os.chdir('C:\\somepath')

from testgraph import * 

class graph(QWidget):     
    def __init__(self, parent=None):         
        QWidget.__init__(self)         
        self.ui=Ui_Form()         
        self.ui.setupUi(parent)         
        #Ici, personnalisez vos widgets si nécessaire         
        #Réalisez les connexions supplémentaires entre signaux et slots 
        self.ui.horizontalSlider.setRange(10, 100)
        self.ui.horizontalSlider.valueChanged.connect(self.plotgraph)
        self.fig = self.ui.mplwidget.axes
        self.fig.set_xlim(1,100)
        self.fig.set_ylabel('plof')
        self.fig.set_xlabel('plif')
        self.fig.set_ylim(-1,1)
        x = arange(1,100)
        self.line = self.fig.plot(x,sin(x*pi/10))
        #self.ui.mplwidget.show()

    def plotgraph(self):         
        T1=self.ui.horizontalSlider.value() 
        print(str(T1))

        self.fig.clear() #clear the previous plot
        self.fig.plot(x,sin(x*pi/T1))
        self.ui.mplwidget.figure.canvas.draw() #redraw the canvas

        #self.ui.mplwidget.figure.canvas.draw()
        #self.ui.mplwidget.figure.canvas.update()
        #self.line.set_ydata(sin(x*pi/T1))
        #self.fig.plot(x,)

        #self.fig.canvas.update()
#        



def main(args):     
    a=QApplication(args)     
    f=QWidget()     
    c=graph(f)     
    f.show()     
    r=a.exec_()     
    return r

if __name__=="__main__":     
    main(sys.argv)#include <QApplication>

and here is the testgraph from qtdesigner:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'testgraph.ui'
#
# Created: Thu Aug 24 16:56:29 2017
#      by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from matplotlibwidget import MatplotlibWidget
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(1181, 837)
        self.verticalLayout = QtGui.QVBoxLayout(Form)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.mplwidget = MatplotlibWidget(Form)
        self.mplwidget.setObjectName(_fromUtf8("mplwidget"))
        self.verticalLayout.addWidget(self.mplwidget)
        self.horizontalSlider = QtGui.QSlider(Form)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName(_fromUtf8("horizontalSlider"))
        self.verticalLayout.addWidget(self.horizontalSlider)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))

The value of the slider is printed, but none of the option I tried worked to refresh my plot. I am stuck here.

denis
  • 5,580
  • 1
  • 13
  • 40

1 Answers1

0

I guess you get an error in the line

self.fig.plot(x,sin(x*pi/T1))

because x is undefined.

Either make x a class variable self.x = arange(1,100) and later

self.fig.plot(self.x,sin(self.x*pi/T1))

or define x inside the updating function

x = arange(1,100)
self.fig.plot(x,sin(x*pi/T1))
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712