Graphics Contexts and DCs explanations?

2008-08-19 Thread RgeeK
Experimenting with graphics in an app: it's AUI based with a few panes, 
one of which has a panel containing a few sizers holding UI elements. 
One sizer contains a panel that needs some basic line-drawing graphics 
in it.


I use the wxPython demo app heavily to figure this stuff out, and my 
experiments seem to work, but I'm flying blind somewhat.


Can someone englighten me about the wx.GraphicsContext versus wx.PaintDC 
  (BTW what does PaintDC stand for? Drawing Context perhaps?)


The scrolledWindow and GraphicsContext examples are helpful, but it 
appears I can draw a rectangles, lines and text in either just a 
straight wxPaintDC, or can do:


dc = wx.PaintDC(self)
gc = wx.GraphicsContext.Create(dc)

...and do the same drawing with gc.DrawText etc...

Can someone clarify the differences or added value of the gc over the 
dc, and is using the dc alone a valid approach?  Where will it end up 
biting me - I don't mean which body part :)  Perhaps I should say in 
which situation will it bite...


Thx,
Ross.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Graphics Contexts and DCs explanations?

2008-08-21 Thread RgeeK

Chris Mellon wrote:

This is probably better suited to the wxPython ML instead of c.l.p,
because it's so specific.

In short: wxDC (and friends) are traditional raster based drawing
contexts. wxGraphicsContext is a vector/path based API. If you're
doing drawing that's suited for a vector format (like line drawing
probably is), using wxGraphicsContext will give you better image
quality as well as the general vector features like free scaling,
rotation, transforms, etc,


Thx Chris - I wasn't aware of the wxPython ML.  I'll hunt around for it 
for further q's in that vein.


Your explanation is helpful tho' - thx.
--
http://mail.python.org/mailman/listinfo/python-list


Unexpected global variable behaviour

2008-08-25 Thread RgeeK
I'm seeing something which make me think I'm missing something  about 
how global var's behave.  I've defined a global string, right at the 
start of my .py file.


outXMLfile = "abc"

I define a class and do a bunch of stuff below that.  Then I have 
another class, and in it, there is a method 'def' that has:


  def OnOutfileButton(self,evt):
   (fPath, fName)=os.path.split(fullName)
print "Selected output file: " + fName
outXMLfile = fName

print "output file: " + outXMLfile

Print statements in random other places in the project, show outXMLfile 
prints as "abc"  however, in this def, it comes out as the same as fName 
(e.g. "myfile.xml")   If the print line for outXMLfile is before the 
assignment to fName, it throws the error:


 UnboundLocalError: local variable 'outXMLfile' referenced before 
assignment


If I remove the line that says "outXMLfile = fName"
then the print statement gives me the value "abc"

What am I missing?  When I assign a value to update my global variable, 
it becomes a local variable. If I don't try to update it, it stays 
global.  I assume it's acting like a constant, though I have a couple of 
global lists and I seem to be able to append to them okay.


I guess I can move the variable into the parent class and get at it that 
way, but want to understand the error of my ways...


Ross.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected global variable behaviour

2008-08-26 Thread RgeeK

Thanks a lot Fredrik,

A big wave of deja vu came over me as I read your note. That bit me once 
before, and was my only previous run-in with the 'global' directive 
which clearly didn't stick with me. :(


Thanks for the concise, helpful reply. The universe makes sense again.

-Ross.




Fredrik Lundh wrote:

http://docs.python.org/ref/naming.html

"If a name binding operation occurs anywhere within a code block, all 
uses of the name within the block are treated as references to the 
current block. This can lead to errors when a name is used within a 
block before it is bound."


to fix this, use the global directive:

http://docs.python.org/ref/global.html

   def OnOutfileButton(self,evt):
 global outXMLfile # flag variable as global
 fPath, fName = os.path.split(fullName)
 print "Selected output file:", fName
 outXMLfile = fName
 ...




--
http://mail.python.org/mailman/listinfo/python-list


Global var access in imported modules?

2008-08-27 Thread RgeeK
I have a main module doStuff.py and another module utility.py.  At the 
start of doStuff.py I call


   import utility.py

Then I also proceed to initiallize some global variables

sName = ""

Then I create a class, some methods etc.  In one of the methods I assign
a value to my variable sName.  Then I call a function from within
my utility.py file:

 utility.makeOne(stuff)


Within my utility.py file, I define the makeOne function. But I want to 
use that same global variable "sName"  In utility.py I have tried to 
indicate that I'm using the global "sName" through the statement:


  global sName

But when I go to use the variable it still gives me an error:

  NameError: global name 'sName' is not defined

I thought perhaps I need to indicate 'globality' in my main module, so 
before I initiallized sName in doStuff.py I added:


global sName

But it doesn't help me.   I had this issue before and resolved it by 
declaring the variable global in the sub-module utility.py, but then I 
needed to reference it in my main module with a prefix:


 utility.sName = ""

It's more verbose,and defining globals in a submodule seems backward.
But also, what if I need to access "sName" in another imported module, 
say "otherstuff.py"?   I would do my "import otherstuff" call in my main 
module, but would I have to put an "import utility" into the 
otherstuff.py file?


Is there some way I can define globals in my main module, and have them 
accessible in all my imported submodule?


As you can see I'm a little unsure about the global handling in a 
multi-module environment. Any suggestions appreciated. I've read 
http://docs.python.org/ref/naming.html but it hasn't enlightened me on 
this one.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Global var access in imported modules?

2008-08-27 Thread RgeeK

Fredrik Lundh wrote:
>>import utility.py
> that tries to import a module named "py" from the package "utility".

oops - that was just a typo in my post - I meant of course "import utility"
>
> Python doesn't have "program-wide global" variables; if you need that,
> create a support module and import that module everywhere you need to
> access those variables:
>
> # file: globalvars.py
> sName = ""
>
> # file: myprogram.py
> import globalvars
> print globalvars.sName
>
> etc.
>
> 
>

That's news - thanks, I didn't realize that there just wasn't the 
concept of program-wide globals.  The support module idea sounds like a 
path forward.


-R.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Global var access in imported modules?

2008-08-27 Thread RgeeK

Dennis Lee Bieber wrote:

On Wed, 27 Aug 2008 16:21:03 -0400, RgeeK <[EMAIL PROTECTED]>
declaimed the following in comp.lang.python:

I have a main module doStuff.py and another module utility.py.  At the 
start of doStuff.py I call


import utility.py


I hope not...   import utility  no .py

Then I also proceed to initiallize some global variables


Python does not have global variables. Names belong within a module
(or within functions defined within the module).


sName = ""

Then I create a class, some methods etc.  In one of the methods I assign
a value to my variable sName.  Then I call a function from within
my utility.py file:

  utility.makeOne(stuff)


Within my utility.py file, I define the makeOne function. But I want to 
use that same global variable "sName"  In utility.py I have tried to 
indicate that I'm using the global "sName" through the statement:


   global sName


The global statement is only used within functions (def blocks) to
indicate that "writes" to the specified name are to modify the MODULE
level version of the name, otherwise a write modifies a function local
version of the name (you don't need global for read-only access of
names, the search for names first looks inside the function, then out to
the module)
 

But when I go to use the variable it still gives me an error:

   NameError: global name 'sName' is not defined

I thought perhaps I need to indicate 'globality' in my main module, so 
before I initiallized sName in doStuff.py I added:


global sName

But it doesn't help me.   I had this issue before and resolved it by 
declaring the variable global in the sub-module utility.py, but then I 
needed to reference it in my main module with a prefix:


  utility.sName = ""

It's more verbose,and defining globals in a submodule seems backward.
But also, what if I need to access "sName" in another imported module, 
say "otherstuff.py"?   I would do my "import otherstuff" call in my main 
module, but would I have to put an "import utility" into the 
otherstuff.py file?



If you really need "globals" the common solution is to create a
module such as "myglobals", define all the shared names within that
module, and import that module where ever you need access to one of the
names. And yes, you will need to qualify all those names with the module
name (though you can do things like:

import myglobals as mg

and then use

mg.somename

instead of

myglobals.somename)
 



Thanks for the reply.  Good to see that approach has broad support :) 
I'll do that.  I like the idea of a nice short alias for the import to 
keep the qualifications brief.


Ross.
--
http://mail.python.org/mailman/listinfo/python-list