 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
maximinus
Joined: Tue Apr 12, 2005 10:17 pm Posts: 2
|
Posted: Tue Apr 12, 2005 10:49 pm Post subject: Python modules, anyone? |
|
|
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? |
|
| Back to top |
|
 |
evilnick Moderator

Joined: Mon Apr 04, 2005 12:47 pm Posts: 151 Location: LXF towers
|
Posted: Tue Apr 12, 2005 11:12 pm Post subject: RE: Python modules, anyone? |
|
|
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 |
|
| Back to top |
|
 |
maximinus
Joined: Tue Apr 12, 2005 10:17 pm Posts: 2
|
Posted: Tue Apr 12, 2005 11:49 pm Post subject: |
|
|
Code example:
In main.py:
| Code: | # 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: | # 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. |
|
| Back to top |
|
 |
evilnick Moderator

Joined: Mon Apr 04, 2005 12:47 pm Posts: 151 Location: LXF towers
|
Posted: Thu Apr 14, 2005 10:11 am Post subject: |
|
|
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: |
# 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: |
# 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)
|
|
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|