[Tutor] Self and class functions

Phil phil_lor at bigpond.com
Sun Jun 30 06:10:07 CEST 2013


On 30/06/13 11:41, Dave Angel wrote:

Thank you Dave, Matthew and Steven for taking the time to reply. All 
hints are greatly appreciated.

>> I'm attempting to access the GUI widgets. The buttonClicked slot does
>> what I want but I'd like to access the widgets outside of the class. My
>> function setLabel doesn't work because self and ui are not known.
>>

Perhaps my "doesn't work" statement was a bit cryptic. The error message 
read "name self is not defined" and the same for ui.

I have attempted to create an instance of the DrawTest class although I 
had not tried DrawTest(parent) and in this case the error message is 
once again:

Traceback (most recent call last):
   File "/home/phil/Python/Qt_draw_sub/draw_test.py", line 52, in <module>
     draw = DrawTest(parent)
NameError: name 'parent' is not defined


>> I have a second class named Frame that includes a function named dummy.
>> Accessing that function is not a problem.

Creating an instance of the Frame class is not a problem, although 
accessing it's attributes without a function call is causing me to 
scratch my head. This is not possible under C++ unless the classes are 
friends but I thought that I'd read somewhere that, under Python, 
attributes are accessible directly. Anyway, this is a side issue and I 
only included that in my previous call for help to show that I 
understand the need to create an instance of the class.

>
> The function is called a method if it's actually part of a class.
>
> You don't show us the code for that class and method.  But according to
> the call below, the method must be a staticmethod or equivalent. Perhaps
> like the following:
>
> class Frame:
>      @staticmethod
>      def dummy():
>          return 42
>

Yes, much like that.

> Static methods are strange because they have no self or cls arguments.
> In other words, they're just ordinary functions which happen to be
> defined inside a class.  So it's in a different namespace, but has no
> special arguments.
>
> The other "strange" method is a classmethod, which takes a cls (class)
> parameter instead of a self parameter.
>
> The methods below are ordinary methods, and thus need a self parameter.
>   Normally that's obtained by creating an instance of the class.
>
>
>>
>> -------------------------------------
>>
>> from mainwindow import Ui_MainWindow
>> from PySide import QtCore, QtGui
>> from frame import Frame
>>
>> class DrawTest(QtGui.QMainWindow):
>>      def __init__(self, parent=None):
>>          super(DrawTest, self).__init__(parent)
>>
>>          self.ui = Ui_MainWindow()
>>          self.ui.setupUi(self)
>>
>>      def buttonClicked(self):
>>          print("clicked")
>>          self.ui.pushButton.setText("test")
>>          self.ui.label.setText("Testing")
>>
>>      def setLabel(self):
>>          self.ui.label.setText("Bingo")
>>
>> DrawTest.setLabel(self)
>> DrawTest.ui.label.setText("Bingo")
>>
>> The two lines above don't work,
>
> You generally should specify in what way they don't work.  In your case
> they don't work because 'self' is unbound in top-level code.  You could
> use self if you were already inside a method of the same class and were
> calling a different one.
>
>> so my question is how do access the
>> setText function? And, what do I do about the self parameter in the
>> setLabel function?
>>
>
> Normally, you create an instance of the class.  For example, you might do:
>      mywindow = DrawTest(parent)
>
> Then, now you have the object, you can call the method:
>
>      mywindow.setLabel()
>

See above, "parent not defined". What should the defined parameter be? 
That's really what I was asking.

>
> You need to play with classes using a simple Python tutorial, before you
> try to even understand a GUI.
>

I done little else over the weekend but play with classes. It's been 
eight or nine years since I've done any programming at all and then it 
was strictly C++ and Qt. Extending that experience to PyQt or PySide is 
not completely straight forward especially as the old memory fades.

Although it doesn't seem relevant to the question, I'm programming with 
Python 3 and, on this particular computer, the OS is Kububtu 13.04.

-- 
Regards,
Phil


More information about the Tutor mailing list