pyGTK 应用程序中的 pyGame

发布于 2024-07-04 12:23:36 字数 105 浏览 11 评论 0原文

在 PyGTK 应用程序中使用 PyGame (SDL) 的最佳方式是什么?

我正在寻找一种方法,允许我在 GTK 窗口中拥有绘图区域,同时能够管理 GTK 和 SDL 事件。

What is the best way to use PyGame (SDL) within a PyGTK application?

I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜是你 2024-07-11 12:23:36

http://faq.pygtk.org/index.html py?file=faq23.042.htp&req=show 提到了一切:

实现后需要创建一个绘图区域并设置环境变量SDL_WINDOWID:

 import os

 import gobject
 import gtk
 import pygame

 WINX = 400
 WINY = 200

 window = gtk.Window()
 window.connect('delete-event', gtk.main_quit)
 window.set_resizable(False)
 area = gtk.DrawingArea()
 area.set_app_paintable(True)
 area.set_size_request(WINX, WINY)
 window.add(area)
 area.realize()

 # Force SDL to write on our drawing area
 os.putenv('SDL_WINDOWID', str(area.window.xid))

 # We need to flush the XLib event loop otherwise we can't
 # access the XWindow which set_mode() requires
 gtk.gdk.flush()

 pygame.init()
 pygame.display.set_mode((WINX, WINY), 0, 0)
 screen = pygame.display.get_surface()

 image_surface = pygame.image.load('foo.png')
 screen.blit(image_surface, (0, 0))

 gobject.idle_add(pygame.display.update)

 window.show_all()

 while gtk.event_pending():
     # pygame/SDL event processing goes here
     gtk.main_iteration(False)

http://faq.pygtk.org/index.py?file=faq23.042.htp&req=show mentions it all:

You need to create a drawing area and set the environment variable SDL_WINDOWID after it's realized:

 import os

 import gobject
 import gtk
 import pygame

 WINX = 400
 WINY = 200

 window = gtk.Window()
 window.connect('delete-event', gtk.main_quit)
 window.set_resizable(False)
 area = gtk.DrawingArea()
 area.set_app_paintable(True)
 area.set_size_request(WINX, WINY)
 window.add(area)
 area.realize()

 # Force SDL to write on our drawing area
 os.putenv('SDL_WINDOWID', str(area.window.xid))

 # We need to flush the XLib event loop otherwise we can't
 # access the XWindow which set_mode() requires
 gtk.gdk.flush()

 pygame.init()
 pygame.display.set_mode((WINX, WINY), 0, 0)
 screen = pygame.display.get_surface()

 image_surface = pygame.image.load('foo.png')
 screen.blit(image_surface, (0, 0))

 gobject.idle_add(pygame.display.update)

 window.show_all()

 while gtk.event_pending():
     # pygame/SDL event processing goes here
     gtk.main_iteration(False)
烟火散人牵绊 2024-07-11 12:23:36

有一个简单的解决方案可能适合您。

将 PyGTK 内容和 PyGame 内容编写为单独的应用程序。 然后从 PyGTK 应用程序调用 PyGame 应用程序,使用 os.system 调用 PyGame 应用程序。 如果您需要在两者之间共享数据,那么可以使用数据库、管道或 IPC。

There's a simple solution that might work for you.

Write the PyGTK stuff and PyGame stuff as separate applications. Then from the PyGTK application call the PyGame application, using os.system to call the PyGame application. If you need to share data between the two then either use a database, pipes or IPC.

小清晰的声音 2024-07-11 12:23:36

您可能对此消息线程感兴趣。 看起来他们建议反对。

You may be interested in this message thread. Looks like they recommend against it.

风筝有风,海豚有海 2024-07-11 12:23:36

当 PyGame 可以管理自己的窗口,甚至使用整个屏幕时,效果会更好。 GTK 有足够灵活的小部件来允许创建绘图区域。

不过,此页面可能会有所帮助,如果你想尝试一下。

PyGame works much better when it can manage its own window, or even better, use the whole screen. GTK has flexible enough widgets to allow creation of a drawing area.

This page may help, though, if you want to try it.

云胡 2024-07-11 12:23:36

我自己从未尝试过,但听到很多其他尝试过的人的说法,这不是你想要走的路。

还有一种选择是将 gui 放在 pygame 本身中。 有很多专门为 pygame 构建的 GUI 工具包可供您使用。 其中大多数都尚未完成,但有 2 个大型的、积极维护的: PGUOcempGUI。 pygame 网站上的完整列表位于此处

I've never attempted it myself, but hearing plenty about other people who've tried, it's not a road you want to go down.

There is the alternative of putting the gui in pygame itself. There are plenty of gui toolkits built specifically for pygame that you could use. Most of them are rather unfinished, but there are 2 big, actively maintained ones: PGU and OcempGUI. The full list on the pygame site is here.

看轻我的陪伴 2024-07-11 12:23:36

不久前我自己尝试这样做,但从未让它完美地工作。 事实上,我从来没有让它在 Windows 下工作过,因为它不断地使整个操作系统崩溃,我失去了耐心。 我继续使用它,因为它只在 Linux 上运行才重要,而且只是一个小项目。 我强烈建议您研究替代方案。 这总感觉像是一种令人讨厌的黑客行为,让我感觉很肮脏。

I tried doing this myself a while ago, and I never got it to work perfectly. Actually I never got it to work at all under Windows, as it kept crashing the entire OS and I ran out of patience. I continued to use it though as it was only important it ran on Linux, and was only a small project. I'd strongly recommend you investigate alternatives. It always felt like a nasty hack, and made me feel dirty.

情定在深秋 2024-07-11 12:23:36

Sugar 项目有几个使用 PyGTK 和 PyGame 构建的活动。

他们编写了一个支持库来实现此目的,称为 Sugargame。 您应该能够针对常规 PyGTK 应用程序(而不是 Sugar)修改它。

这里有Sugar 开发书中的一章介绍如何使用它。

该库允许 GTK 和 PyGame 之间的事件通信。

享受!

The Sugar project has several Activities built with PyGTK and PyGame.

They wrote a support lib to achieve this, called Sugargame. You should be able to modify it for regular PyGTK apps instead of Sugar.

Here's a chapter in Sugar's development book about how to use it.

The lib allows for communicating events between GTK and PyGame.

Enjoy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文