我需要注意将 Tkinter 和 pygame 一起使用吗? [英] Is there anything I need aware of using Tkinter and pygame together?

查看:51
本文介绍了我需要注意将 Tkinter 和 pygame 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pygame 编写程序,我需要一些 GUI 配置文本字段和按钮进行控制.我已经使用 pygame 制作了按钮,但我只能用 pygame 编写一个文本字段.也许我需要将 tkinter 与 pygame 一起使用.

I am using pygame to write a program and I need some GUI configuration text field and button for control. I've already made the button using pygame, but I just can write a text field out of pygame. Maybe I need to use tkinter together with pygame.

我认为如果无法将 pygame 部分和 tkinter 部分放在 1 个窗口中,那么我可以将它们放入 2 个单独的窗口中.

I think if there is no way to made to pygame part and tkinter part together in 1 window, then I could put them into 2 separate windows.

我希望 tkinter 部分可以更新我的 pygame 部分中的全局变量,如果有任何问题会出现吗?我可能会从 pygame 部分创建一个 tkinter 的子进程,以便 tkinter 部分可以看到"pygame 部分中的全局变量并对其进行修改.

I hope the tkinter part can update the global variable in my pygame part, would there if any problem? I might create a child process of tkinter from the pygame part so that the tkinter part can probably "see" the global variable in pygame part and modify them.

我可以这样做吗?有什么坑吗?

Can I do this? Are there any pitfalls?

推荐答案

Tkinter 和 Pygame 都有自己的事件循环,所以做你想做的事情绝非易事.问题是 Pygame 想要控制屏幕和用户输入计算机的事件.这不适用于 GUI 库,它们也希望处于控制之中".

Both Tkinter and Pygame have their own event loops, so doing what you want is far from simple. The problem is that Pygame wants to control both the screen and the events the user feeds into the computer. This doesn't work well with GUI libraries, which also want to be "in control".

我建议坚持使用 Pygame,它有一些不错的 GUI 工具包,可以帮助您创建按钮和其他控件.翻阅这个页面 - 它应该清除一切.您可能还会发现此讨论很有用.

I would suggest sticking with Pygame, it has some nice GUI toolkits that will help you create buttons and other controls. Go over this page - it should clear things out. You may also find this discussion useful.

除了实用方面之外,恕我直言,使用 Pygame 创建的 GUI 也比使用 Tkinter 创建的更适合游戏,因为游戏通常具有原创的、主题化的用户界面,而不是我们平淡的文本框 + 按钮"窗口重新用于其他应用程序.

Apart from the practical aspects, a GUI created with Pygame is also IMHO more suitable for a game than something done with Tkinter, since games usually have original, thematical user interfaces and not the bland "text box + button" windows we're used to in other applications.

查看 Pygame wiki 上的一些示例游戏,其中许多都有 GUI,您可以从中借鉴想法和代码.

Take a look at some of the sample games on the Pygame wiki, many have GUIs and you can borrow ideas and code from them.

这篇关于我需要注意将 Tkinter 和 pygame 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆