我想在 pygame 中创建一个和 tkinter 中完全一样的 menubar。
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()
这里是添加的environ:
# 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()
希望对你有帮助!
我想它是 不 可以整合 tkinter 在门栏 pygame它们是不同的工具。
我看你唯一能做的就是直接用以下工具创建menubar pygame 您可以将屏幕的一部分用于主场景,另一部分用于menubar,menubar可以用按钮创建(用矩形或类似的对象创建)。
你还有另一个选择,那就是创建一个单独的 tkinter 根窗口(不可取,因为你会有2个主循环互相争斗:来自于 tkinter 申请和来自 pygame 应用程序),并将其与 pygame 主应用程序。
这些帖子可以帮助你选择第二种方案。
很显然,你也可以使用图书馆的 wxPython,它允许你整合一个 pygame 窗内 正常 wxPython 窗口。