gpt4 book ai didi

python-3.x - 如何为 pygame 创建这个 tkinter 菜单栏

转载 作者:行者123 更新时间:2023-12-05 01:47:19 24 4
gpt4 key购买 nike

我想在 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 中创建以完全相同的方式执行此操作的东西吗?

谢谢。

最佳答案

我使用 os.environ 将 pyGame 与 tkiter 结合起来,所以在你的 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()

希望对您有所帮助!

关于python-3.x - 如何为 pygame 创建这个 tkinter 菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872605/

24 4 0