0

I am making my own UI in python 2 and pygame. I have my main script that creates the "screen" variable for the rendering surface in pygame.

How could I go about letting other python scripts access - and render on - the surface of another script?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
MezuCobalt
  • 60
  • 1
  • 2
  • 11
  • It is really unclear what you're asking. Are your scripts related in some ways? Can you explain how you are "calling" them? – 301_Moved_Permanently Aug 04 '15 at 20:01
  • @MathiasEttinger I have "extension" scripts that allow people to make their own widgets. These scripts are activated from the main script through a call (ex. ExtensionSample.Main.Draw()) – MezuCobalt Aug 04 '15 at 20:15
  • @MathiasEttinger I would like the extension scripts to be able to render to the screen/surface that the main script already has. – MezuCobalt Aug 04 '15 at 20:16
  • Note that you can also always use `pygame.display.get_surface()` to get the display surface. (but Mathias Ettinger's answer is a generally better approach). – sloth Aug 05 '15 at 06:37

1 Answers1

1

You can achieve the desired effect by passing your screen variable as a parameter on any call of your extensions modules. The easiest way would be to design your modules as classes whose __init__ method accept the screen to render on:

#extension1.py
class Main(whatever_base_class):
    def __init__(self, screen, *args, **kwargs):
        self.screen = screen
        ...
    def Draw(self):
        #use self.screen to draw on screen.

And from your main script:

from extension1 import Main

#define screen somehow
ext1 = Main(screen, ...)
ext1.Draw()

If you can't or don't want to force the creation/modification of an __init__ in your extensions, you can either rely on a def setup(screen) method or directly pass the screen variable to the Draw method.

301_Moved_Permanently
  • 4,007
  • 14
  • 28