On 06/30/2013 12:10 AM, Phil wrote:
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.

That's part of the error message, and it's paraphrased. In this case, we can guess the rest. But in many cases there are lots of clues in the whole error message. The whole error message is called a traceback, and usually looks something like the one you paste below.



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.

They are. So what's the problem? Again, be specific. Once you have the instance object 'draw', you can access the instance attributes. The only one visible in your code below is called ui. So you do
    draw.ui

to access it. Previously you tried Drawtest.ui, which would access class attributes instead.

There are undoubtedly many other attributes inherited from QMainWindow, but I wouldn't know what they are. dir(draw) will tell you the attributes of the instance object draw.

 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.

If QT works like other GUI's, it should be the instance of the parent window. If there's no parent window, it should be None, which is the default. So it may be correct to simply say

    mywindow = DrawTest()

But since I don't use QT, I can't tell you if that's reasonable or not.



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.


Lots of things are different between Python 2.x and 3.x, like the behavior of classes. In 2.x, you had new-style and old-style classes, and in 3.x you have only the new-style. Similarly, for tkinter, the imports are different between 2.x and 3.x. Telling us ahead of time can avoid people having to give more complex explanations, most of which aren't applicable.

Perhaps the auxiliary functions dir() and help() might be of use when you can't figure out what attributes are available. They're intended for interactive use, but they work just fine inside real code. Just avoid putting them inside loops which might make the console display an inordinate amount of text.


--
DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to