<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
Thank you so much for the help. <div><br></div><div>Once more question. Again related to the RadioButton. </div><div><br></div><div>Assume we define two RadioButtons as</div><div> rb1 = QtGui.QRadioButton('MyRadioButton1', self)<br style="text-indent: 0in !important; "> rb2 = QtGui.QRadioButton('MyRadioButton2', self)</div><div><br></div><div><br></div><div>If rb1 is checked , one action is performed in some other place. If rb2 is checked, some other action is performed. </div><div><br></div><div>For example, in the following code, </div><div> if rb1 is checked, the name should be set to 'Alice' if Button 'update' is hit. </div><div> if rb2 is checked, then the name should be set to 'Tom'. </div><div><br></div><div>How should the code be modified to achieve this goal. </div><div><br></div><div><br></div><div>Many thanks. </div><div><br></div><div><br></div><div><br></div><div>===================================================</div><div><div>#!/usr/bin/python</div><div>#MyUI.py</div><div><br></div><div>import sys</div><div>import random</div><div>from PyQt4 import QtCore, QtGui</div><div><br></div><div><br></div><div>class MyUI2(QtGui.QWidget):</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>def __init__(self, parent=None):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>QtGui.QWidget.__init__(self, parent)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.setWindowTitle('My UI2')</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.rb1 = QtGui.QRadioButton('MyButton1', self)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.rb2 = QtGui.QRadioButton('MyButton2', self)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>bg = QtGui.QButtonGroup(parent)</div><div> </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>bg.setExclusive(1)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>bg.addButton(self.rb1)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>bg.addButton(self.rb2)</div><div> </div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox = QtGui.QHBoxLayout()</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox.addStretch(1)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox.addWidget(self.rb1)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox.addWidget(self.rb2)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.setLayout(hbox)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.resize(900, 600)</div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>lb1 = QtGui.QLabel('Name')</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.tf1 = QtGui.QLineEdit()</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox.addWidget(lb1)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox.addWidget(self.tf1)</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.tf1.setText('Jone')</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>update = QtGui.QPushButton('update')</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>hbox.addWidget(update)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.connect(update, QtCore.SIGNAL('clicked()'),</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>self.update_info )</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>def update_info(self):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>tx=''</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>#if self.rb1.checked():</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>#<span class="Apple-tab-span" style="white-space:pre">        </span>self.tf1.setText('Alice')</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>#if self.rb2.checked():</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>#<span class="Apple-tab-span" style="white-space:pre">        </span>self.tf1.setText('Tom')</div><div><br></div><div><br></div><div><br></div><div>app = QtGui.QApplication(sys.argv)</div><div>ui = MyUI2()</div><div>ui.show()</div><div>sys.exit(app.exec_())</div></div><div>===================================================</div><div><br></div><div><br></div><div><br></div><div><div><br></div></div><div><br></div><div><br></div><div><br></div><div><br>> From: david.douard@logilab.fr<br>> To: pyqt@riverbankcomputing.com<br>> Subject: Re: [PyQt] Two questions about PyQt<br>> Date: Wed, 25 Nov 2009 08:46:58 +0100<br>> CC: lewtiob@hotmail.com<br>> <br>> Le Wednesday 25 November 2009 07:19:48 tiob lew, vous avez écrit :<br>> > I am a newbie to Python and have two questions using python to design UI<br>> > based PyQT4.6.<br>> ><br>> > The source code is<br>> > ===========================================<br>> > #!/usr/bin/python<br>> > #MyUI.py<br>> ><br>> > import sys<br>> > import random<br>> > from PyQt4 import QtCore, QtGui<br>> ><br>> ><br>> > class MyUI(QtGui.QWidget):<br>> > def __init__(self, parent=None):<br>> > QtGui.QWidget.__init__(self, parent)<br>> > self.setWindowTitle('My UI')<br>> ><br>> > cb1 = QtGui.QCheckBox('MyCheckBox1', self)<br>> > cb2 = QtGui.QCheckBox('MyCheckBox2', self)<br>> ><br>> > bg = QtGui.QButtonGroup(parent)<br>> ><br>> > bg.setExclusive(1)<br>> > bg.addButton(cb1)<br>> > bg.addButton(cb2)<br>> ><br>> > hbox = QtGui.QHBoxLayout()<br>> > hbox.addStretch(1)<br>> > hbox.addWidget(cb1)<br>> > hbox.addWidget(cb2)<br>> > self.setLayout(hbox)<br>> > self.resize(900, 600)<br>> ><br>> > lb1 = QtGui.QLabel('Name')<br>> > lb2 = QtGui.QLabel('Age')<br>> > lb3 = QtGui.QLabel('Address')<br>> ><br>> > tf1 = QtGui.QLineEdit()<br>> > tf2 = QtGui.QLineEdit()<br>> > tf3 = QtGui.QLineEdit()<br>> ><br>> > hbox.addWidget(lb1)<br>> > hbox.addWidget(tf1)<br>> > hbox.addWidget(lb2)<br>> > hbox.addWidget(tf2)<br>> > hbox.addWidget(lb3)<br>> > hbox.addWidget(tf3)<br>> ><br>> > tf1.setText('Jone')<br>> > tf2.setText('28')<br>> > tf3.setText('London')<br>> ><br>> > update = QtGui.QPushButton('update')<br>> > hbox.addWidget(update)<br>> > self.connect(update, QtCore.SIGNAL('clicked()'),<br>> > self, QtCore.SLOT('update_info(self)'))<br>> ><br>> > def update_info(self):<br>> > tf1.setText('Alice')<br>> > tf2.setText('18')<br>> > tf3.setText('New York')<br>> ><br>> > app = QtGui.QApplication(sys.argv)<br>> > ui = MyUI()<br>> > ui.show()<br>> > sys.exit(app.exec_())<br>> ><br>> ><br>> > ============================================<br>> ><br>> > I want to implement two functions in this piece of code.<br>> > 1) implement the exclusive checkbox in a group of checkboxes. In another<br>> > word, only one of CheckBox1 and Checkbox2 could be checked at any time.<br>> <br>> Use QRadioButton widgets for this.<br>> <br>> > 2) After clicking "update" button, the information should be updated as<br>> > Name ----> Alice<br>> > Age -----> 18<br>> > Address---> New York<br>> <br>> Simply keep references to your QLineEdit widgets as attributes of your class, <br>> eg. by replacing everywhere "tf1" by "self.tf1" (and so forth).<br>> And replace <br>> self.connect(update, QtCore.SIGNAL('clicked()'),<br>> self, QtCore.SLOT('update_info(self)'))<br>> <br>> by <br>> self.connect(update, QtCore.SIGNAL('clicked()'),<br>> self.update_info)<br>> <br>> ><br>> > How should the code be modified to implement this two functionalities?<br>> <br>> Basically, you really should read a bit of Python, Qt and PyQt documentation <br>> or at least follow tutorials.<br>> <br>> > Many thanks.<br>> ><br>> > _________________________________________________________________<br>> > Hotmail: Trusted email with powerful SPAM protection.<br>> > http://clk.atdmt.com/GBL/go/177141665/direct/01/<br>> <br>> <br>> <br>> -- <br>> David Douard LOGILAB, Paris (France), +33 1 45 32 03 12<br>> Formations Python, Zope, Debian : http://www.logilab.fr/formations<br>> Développement logiciel sur mesure : http://www.logilab.fr/services<br>> Informatique scientifique : http://www.logilab.fr/science<br></div>                                            <br /><hr />Windows 7: It works the way you want. <a href='http://www.microsoft.com/Windows/windows-7/default.aspx?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen:112009v2' target='_new'>Learn more.</a></body>
</html>