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.
-
it suprising to know that python has no way of adding a button, in Java it was very straight forward. Please prove me wrong !!hershey92– hershey922012-01-10 03:03:49 +00:00Commented 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.robots.jpg– robots.jpg2012-01-10 17:14:04 +00:00Commented 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.thelost– thelost2012-01-26 20:37:06 +00:00Commented Jan 26, 2012 at 20:37
2 Answers
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()
3 Comments
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.