How to create this tkinter menubar for pygame
I used the os.environ to combine pyGame with tkiter, so there is a pygame window within your tkinter one:
from tkinter import *from pygame import *from pygame.locals import *import os
I added a frame for the pygame to be in:
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()
And here is the added environ:
# Tell pygame's SDL window which window ID to useos.environ['SDL_WINDOWID'] = str(embed.winfo_id())# Show the window so it's assigned an ID.root.update()# Usual pygame initializationpygame.init()# Dimensions should match those used in the embed framescreen = pygame.display.set_mode((640, 480))running = Truedef done(): global running running = Falseroot.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()
Hope it helps somehow!
I think it is not possible to integrate a tkinter menubar in pygame, they are different tools.
The only thing I see you can do is to create directly the menubar with pygame , using just one part of the screen for the main scene and the other for the menubar, which could be created with buttons (created with rectangles or similar objects).
You have also another option, that is to create a separate tkinter root window (not advisable, because you would have 2 main loops fighting one another: the one from the tkinter application and the one from the pygame application), and run it in parallel with the pygame main application.
These are some posts that could help you for the second option:
Is there anything I need aware of using Tkinter and pygame together?
Mac and Windows compatible GUI for Python, which is easy to install and works with pygame?
Apparently, you could also use the library wxPython, which allows you to integrate a pygame window inside of a normal wxPython window.