6

I am looking for a way in which wxPython GUI elements can be added into pygame. If there is an alternative to solve the purpose of adding GUI elements to a pygame please suggest so. I am having problem in installing PGU.Thanks for help.

3
  • it suprising to know that python has no way of adding a button, in Java it was very straight forward. Please prove me wrong !! Commented Jan 10, 2012 at 3:03
  • Duplicate of stackoverflow.com/questions/8794506/…. Please continue to update your original question with more details on the problems you're having. Commented Jan 10, 2012 at 17:14
  • it is possible as I have done it before. Though i wish it was more compatible. wxPython is much better than any pygame GUI library. PGU sucks big time!!! Dont even bother downloading it. Its very limiting to what you can do when you put pygame in wxpython, though GUI wise, it looks decent. Commented Jan 26, 2012 at 20:37

2 Answers 2

4

Nope; you CAN add wxWidgets to PyGame. It's trivial to add open/save dialogs, buttons, etc. Sometimes it gets nasty when you try to share areas, (as above, there's conflicting window systems), but it is definitely doable.

I wrote the following years ago, so it's messy; but at least it's simple. It should help you get started:

class SDLThread:
    def __init__(self,screen):
        self.m_bKeepGoing = self.m_bRunning = False
        self.screen = screen
        self.color = (255,0,0)
        self.rect = (10,10,100,100)
    def Start(self):
        self.m_bKeepGoing = self.m_bRunning = True
        thread.start_new_thread(self.Run, ())
    def Stop(self):
        self.m_bKeepGoing = False
    def IsRunning(self):
        return self.m_bRunning
    def Run(self):
        while self.m_bKeepGoing:
            pass
##            GetInput()
##            Draw()
        self.m_bRunning = False;
class SDLPanel(wx.Panel):
    def __init__(self,parent,ID,tplSize):
        global pygame
        wx.Panel.__init__(self, parent, ID, size=tplSize)
        self.Fit()
        os.environ['SDL_WINDOWID'] = str(self.GetHandle())
        os.environ['SDL_VIDEODRIVER'] = 'windib'
        import pygame
        pygame.init()
        icon = pygame.Surface((1,1));icon.set_alpha(0);pygame.display.set_icon(icon)
        global Surface
        Surface = pygame.display.set_mode(tplSize)
        self.thread = SDLThread(Surface)
        self.thread.Start()
    def __del__(self):
        self.thread.Stop()
Sign up to request clarification or add additional context in comments.

3 Comments

this looks promising, but can you show HOW you combine it with a pygame demo?
@MarcMaxmeister If I were to write this answer today, I would say something like: "Don't do this. Instead of adding wx widgets into pygame, add pygame surface operations into a wx application. This is will be much simpler, clearer, faster, and easier."
so maybe writing "it is trivial" was premature. I spent hours writing my own functions for creating right-click context menus in pygame, and while they work, they don't seem to have a structure that works generally. too much involvement with the game loop for that.
0

I dont think its possible since wxPython (using wxWidgets c++) is a wrapper around the window management system, it rely on the OS functions to draw on screen (it convert to the right system calls depending on OS).

Pygame uses SDL and a simple code to window management system, it calls few OS functions just to create the window and give it to SDL to draw on it, like a canvas.

One way is:

  • using ctypes to make C calls into OS library files to draw system native widgets, this way you aren't using any pygame functions.

Other way(ugly and not sure if works):

  • I dont know how, but finding a way to convert widgets into image buffer objects and use it like surfaces(plain bitmaps).

What I think you should do:

  • Use pygame, build your own GUI using surfaces and mouse/keyboard events to control it or download some pre-made.

  • Stick with wx, you can build your GUI easily on system native theme, also you can use DC (draw contexts) to draw like pygame, and maybe (not sure) include pygame context into wx as one widget. You can benefit from wx having its own event handler.

I hope it helps you in some way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.