[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    RE: [PyQt] Two questions about PyQt
From:       tiob lew <lewtiob () hotmail ! com>
Date:       2009-11-25 15:07:21
Message-ID: BLU137-W14F88FD1D9375DF49B31ECA19C0 () phx ! gbl
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thank you so much for the help. 
Once more question. Again related to the RadioButton. 
Assume we define two RadioButtons as rb1 = QtGui.QRadioButton('MyRadioButton1', self) \
rb2 = QtGui.QRadioButton('MyRadioButton2', self)

If rb1 is checked , one action is performed in some other place.  If rb2 is checked, \
some other action is performed.  For example, in the following code,  if rb1 is \
checked, the name should be set to 'Alice' if Button 'update' is hit.   if rb2 is \
checked, then the name should be set to 'Tom'.  How should the code be modified to \
achieve this goal. 

Many thanks. 


===================================================#!/usr/bin/python#MyUI.py
import sysimport randomfrom PyQt4 import QtCore, QtGui

class MyUI2(QtGui.QWidget):	def __init__(self, \
parent=None):		QtGui.QWidget.__init__(self, parent)  self.setWindowTitle('My UI2')
		self.rb1 = QtGui.QRadioButton('MyButton1', self)		self.rb2 = \
QtGui.QRadioButton('MyButton2', self)  bg = QtGui.QButtonGroup(parent)        \
bg.setExclusive(1)		bg.addButton(self.rb1)		bg.addButton(self.rb2)          hbox = \
QtGui.QHBoxLayout()		hbox.addStretch(1)		hbox.addWidget(self.rb1)		hbox.addWidget(self.rb2)		self.setLayout(hbox)		self.resize(900, \
600)

		lb1 = QtGui.QLabel('Name')
		self.tf1 = QtGui.QLineEdit()		hbox.addWidget(lb1)		hbox.addWidget(self.tf1)
		self.tf1.setText('Jone')				update = \
QtGui.QPushButton('update')		hbox.addWidget(update)		self.connect(update, \
QtCore.SIGNAL('clicked()'),			self.update_info )		                	def \
update_info(self):		tx=''		#if self.rb1.checked():		#	self.tf1.setText('Alice')		#if \
self.rb2.checked():		#	self.tf1.setText('Tom')


app = QtGui.QApplication(sys.argv)ui = \
MyUI2()ui.show()sys.exit(app.exec_())===================================================








> From: david.douard@logilab.fr
> To: pyqt@riverbankcomputing.com
> Subject: Re: [PyQt] Two questions about PyQt
> Date: Wed, 25 Nov 2009 08:46:58 +0100
> CC: lewtiob@hotmail.com
> 
> Le Wednesday 25 November 2009 07:19:48 tiob lew, vous avez écrit :
> > I am a newbie to Python and have two questions using python to design UI
> > based PyQT4.6.
> > 
> > The source code is
> > ===========================================
> > #!/usr/bin/python
> > #MyUI.py
> > 
> > import sys
> > import random
> > from PyQt4 import QtCore, QtGui
> > 
> > 
> > class MyUI(QtGui.QWidget):
> > def __init__(self, parent=None):
> > QtGui.QWidget.__init__(self, parent)
> > self.setWindowTitle('My UI')
> > 
> > cb1 = QtGui.QCheckBox('MyCheckBox1', self)
> > cb2 = QtGui.QCheckBox('MyCheckBox2', self)
> > 
> > bg = QtGui.QButtonGroup(parent)
> > 
> > bg.setExclusive(1)
> > bg.addButton(cb1)
> > bg.addButton(cb2)
> > 
> > hbox = QtGui.QHBoxLayout()
> > hbox.addStretch(1)
> > hbox.addWidget(cb1)
> > hbox.addWidget(cb2)
> > self.setLayout(hbox)
> > self.resize(900, 600)
> > 
> > lb1 = QtGui.QLabel('Name')
> > lb2 = QtGui.QLabel('Age')
> > lb3 = QtGui.QLabel('Address')
> > 
> > tf1 = QtGui.QLineEdit()
> > tf2 = QtGui.QLineEdit()
> > tf3 = QtGui.QLineEdit()
> > 
> > hbox.addWidget(lb1)
> > hbox.addWidget(tf1)
> > hbox.addWidget(lb2)
> > hbox.addWidget(tf2)
> > hbox.addWidget(lb3)
> > hbox.addWidget(tf3)
> > 
> > tf1.setText('Jone')
> > tf2.setText('28')
> > tf3.setText('London')
> > 
> > update = QtGui.QPushButton('update')
> > hbox.addWidget(update)
> > self.connect(update, QtCore.SIGNAL('clicked()'),
> > self, QtCore.SLOT('update_info(self)'))
> > 
> > def update_info(self):
> > tf1.setText('Alice')
> > tf2.setText('18')
> > tf3.setText('New York')
> > 
> > app = QtGui.QApplication(sys.argv)
> > ui = MyUI()
> > ui.show()
> > sys.exit(app.exec_())
> > 
> > 
> > ============================================
> > 
> > I want to implement two functions in this piece of code.
> > 1) implement the exclusive checkbox in a group of checkboxes. In another
> > word, only one of CheckBox1 and Checkbox2 could be checked at any time.
> 
> Use QRadioButton widgets for this.
> 
> > 2) After clicking "update" button, the information should be updated as
> > Name ----> Alice
> > Age -----> 18
> > Address---> New York
> 
> Simply keep references to your QLineEdit widgets as attributes of your class, 
> eg. by replacing everywhere "tf1" by "self.tf1" (and so forth).
> And replace 
> self.connect(update, QtCore.SIGNAL('clicked()'),
> self, QtCore.SLOT('update_info(self)'))
> 
> by 
> self.connect(update, QtCore.SIGNAL('clicked()'),
> self.update_info)
> 
> > 
> > How should the code be modified to implement this two functionalities?
> 
> Basically, you really should read a bit of Python, Qt and PyQt documentation 
> or at least follow tutorials.
> 
> > Many thanks.
> > 
> > _________________________________________________________________
> > Hotmail: Trusted email with powerful SPAM protection.
> > http://clk.atdmt.com/GBL/go/177141665/direct/01/
> 
> 
> 
> -- 
> David Douard                        LOGILAB, Paris (France), +33 1 45 32 03 12
> Formations Python, Zope, Debian :   http://www.logilab.fr/formations
> Développement logiciel sur mesure : http://www.logilab.fr/services
> Informatique scientifique :         http://www.logilab.fr/science
 		 	   		  
_________________________________________________________________
Windows 7: It works the way you want. Learn more.
http://www.microsoft.com/Windows/windows-7/default.aspx?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_evergreen:112009v2



[Attachment #5 (text/html)]

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



_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic