如何为 Pygame 创建这个 tkinter 菜单栏

3

我想在pygame中创建一个菜单栏,就像tkinter中的这个一样。

from tkinter import *
def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()

这是tkinter的代码,但有谁知道如何在pygame中以完全相同的方式创建它?谢谢。
2个回答

5
我使用了os.environ将pyGame与tkinter结合在一起,这样你的tkinter窗口中就会有一个pygame窗口:
from tkinter import *
from pygame import *
from pygame.locals import *
import os

我为pygame添加了一个框架:

def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)

embed = Frame(root, width=640, height=480)
embed.pack()

这里是新增的环境变量:

# Tell pygame's SDL window which window ID to use
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
# Show the window so it's assigned an ID.
root.update()

# Usual pygame initialization
pygame.init()

# Dimensions should match those used in the embed frame
screen = pygame.display.set_mode((640, 480))

running = True
def done():
    global running
    running = False

root.protocol("WM_DELETE_WINDOW", done)
while running:
    # game logic goes here
    pygame.display.flip()  # (or pygame.display.update())
    root.update_idletasks()
    root.update()
pygame.quit()

希望这能在某种程度上有所帮助!

1
我认为在pygame中集成tkinter菜单栏是不可能的,因为它们是不同的工具。
唯一的可行方案就是使用pygame直接创建菜单栏,在屏幕主界面和菜单栏之间划分区域,并用按钮(可以用矩形或类似对象创建)来实现菜单栏。
另外一个选择是创建单独的tkinter根窗口(不建议这样做,因为你会有两个主循环相互竞争:一个来自tkinter应用程序,一个来自pygame应用程序),并将其与pygame主应用程序并行运行。
以下是一些帖子,可以帮助您了解第二种选项:
  1. 使用Tkinter和pygame时需要注意什么?

  2. Pygame应该使用哪个GUI工具包?

  3. 将GUI添加到Pygame应用程序的最佳方法是什么?

  4. 适用于Mac和Windows的易于安装并与pygame兼容的Python GUI是什么?

  5. 使用pyGame创建带有窗口菜单元素的Python游戏

显然,您还可以使用库 wxPython,它允许您在普通的 wxPython 窗口中集成一个 pygame 窗口。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接