在Windows和Linux的主窗口中运行线程

时间:2014-06-05 15:48:08

标签: window wxpython pygame

我正在创建一个在GUI内运行的游戏(文本区域,按钮,菜单等)我已经用wxpython创建了一个GUI。我在主窗口中创建了一个面板,它运行了一个pygame线程。

问题:

在Windows上,pygame线程在主窗口内运行完美。但是在Linux上,pygame会弹出一个新窗口。我怎样才能设置这个窗口和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() 

2 个答案:

答案 0 :(得分:1)

解决了问题。

在主窗口中我们必须self.Show() Idk为什么在linux中必须显示主窗口。相同的代码。 全部

答案 1 :(得分:0)

这是一个免责声明,根据https://forums.libsdl.org/viewtopic.php?p=39332,该解决方案仅适用于SDL 1.2而非2.0。