Simple Class Question - need clarity please

S

Stevie_mac

OK, 1st off, I'm brand new to python so all help is appreciated. Right...

Lets say I want a class called Window. It will have some functions namely DoModal()
Now this class is inherited in MyClass
What is wrong with this?...

import win32con, win32ui
from pywin.mfc import dialog #, window
class Window:
def MakeDlgTemplate(self):
style = win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION |
win32con.WS_SYSMENU | win32con.DS_SETFONT
dlg = [ ["My Dialog", (0, 0, 100, 100), style, None, (8, "MS Sans Serif")], ]
s = win32con.BS_PUSHBUTTON | win32con.WS_CHILD | win32con.WS_VISIBLE
dlg.append([128, "Cancel", win32con.IDCANCEL, (55, 80, 40, 16), s])
return dlg
def DoModal(self):
mw = win32ui.CreateDialogIndirect( self.MakeDlgTemplate() )
mw.DoModal()

w = Window()
w.DoModal()

^ ^ ^ This Works ^ ^ ^
but if I disable w = Window() & w.DoModal() and derive from this class . . .

# file test.py #
import mywindow
class MyClass(mywindow.Window):
def Show(self):
self.DoModal()

t = MyClass()
t.Show()

.. . . I get
NameError: global name 'MakeDlgTemplate' is not defined ???

another 1 I managed to clear up was . . .
TypeError: Show() takes no arguments (1 given)
.. . . until I added 'self' to the Show function ???


I really am struggling with this - some real clarity is needed - can anyone explain the rules (I've read stuff &
dissected samples, but I still struggle when it comes to this. If someone can clear this up for me, I should be flying
as I am familiar with the concepts of OOP)

I'm also struggling with other things like...
Where do I save .py files?
Will 'import' find my files where ever they are? (obviously not but...)
What basic should be considerations when deriving classes?

any help will be greatly appreciated - Ta Stevie_Mac
 
A

Alan Gauld

What is wrong with this?...

import win32con, win32ui
from pywin.mfc import dialog #, window
class Window:
def MakeDlgTemplate(self): ....
def DoModal(self):
mw = win32ui.CreateDialogIndirect( self.MakeDlgTemplate() )
mw.DoModal()

# file test.py #
import mywindow
class MyClass(mywindow.Window):
def Show(self):
self.DoModal()

t = MyClass()
t.Show()

. . . I get
NameError: global name 'MakeDlgTemplate' is not defined ???

Its a good idea to post the entire error traceback so we see the
actual line of code the error refers to. This error usually means
you accessed a name from a module without prefixing it with the
module name. But so far as I can see you haven't done that here.
another 1 I managed to clear up was . . .
TypeError: Show() takes no arguments (1 given)
. . . until I added 'self' to the Show function ???

Yes, every method of a class must have a parameter that refers to
the instance at call time, usually called self (or maybe "this"
if you come from Java or C++)

At runtime Python effectively calls

class.method(instance)
I'm also struggling with other things like...
Where do I save .py files?

Anywhere you like! I have a folder called Projects\Python where I
keep mine :)

So long as Python knows where to find them - either by modifying
the sys.path value (explicitly or via the registry setting) or
the PYTHONPATH environment variable...
Will 'import' find my files where ever they are?

Yes if one of the two locations above are set.

HTH,

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
S

Stevie_mac

Its a good idea to post the entire error traceback so we see the
actual line of code the error refers to. This error usually means
you accessed a name from a module without prefixing it with the
module name. But so far as I can see you haven't done that here.

# mywindow.py #
import win32con, win32ui
from pywin.mfc import dialog #, window
class Window:
def MakeDlgTemplate(self):
style = win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE | win32con.WS_CAPTION |
win32con.WS_SYSMENU | win32con.DS_SETFONT
dlg = [ ["My Dialog", (0, 0, 100, 100), style, None, (8, "MS Sans Serif")], ]
s = win32con.BS_PUSHBUTTON | win32con.WS_CHILD | win32con.WS_VISIBLE
dlg.append([128, "Cancel", win32con.IDCANCEL, (55, 80, 40, 16), s])
return dlg
def DoModal(self):
mw = win32ui.CreateDialogIndirect( self.MakeDlgTemplate() )
mw.DoModal()

import mywindow
class MyClass(mywindow.Window):
def Show(self):
self.DoModal()

t = MyClass()
t.Show()

#ERROR OUTPUT
File "C:\My Python Stuff\window\test.py", line 7, in ?
t.Show()
File "C:\My Python Stuff\window\test.py", line 4, in Show
self.DoModal()
File "C:\My Python Stuff\window\mywindow.py", line 12, in DoModal
mw = win32ui.CreateDialogIndirect( self.MakeDlgTemplate() )
NameError: global name 'MakeDlgTemplate' is not defined
 
S

Shalabh Chaturvedi

Stevie_mac said:
OK, 1st off, I'm brand new to python so all help is appreciated. Right...

Lets say I want a class called Window. It will have some functions namely
DoModal() Now this class is inherited in MyClass
What is wrong with this?...

. . . I get
NameError: global name 'MakeDlgTemplate' is not defined ???

On which line in your example do you get this error (from the traceback)?
another 1 I managed to clear up was . . .
TypeError: Show() takes no arguments (1 given)
. . . until I added 'self' to the Show function ???

Going through the Python tutorial should clear this up and other confusions
as well. To quote "the method function is declared with an explicit first
argument representing the object, which is provided implicitly by the
call". I urge you read the entire section on classes [1].
I really am struggling with this - some real clarity is needed - can
anyone explain the rules (I've read stuff &
dissected samples, but I still struggle when it comes to this. If someone
can clear this up for me, I should be flying as I am familiar with the
concepts of OOP)

I'm also struggling with other things like...
Where do I save .py files?
Will 'import' find my files where ever they are? (obviously not but...)

Look for PYTHONPATH and sys.path in the documentation. Hmm, now I urge you
to go through the entire tutorial - it doesn't take long!
What basic should be considerations when deriving classes?

any help will be greatly appreciated - Ta Stevie_Mac

HTH,
Shalabh

[1] http://python.org/doc/2.3.3/tut/node11.html
 
P

Patrick Ellis

Stevie_mac said:
<snipped code for mywindow.py and test.py>

<snipped error message from running test.py>

I cut and pasted from your post into mywindow.py and test.py. When I ran
test.py I got a window with a cancel button, that went away when I clicked
the button. There were no error messages.
 
S

Stevie_mac

very odd. Works for my now too. Is there some sort of cache that holds onto old (bad) code?

I was definitely getting an error when I ran test.py, but not when I ran mywindow directly!
 
A

Alan Gauld

very odd. Works for my now too. Is there some sort of cache that
holds onto old (bad) code?

How were you running it? From an OS command prompt or from the
IDLE shell (or any other python) prompt? If the latter maybe you
needed to do a

But if from the OS prompt it should be fine.

Alan g.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
 
S

Stevie_mac

Yes, I've since found the to be the case (reload). I realised it didn't like some formatting & held onto the old (bad)
code.

I had corrected some indentation ( a tab character was used in 1 line were all others were spaces ). Once corrected, I
reloaded & all was OK

PS, using activepython (pythonwin) in windows
 
S

Scott David Daniels

Shalabh Chaturvedi (and before him Alan Gauld) gave lots of good advice
to Stevie_mac, including:
....
Look for PYTHONPATH and sys.path in the documentation. Hmm, now I urge you
to go through the entire tutorial - it doesn't take long!

Another technique to consider:

I drop a file in my .../Python23/Lib/site-packages directory.
It contains a line with an unquoted directory name to add to the path.
My file is named 'projects.pth', but use any name you like as long as
it ends with '.pth' -- the name is unimportant.
You don't want it to conflict with names you are likely to get from
outside parties (third-party packages like PIL often use this
technique). I should probably call it 'ScottsWorkingTopics.pth' by
that logic.

There is no restriction to naming a single directory, but each should
be on its own line. If I ember correctly, starting a line with a
hash mark (#) should make it a comment, but check your python docs to
be sure.

In my case, again, the line is: C:\Projects\Current\Python
I've never tried using windows magic like:
%USERPROFILE%\DeskTop\Py
for the path names; you could try it and report on the results.
I also usually try to derive from object (for "new-style" classes.
I don't even particularly need their features; I am simply getting
used to the way all classes will eventually work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top