How can I add the Manu bar in my custom TKinter window?
I want to add a menu bar in my custom TKinter window.
CodePudding user response:
import tkinter as tk
root = tk.Tk()
root.title("My Window")
menu_bar = tk.Menu(root)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_program)
menu_bar.add_cascade(label="File", menu=file_menu)
edit_menu = tk.Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Undo", command=undo)
edit_menu.add_command(label="Redo", command=redo)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
root.config(menu=menu_bar)
root.mainloop()
Unless you have something specific you want to achieve, this is how i would do it
CodePudding user response:
Entry level for newbie.
Is this what you want, black on the background on menubar
?
import tkinter as tk
app = tk.Tk()
app.title("how to create menu bar color in tkinter")
app.geometry("800x500")
menubar = tk.Menu(app, background='black', fg='white')
file = tk.Menu(menubar, tearoff=False, background='yellow', fg='red')
edit = tk.Menu(menubar, tearoff=False, background='pink', fg='green')
file.add_command(label="New")
file.add_command(label="Exit", command=app.quit)
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
menubar.add_cascade(label="File", menu=file)
menubar.add_cascade(label="Edit", menu=edit)
app.config(menu=menubar)
app.mainloop()
Intermediate level:"
import tkinter as tk
class MenuBar(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master, bd=1, relief='raised')
self.master=master
self.configure(background='black',
cursor='hand2')
file = tk.Menubutton(self, text='File',
background='black',
foreground='red',
activeforeground='black',
activebackground='white'
)
file_menu = tk.Menu(file,tearoff=0)
file_menu.add_command(label='save',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
file.config(menu=file_menu)
file.pack(side='left')
edit = tk.Menubutton(self, text='Edit',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
edit_menu = tk.Menu(edit,tearoff=0)
edit_menu.add_command(label='add',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
edit.config(menu=edit_menu)
edit.pack(side='left')
close = tk.Button(self, text='X',
background='black',
foreground='white')
close.pack(side='right')
root = tk.Tk()
menubar = MenuBar(root)
menubar.pack(side='top', fill='x')
root.mainloop()
Result screenshot: