[PyQt] How to extract the value of a Qspinbox?

Fabien Lafont lafont.fabien at gmail.com
Wed Jan 4 15:26:39 GMT 2012


I'm trying to extract the value of an QDoubleSpinBox but I can't
understand why it does not work. It returns:

levoltage = self.tension.value()
AttributeError: 'ApplicationWindow' object has no attribute 'tension'

Do you have any idea why? I'm just starting to learn PyQt :)

My code:




from visa import *
from pylab import *
import sys
from PyQt4 import QtGui
import numpy as np
import random
import ImageGrab
from PyQt4 import QtCore, QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg
as NavigationToolbar
from PyQt4.QtGui import (QApplication, QLabel, QLineEdit, QSpinBox,
QDoubleSpinBox,
        QVBoxLayout, QDial, QGridLayout, QComboBox, QPushButton)
from PyQt4.QtCore import (QObject, Qt, SIGNAL, SLOT)

class ApplicationWindow(QtGui.QMainWindow):
    """Example main window"""
    def __init__(self):
        global lechemin
        # initialization of Qt MainWindow widget
        QtGui.QMainWindow.__init__(self)
        # set window title
        self.setWindowTitle("QHE manip")
        # instantiate a widget, it will be the main one
        self.main_widget = QtGui.QWidget(self)
        # create a vertical box layout widget
        vbl = QtGui.QVBoxLayout(self.main_widget)

         # instantiate our Matplotlib canvas widget


#===============================================================================

#===============================================================================

        chemin = QLineEdit("C://testfab.jpg")
#        chemin.returnPressed.connect(self.sauvegarde)
        self.connect(chemin, SIGNAL("returnPressed()"),
                     self.sauvegarde)
#        lechemin = unicode(chemin.text())
#===============================================================================

        tension = QDoubleSpinBox()
        tension.valueChanged.connect(self.voltage)
        tension.setRange(0,100)


#===============================================================================

        vbl.addWidget(chemin)
        vbl.addWidget(tension)

        # set the focus on the main widget
        self.main_widget.setFocus()
        # set the central widget of MainWindow to main_widget
        self.setCentralWidget(self.main_widget)

    def sauvegarde(self) :
        global lechemin
        lechemin = unicode(self.chemin.text())

        print lechemin

    def voltage(self) :
        global levoltage
        levoltage = self.tension.value()

qApp = QtGui.QApplication(sys.argv)
# instantiate the ApplicationWindow widget
aw = ApplicationWindow()
# show the widget
aw.show()
# start the Qt main loop execution, exiting from this script
# with the same return code of Qt application
sys.exit(qApp.exec_())


More information about the PyQt mailing list