Python modules, anyone?

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Python modules, anyone?

Postby maximinus » Tue Apr 12, 2005 9:49 pm

Any python coders out there? I am having problems with my own modules. (I'm trying to code a python/pygame gui.)

Basically I have a gfx class which stores all the graphics, which is in it's own seperate module. Next I have the window widget classes, in another module, which need to be able to access those graphics. The trouble is, I create the instance of the gfx class in my main .py file, and then need to use this in the seperate modules - which python doesn't let me do :-(

If I write all the modules out in a single text file, the code works. When it gets broken into modules, I get problems. Am I missing something obvious, or is there a neat solution?
maximinus
 
Posts: 2
Joined: Tue Apr 12, 2005 9:17 pm

RE: Python modules, anyone?

Postby evilnick » Tue Apr 12, 2005 10:12 pm

Everything in python breaks down into objects. You can use that to pass objects between modules etc., and you may want to import modules from modules.
It sounds like your widget module wants to import from your graphics module. difficult to say without some example code
User avatar
evilnick
Moderator
 
Posts: 151
Joined: Mon Apr 04, 2005 11:47 am
Location: LXF towers

Postby maximinus » Tue Apr 12, 2005 10:49 pm

Code example:

In main.py:

Code: Select all
# set up gui and load graphics (all in constructor code)
import spqr_gui
my_gui=spqr_gui.game_gui(SCREEN_WIDTH,SCREEN_HEIGHT)


somewhere in a widgets module

Code: Select all
# draw the image
base_image=my_gui.images[WINDOW_TOP_LEFT].image


This fails because my_gui is not yet known when it looks at the widgets module. I could pass my_gui every time I use a function in another module, but this doesn't seem a neat way of doing it at all.
I need some way of storing the graphics in a way that they can be seen by many other modules that use this data.
maximinus
 
Posts: 2
Joined: Tue Apr 12, 2005 9:17 pm

Postby evilnick » Thu Apr 14, 2005 9:11 am

Are the graphics going to be persistent ? Ar eyou always going to use the same graphics with this module? You could set up some static objects in the module.

But usually, the answer would be to have the main.py initiate the class and pass the relevant item to other functions:

Code: Select all
# set up gui and load graphics (all in constructor code)
import spqr_gui
my_gui=spqr_gui.game_gui(SCREEN_WIDTH,SCREEN_HEIGHT)
...
widget_function(my_gui.images[WINDOW_TOP_LEFT].image)


Alternatively, you can import the gui graphics module into the widget code and just pass the reference to my_gui.


Code: Select all
# set up gui and load graphics (all in constructor code)
import spqr_gui
my_gui=spqr_gui.game_gui(SCREEN_WIDTH,SCREEN_HEIGHT)
...
widget_function(my_gui)
User avatar
evilnick
Moderator
 
Posts: 151
Joined: Mon Apr 04, 2005 11:47 am
Location: LXF towers


Return to Programming

Who is online

Users browsing this forum: Majestic-12 [Bot] and 0 guests