Ceci est une ancienne révision du document !
radioButton_1 = QtGui.QRadioButton(patent) radioButton_1.setText(u"choix 1") radioButton_2 = QtGui.QRadioButton(parent) radioButton_2.setText(u"choix 2")
radioButton_1.setChecked(True)
etat = radioButton_1.isChecked(True)
#!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import division import sys from PyQt4 import QtCore, QtGui class Fenetre(QtGui.QWidget): def __init__(self, parent=None): super(Fenetre, self).__init__(parent) self.setWindowTitle("Code test") # créer les 3 radioboutons self.groupBox = QtGui.QGroupBox(self) self.radioButton_1 = QtGui.QRadioButton(self.groupBox) self.radioButton_1.setText(u"choix 1") self.radioButton_2 = QtGui.QRadioButton(self.groupBox) self.radioButton_2.setText(u"choix 2") self.radioButton_3 = QtGui.QRadioButton(self.groupBox) self.radioButton_3.setText(u"choix 3") # sélectionner au lancement le radiobouton 0 self.radioButton_1.setChecked(True) # faire que chaque modification des radioboutons exécute la méthode 'change' self.connect(self.radioButton_1, QtCore.SIGNAL('clicked()'), self.change) self.connect(self.radioButton_2, QtCore.SIGNAL('clicked()'), self.change) self.connect(self.radioButton_3, QtCore.SIGNAL('clicked()'), self.change) # création d'une ligne d'édition self.lineEdit = QtGui.QLineEdit(self) # positionnement des widgets dans la fenêtre posit = QtGui.QGridLayout() posit.addWidget(self.groupBox, 0, 0, 1, 3) posit.addWidget(self.radioButton_1, 0, 0) posit.addWidget(self.radioButton_2, 0, 1) posit.addWidget(self.radioButton_3, 0, 2) posit.addWidget(self.lineEdit, 1, 0, 1, 3) self.setLayout(posit) def change(self): """méthode exécutée à chaque changement des radioboutons""" if self.radioButton_1.isChecked(): v = 1 elif self.radioButton_2.isChecked(): v = 2 elif self.radioButton_3.isChecked(): v = 3 else: v = 1 self.lineEdit.setText(unicode(v)) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('plastique')) fen = Fenetre() fen.show() sys.exit(app.exec_())