import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x120")
def my_fun():
pass
menubar = tk.Menu(my_w)
menu_file = tk.Menu(menubar, tearoff=0,bg='yellow') # file
menu_edit=tk.Menu(menubar,tearoff=0) # edit menu
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line
menu_file.add_command(label="New", command=my_fun()) # Item 1 of file
menu_file.add_command(label="Open..", command=my_fun()) # Item 2
#menu_file.add_command(label="Exit", command=lambda:exit()) # Item 3
#menu_file.add_command(label="Exit", command=my_w.destroy) # Item 3
menu_file.add_command(label="Exit", command=my_w.quit) # Item 3
menu_edit.add_command(label="Undo", command=my_fun())# Item 1 of Edit
menu_edit.add_command(label="Redo", command=my_fun())# Item 2 of Edit
my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
We have added option bg='yellow'
to set background colour to Yellow. Here is the list of options we can add.
menu_file = tk.Menu(menubar, tearoff=1,bg='yellow',
activebackground='red',activeborderwidth=3,activeforeground='yellow')
tearoff=1
and then watch the border width.)
tearoff=1
and then watch the change in Cursor.)
menu_file.entryconfig(2,state='disabled')
menu_file = tk.Menu(menubar, tearoff=1,bg='yellow',
activebackground='green',activeforeground='yellow',
disabledforeground='red')
my_font1=('Times',16,'bold')
menu_file = tk.Menu(menubar, tearoff=False,bg='yellow',font=my_font1)
menu_file = tk.Menu(menubar, fg='red',tearoff=0,
bg='yellow',font=my_font1)
fg=color
will toggle the font color to red and green every 1 second.
def Change_colour(color):
if color=='green':
color='red'
else:
color='green'
menu_file.config(fg=color)
my_w.after(1000,Change_colour, color)
menubar = tk.Menu(my_w)
my_font1=('Times',18,'bold')
menu_file = tk.Menu(menubar, fg='red',tearoff=0,bg='yellow',
font=my_font1, postcommand=lambda: Change_colour('green'))
menu_file = tk.Menu(menubar,title='my title' ,
tearoff=False,fg='red',bg='yellow',selectcolor='blue')
tearoff=1
) then the menu can be taken out from the menu bar. Click the dashed line at the top to detach the menu. It can take values True or False.
menu_file = tk.Menu(menubar,title='my title' ,
tearoff=1,fg='red',bg='yellow')
menu_file = tk.Menu(menubar, title='my title',tearoff=1,bg='yellow')
menu_file.add_command(label="New", command=my_fun())
menu_file.add_radiobutton(label='Option 1',value=0,variable=optvar)
menu_file.add_checkbutton(label='Chk 1',onvalue=1,offvalue=0,variable=chkvar0)
menu_file.add_checkbutton(label='Chk 2',onvalue=1,offvalue=0,variable=chkvar1)
Checkbutton to manage text Wrap import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x120")
def my_fun():
pass
menubar = tk.Menu(my_w)
my_font1=('Times',12,'bold')
menu_file = tk.Menu(menubar,title='my title' ,
tearoff=1,fg='red',bg='yellow') # file
menu_edit=tk.Menu(menubar,tearoff=0) # edit menu
menu_sub=tk.Menu(menu_file,tearoff=0,bg='green')
menu_file.add_cascade(label='Sub 1',menu=menu_sub ) # add sub
menu_sub.add_command(label='Sub 11',command=my_fun())
menu_sub.add_command(label='Sub 12',command=my_fun())
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line
menu_file.add_command(label="New", command=my_fun()) # Item 1 of file
menu_file.add_command(label="Open..", command=my_fun()) # Item 2
menu_file.add_separator()
menu_file.add_command(label="Exit", command=my_w.quit) # Item 3
menu_edit.add_command(label="Undo", command=my_fun())# Item 1 of Edit
menu_edit.add_command(label="Redo", command=my_fun())# Item 2 of Edit
chkvar0=tk.BooleanVar(value=1) # checkbutton 1 selected
chkvar1=tk.BooleanVar()
menu_file.add_checkbutton(label='Chk 1',onvalue=1,offvalue=0,variable=chkvar0)
menu_file.add_checkbutton(label='Chk 2',onvalue=1,offvalue=0,variable=chkvar1)
optvar=tk.BooleanVar(value=1) # Radiobutton selected
menu_file.add_radiobutton(label='Option 1',value=1,variable=optvar)
my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
menubar.add_cascade(label="File", menu=menu_file)
menu_file.add_separator()
menu_file.delete(0,2)
menu_file.entryconfig(2,state='disabled')
print(menu_file.index('Chk 1')) # 8
menu_file.insert_separator(4)
menu_file.invoke(5)
print(menu_file.type(5)) # radiobutton
print(menu_file.type(7)) # separator
print(menu_file.type(9)) # checkbutton
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x120")
def my_fun():
pass
menubar = tk.Menu(my_w)
my_font1=('Times',12,'bold')
menu_file = tk.Menu(menubar,title='my title' ,
tearoff=1,fg='red',bg='yellow') # file
menu_edit=tk.Menu(menubar,tearoff=0) # edit menu
menu_sub=tk.Menu(menu_file,tearoff=0,bg='green')
menu_file.add_cascade(label='Sub 1',menu=menu_sub ) # add sub
menu_sub.add_command(label='Sub 11',command=my_fun())
menu_sub.add_command(label='Sub 12',command=my_fun())
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line
menu_file.add_command(label="New", command=my_fun()) # Item 1 of file
menu_file.add_command(label="Open..", command=my_fun()) # Item 2
menu_file.add_separator()
menu_file.add_command(label="Exit", command=my_w.quit) # Item 3
menu_edit.add_command(label="Undo", command=my_fun())# Item 1 of Edit
menu_edit.add_command(label="Redo", command=my_fun())# Item 2 of Edit
my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
menu_sub2=tk.Menu(menu_sub,tearoff=0,bg='red')
menu_sub.add_cascade(label='Sub 2',menu=menu_sub2)
menu_sub2.add_command(label='Sub 21',command=my_fun())
menu_sub2.add_command(label='Child ',command=lambda:my_child())
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("250x120")
def my_child():
my_w_child=Toplevel(my_w) # Child window
my_w_child.geometry("200x200") # Size of the window
my_w_child.title("www.plus2net.com")
my_str1 = tk.StringVar()
l1 = tk.Label(my_w_child, textvariable=my_str1 )
l1.grid(row=1,column=2)
my_str1.set("Hi I am Child window")
menubar = tk.Menu(my_w)
menu_file = tk.Menu(menubar,tearoff=0) # file
menu_edit=tk.Menu(menubar,tearoff=0) # edit menu
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line
menu_file.add_command(label="New window", command=lambda:my_child())
menu_file.add_command(label="Exit", command=my_w.quit)
my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("250x120")
def my_fun():
pass
def my_child(): # to open child window
my_w_child=Toplevel(my_w) # Child window
my_w_child.geometry("200x200") # Size of the window
my_w_child.title("www.plus2net.com")
my_str1 = tk.StringVar()
l1 = tk.Label(my_w_child, textvariable=my_str1 )
l1.grid(row=1,column=2)
my_str1.set("Hi I am Child window")
menubar = tk.Menu(my_w)
my_font1=('Times',12,'bold')
menu_file = tk.Menu(menubar,title='my title' ,
tearoff=1,fg='red',bg='yellow',disabledforeground='gray') # file
menu_edit=tk.Menu(menubar,tearoff=0) # edit menu
menu_sub=tk.Menu(menu_file,tearoff=0,bg='green')
menu_file.add_cascade(label='Sub 1',menu=menu_sub ) # add sub
menu_sub.add_command(label='Sub 11',command=my_fun())
menu_sub.add_command(label='Sub 12',command=my_fun())
menubar.add_cascade(label="File", menu=menu_file) # Top Line
menubar.add_cascade(label="Edit", menu=menu_edit) # Top Line
menu_file.add_command(label="New", command=my_fun()) # Item 1 of file
menu_file.add_command(label="Open..", command=my_fun()) # Item 2
menu_file.add_separator()
menu_file.add_command(label="Exit", command=my_w.quit) # Item 3
menu_edit.add_command(label="Undo", command=my_fun())# Item 1 of Edit
menu_edit.add_command(label="Redo", command=my_fun())# Item 2 of Edit
chkvar0=tk.BooleanVar(value=1) # checkbutton 1 selected
chkvar1=tk.BooleanVar()
menu_file.add_checkbutton(label='Chk 1',onvalue=1,offvalue=0,variable=chkvar0)
menu_file.add_checkbutton(label='Chk 2',onvalue=1,offvalue=0,variable=chkvar1)
optvar=tk.BooleanVar(value=1) # Radiobutton selected
menu_file.add_radiobutton(label='Option 1',value=1,variable=optvar)
menu_sub2=tk.Menu(menu_sub,tearoff=0,bg='red')
menu_sub.add_cascade(label='Sub 2',menu=menu_sub2)
menu_sub2.add_command(label='Sub 21',command=my_fun())
menu_sub2.add_command(label='Child ',command=lambda:my_child()) # Open child window
my_w.config(menu=menubar) # adding menu to window
my_w.mainloop()
11-04-2023 | |
always helpful |
15-10-2024 | |
Really appreciate sharing this, thanks! |