【发布时间】:2014-06-05 15:48:08
【问题描述】:
我正在创建一个在 GUI 中运行的游戏(文本区域、按钮、菜单等) 我已经使用 wxpython 创建了一个 GUI。我在主窗口中创建了一个面板,它运行一个 pygame 线程。
问题:
在 Windows 上,pygame 线程完美地在主窗口内运行。但是在 Linux 上,pygame 会在一个新窗口中弹出。如何设置这个,让 Windows 和 Linux 都在主窗口中运行线程?
代码:
class SDLPanel(wx.Panel):
def __init__(self,parent,ID,tplSize):
global pygame
global pygame_init_flag
wx.Panel.__init__(self, parent, ID, size=tplSize)
self.Fit()
if (sys.platform == 'win32'):
os.environ['SDL_WINDOWID'] = str(self.GetHandle())
os.environ['SDL_VIDEODRIVER'] = 'windib'
else:
os.environ['SDL_VIDEODRIVER'] = 'x11'
#here is where things change if pygame has already been initialized
#we need to do so again
if pygame_init_flag:
#call pygame.init() on subsaquent windows
pygame.init()
else:
#import if this is the first time
import pygame
pygame_init_flag = True #make sure we know that pygame has been imported
pygame.display.init()
window = pygame.display.set_mode(tplSize)
self.thread = SDLThread(window)
self.thread.Start()
def __del__(self):
self.thread.Stop()
print "thread stoped"
#very important line, this makes sure that pygame exits before we
#reinitialize it other wise we get errors
pygame.quit()
【问题讨论】: