【问题标题】:Run thread in main window for both Windows and Linux在 Windows 和 Linux 的主窗口中运行线程
【发布时间】: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() 

【问题讨论】:

    标签: window wxpython pygame


    【解决方案1】:

    问题已解决。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        • 1970-01-01
        • 1970-01-01
        • 2021-10-02
        • 1970-01-01
        相关资源
        最近更新 更多