Answers for "menu in python"

5

how to make menu in tkinter

from tkinter import *

def do_some():
    print('Do some!')

root = Tk()

main_menu = Menu(root)
root.config(menu=main_menu)


# Type 1 Menu
main_menu.add_command(label='File', command=do_some)
# Type 1 Menu


# Type 2 Menu
file_menu = Menu(main_menu)
main_menu.add_cascade(label='File', menu=file_menu)

file_menu.add_command(label='Save', command=do_some)
file_menu.add_command(label='Save As', command=do_some)
# Type 2 Menu


# Type 3 Menu
file_menu = Menu(main_menu)
main_menu.add_cascade(label='File', menu=file_menu)

save_menu = Menu(file_menu)
save_as_menu = Menu(file_menu)

file_menu.add_cascade(label='Save', menu=save_menu)
file_menu.add_cascade(label='Save As', menu=save_as_menu)

save_menu.add_command(label='New', command=do_some)
save_menu.add_command(label='Old', command=do_some)
save_as_menu.add_command(label='New', command=do_some)
save_as_menu.add_command(label='Old', command=do_some)
# Type 3 Menu

root.mainloop()
Posted by: Guest on July-01-2020
2

python menu

from tkinter import *
from tkinter import messagebox # just for example not necessary

# some example fungtions
def Res():
    root.geometry('333x222')
def B2N():
    root.geometry('444x444')
def Full():
    root.geometry('1000x700')
def custom_quit():
    answer=tkinter.messagebox.askokcancel("Are you Sure?","Your Data will be lost if you exit")
    if (answer):
        quit()
        
#the main window
root=Tk()
root.geometry('444x444')
text=Label(root,text='*my first menu*').pack()
# the actual menu (used a big example for better understanding of menu and sub-menu)
menu1=Menu(root)
root.configure(menu=menu1)

submenu1=Menu(menu1)
menu1.add_cascade(label='File',menu=submenu1)
sub2menu=Menu(submenu1)
sub2menu.add_command(label='Recent')
sub2menu.add_command(label='Browse')

submenu1.add_command(label='New File')
submenu1.add_cascade(label='Open File',menu=sub2menu)
submenu1.add_command(label='Save As')
submenu1.add_command(label='Print')
submenu1.add_command(label='Quit', command=custom_quit) # adding command to menu elements

submenu2=Menu(menu1)
menu1.add_cascade(label='Edit',menu=submenu2)
submenu2.add_command(label='Undo')
submenu2.add_cascade(label='Redo')
submenu2.add_command(label='Cut')
submenu2.add_command(label='Copy')
submenu2.add_command(label='Paste')
submenu2.add_command(label='Select All')

submenu3=Menu(menu1)
menu1.add_cascade(label='Format',menu=submenu3)
submenu3.add_command(label='Indent Region')
submenu3.add_cascade(label='Comment')
submenu3.add_command(label='All')
submenu3.add_command(label='Selected')
submenu3.add_command(label='Format Paragraph')

submenu4=Menu(menu1)
menu1.add_cascade(label='Run',menu=submenu4)
submenu4.add_command(label='Python Shell')
submenu4.add_cascade(label='Run Module')
submenu4.add_command(label='Check Module')

submenu5=Menu(menu1)
menu1.add_cascade(label='Options',menu=submenu5)
submenu5.add_command(label='Configure IDLE')
submenu5.add_cascade(label='Show Code Context')
submenu5.add_command(label='Zoom Height')

submenu6=Menu(menu1)
menu1.add_cascade(label='Window',menu=submenu6)
submenu6.add_command(label='Python 3.7.3 shell')
submenu6.add_cascade(label='Untitled')
submenu6.add_command(label='Resize window',command=Res)
submenu6.add_command(label='Norml size window',command=B2N)
submenu6.add_command(label='Large window',command=Full)

submenu7=Menu(menu1)
menu1.add_cascade(label='Help',menu=submenu7)
submenu7.add_command(label='About IDLE')
submenu7.add_cascade(label='IDLE Help')
submenu7.add_command(label='FAQ')
submenu7.add_command(label='Contact US')
Posted by: Guest on May-25-2021
0

python menu example

# python console helpMenu // OptionMenu
# Updated 2021 Oct 18 18:14
# Your Option Menu you can add more option for your needs.
# TODO Option Menu Below rename it to what the function will do.
# Example: your function will download something,
# Then you rename Option 1 To Download Something,

option_menu = {
	1: 'Option 1' , 
	2: 'Option 2',
	3: 'Exit' ,
}

# This function will print out your option menu
def print_options():
	
	for key in option_menu.keys():
		print(key , '---' , option_menu[key])

# Create your option functions here. / Add more for your needs
def option1():
	pass


def option2():
	pass



if __name__ == '__main__':
	
	while(True):

		print_options()

		option = ""

		try:
			# Edit 'Enter option' for your need.
			option = int(input('Enter option:'))

		except:

			print('option' , option)

		if option == 1:
			option1()
			#
			# Code here
			#
			# Also change the print massage. for your need
			print("OPTION1 WORKING....")

		elif option == 2:
			option2()
			#
			# Code here
			#
			# Also change the print massage. for your need
			print("OPTION2 WORING....")

		elif option == 3:
			# Set your Exit message here. for your need
			print("Exit code....")
			exit()

			# Set Your Error masage Here for your Need.
		else:
			print('invalid option:' , option ,'Try again...')
Posted by: Guest on October-12-2021
0

menu in python

from tkinter import (
    Tk,
    Label,
    Menu,
    messagebox,
)


# some example functions
def Res():
    root.geometry("333x222")


def B2N():
    root.geometry("444x444")


def Full():
    root.geometry("1000x700")


def custom_quit():
    answer = messagebox.askokcancel(
        "Are you Sure?", "Your Data will be lost if you exit"
    )
    if answer:
        quit()


# the main window
root = Tk()
root.geometry("444x444")
text = Label(root, text="*my first menu*").pack()
# the actual menu (used a big example for better understanding of menu and sub-menu)
menu1 = Menu(root)
root.configure(menu=menu1)

submenu1 = Menu(menu1)
menu1.add_cascade(label="File", menu=submenu1)
sub2menu = Menu(submenu1)
sub2menu.add_command(label="Recent")
sub2menu.add_command(label="Browse")

submenu1.add_command(label="New File")
submenu1.add_cascade(label="Open File", menu=sub2menu)
submenu1.add_command(label="Save As")
submenu1.add_command(label="Print")
submenu1.add_command(
    label="Quit", command=custom_quit
)  # adding command to menu elements

submenu2 = Menu(menu1)
menu1.add_cascade(label="Edit", menu=submenu2)
submenu2.add_command(label="Undo")
submenu2.add_cascade(label="Redo")
submenu2.add_command(label="Cut")
submenu2.add_command(label="Copy")
submenu2.add_command(label="Paste")
submenu2.add_command(label="Select All")

submenu3 = Menu(menu1)
menu1.add_cascade(label="Format", menu=submenu3)
submenu3.add_command(label="Indent Region")
submenu3.add_cascade(label="Comment")
submenu3.add_command(label="All")
submenu3.add_command(label="Selected")
submenu3.add_command(label="Format Paragraph")

submenu4 = Menu(menu1)
menu1.add_cascade(label="Run", menu=submenu4)
submenu4.add_command(label="Python Shell")
submenu4.add_cascade(label="Run Module")
submenu4.add_command(label="Check Module")

submenu5 = Menu(menu1)
menu1.add_cascade(label="Options", menu=submenu5)
submenu5.add_command(label="Configure IDLE")
submenu5.add_cascade(label="Show Code Context")
submenu5.add_command(label="Zoom Height")

submenu6 = Menu(menu1)
menu1.add_cascade(label="Window", menu=submenu6)
submenu6.add_command(label="Python 3.7.3 shell")
submenu6.add_cascade(label="Untitled")
submenu6.add_command(label="Resize window", command=Res)
submenu6.add_command(label="Norml size window", command=B2N)
submenu6.add_command(label="Large window", command=Full)

submenu7 = Menu(menu1)
menu1.add_cascade(label="Help", menu=submenu7)
submenu7.add_command(label="About IDLE")
submenu7.add_cascade(label="IDLE Help")
submenu7.add_command(label="FAQ")
submenu7.add_command(label="Contact US")

root.mainloop()
Posted by: Guest on August-22-2021

Python Answers by Framework

Browse Popular Code Answers by Language