PyQT3.3 problem

Bruce Wolk baextrawolk at pacbell.net
Thu Aug 1 06:35:17 CEST 2002


Try using self.label instead of label everywhere you use label.

On Wed, 31 Jul 2002 21:43:57 -0600, Jim
<jimb417 at NO*@!#$^&SPAMtotalspeed.net> wrote:

>I'm having trouble with this code which displays two pushbuttons and one 
>label. I want to change the text in the label when one of the buttons is 
>clicked:
>
>from qt import *
>import sys
>
>class Widget(QWidget):
>
>                def __init__(self, parent = None, name = None):
>                        QWidget.__init__(self, parent, name)
>
>                        hello = QPushButton( "Hello There!", self )
>                        hello.setGeometry(80, 80, 100, 120)
>
>                        button = QPushButton( "Text", self )
>                        button.setGeometry( 200, 200, 40, 40 )
>
>                        label = QLabel( "This is some text", self )
>                        label.setGeometry( 80, 220, 100, 20 )
>
>                        self.connect( hello, SIGNAL( "clicked()" ), qApp, SLOT( "quit()" ) )
>                        self.connect( button, SIGNAL( "clicked()" ),  qApp, 
>self.buttonClicked()) 
>
>                def buttonClicked( self ):
>                        label.setText ( "Set" )
>
>
>
>app = QApplication (sys.argv)
>a = Widget()
>a.setGeometry( 100, 100, 300, 300 )
>a.setCaption( "This is a widget")
>app.setMainWidget( a )
>a.show()
>app.exec_loop()
>
>What is the proper way to implement the custom slot correctly?  I wrote 
>this from scratch ( not using Qt Designer ) When I run this code, this 
>error pops up:
>
>File "pyExample.py", line 27 in ?
>a = Widget()
>File "pyExample.py", line 19 in __init__
>self.connect( button, SIGNAL( "clicked()" ), qApp, self.buttonClicked() )
>File "pyExample.py", line 22 in buttonClicked
>label.setText( "Changed" )
>NameError: global name 'label' not defined
>Mutex destroy failure: Device or resource busy
>
>How can 'label' be global when it is a member of the class Widget? The docs 
>aren't very clear on this. Can anyone out there give me a nudge in the right
>direction on getting this to work. If you comment out line 19, the widget 
>will display onscreen( of course the button labled "Text" will do nothing 
>when clicked, the other button closes the program ) Thanks for reading this 
>and for any assistance.
>
>Jim
>
>
>
>
>
>-- 
>Registered Linux User #269187
>http://counter.li.org
>




More information about the Python-list mailing list