Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Python modules, anyone?

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
maximinus



Joined: Tue Apr 12, 2005 10:17 pm
Posts: 2

PostPosted: Tue Apr 12, 2005 10:49 pm    Post subject: Python modules, anyone? Reply with quote

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 Sad

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
View user's profile Send private message
evilnick
Moderator


Joined: Mon Apr 04, 2005 12:47 pm
Posts: 151
Location: LXF towers

PostPosted: Tue Apr 12, 2005 11:12 pm    Post subject: RE: Python modules, anyone? Reply with quote

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
View user's profile Send private message
maximinus



Joined: Tue Apr 12, 2005 10:17 pm
Posts: 2

PostPosted: Tue Apr 12, 2005 11:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
evilnick
Moderator


Joined: Mon Apr 04, 2005 12:47 pm
Posts: 151
Location: LXF towers

PostPosted: Thu Apr 14, 2005 10:11 am    Post subject: Reply with quote

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 user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
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
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast