1from tkinter import *
2from tkinter import messagebox # just for example not necessary
3
4# some example fungtions
5def Res():
6 root.geometry('333x222')
7def B2N():
8 root.geometry('444x444')
9def Full():
10 root.geometry('1000x700')
11def custom_quit():
12 answer=tkinter.messagebox.askokcancel("Are you Sure?","Your Data will be lost if you exit")
13 if (answer):
14 quit()
15
16#the main window
17root=Tk()
18root.geometry('444x444')
19text=Label(root,text='*my first menu*').pack()
20# the actual menu (used a big example for better understanding of menu and sub-menu)
21menu1=Menu(root)
22root.configure(menu=menu1)
23
24submenu1=Menu(menu1)
25menu1.add_cascade(label='File',menu=submenu1)
26sub2menu=Menu(submenu1)
27sub2menu.add_command(label='Recent')
28sub2menu.add_command(label='Browse')
29
30submenu1.add_command(label='New File')
31submenu1.add_cascade(label='Open File',menu=sub2menu)
32submenu1.add_command(label='Save As')
33submenu1.add_command(label='Print')
34submenu1.add_command(label='Quit', command=custom_quit) # adding command to menu elements
35
36submenu2=Menu(menu1)
37menu1.add_cascade(label='Edit',menu=submenu2)
38submenu2.add_command(label='Undo')
39submenu2.add_cascade(label='Redo')
40submenu2.add_command(label='Cut')
41submenu2.add_command(label='Copy')
42submenu2.add_command(label='Paste')
43submenu2.add_command(label='Select All')
44
45submenu3=Menu(menu1)
46menu1.add_cascade(label='Format',menu=submenu3)
47submenu3.add_command(label='Indent Region')
48submenu3.add_cascade(label='Comment')
49submenu3.add_command(label='All')
50submenu3.add_command(label='Selected')
51submenu3.add_command(label='Format Paragraph')
52
53submenu4=Menu(menu1)
54menu1.add_cascade(label='Run',menu=submenu4)
55submenu4.add_command(label='Python Shell')
56submenu4.add_cascade(label='Run Module')
57submenu4.add_command(label='Check Module')
58
59submenu5=Menu(menu1)
60menu1.add_cascade(label='Options',menu=submenu5)
61submenu5.add_command(label='Configure IDLE')
62submenu5.add_cascade(label='Show Code Context')
63submenu5.add_command(label='Zoom Height')
64
65submenu6=Menu(menu1)
66menu1.add_cascade(label='Window',menu=submenu6)
67submenu6.add_command(label='Python 3.7.3 shell')
68submenu6.add_cascade(label='Untitled')
69submenu6.add_command(label='Resize window',command=Res)
70submenu6.add_command(label='Norml size window',command=B2N)
71submenu6.add_command(label='Large window',command=Full)
72
73submenu7=Menu(menu1)
74menu1.add_cascade(label='Help',menu=submenu7)
75submenu7.add_command(label='About IDLE')
76submenu7.add_cascade(label='IDLE Help')
77submenu7.add_command(label='FAQ')
78submenu7.add_command(label='Contact US')
79
80
1from tkinter import *
2
3def do_some():
4 print('Do some!')
5
6root = Tk()
7
8main_menu = Menu(root)
9root.config(menu=main_menu)
10
11
12# Type 1 Menu
13main_menu.add_command(label='File', command=do_some)
14# Type 1 Menu
15
16
17# Type 2 Menu
18file_menu = Menu(main_menu)
19main_menu.add_cascade(label='File', menu=file_menu)
20
21file_menu.add_command(label='Save', command=do_some)
22file_menu.add_command(label='Save As', command=do_some)
23# Type 2 Menu
24
25
26# Type 3 Menu
27file_menu = Menu(main_menu)
28main_menu.add_cascade(label='File', menu=file_menu)
29
30save_menu = Menu(file_menu)
31save_as_menu = Menu(file_menu)
32
33file_menu.add_cascade(label='Save', menu=save_menu)
34file_menu.add_cascade(label='Save As', menu=save_as_menu)
35
36save_menu.add_command(label='New', command=do_some)
37save_menu.add_command(label='Old', command=do_some)
38save_as_menu.add_command(label='New', command=do_some)
39save_as_menu.add_command(label='Old', command=do_some)
40# Type 3 Menu
41
42root.mainloop()
1# python console helpMenu // OptionMenu
2# Easy to read
3
4menu_options = {
5 1: 'Name your option here' ,
6 2: 'Name your option here' ,
7 3: 'Name your option here' ,
8 4: 'Exit' ,
9}
10print('\n')
11def print_menu():
12
13 for key in menu_options.keys():
14 print(key , '--' , menu_options [key] )
15
16def option1():
17 print('Option description Here')
18
19def option2():
20 print('Option description Here')
21
22
23def option3():
24 print('Option description Here')
25
26
27if __name__ == '__main__':
28
29 while(True):
30
31 print_menu()
32
33 option = ""
34
35 try:
36
37 option = int(input('Enter Your Chouce: '))
38
39 except:
40
41 print('Selected:' , option)
42
43 if option == 1:
44 option1()
45
46 #
47 # Code here
48 #
49
50 elif option == 2:
51 option2()
52
53 #
54 # Code here
55 #
56
57 elif option == 3:
58 option3()
59
60 # Code here
61 #
62 #
63
64 elif option == 4:
65 print('Exit the program...')
66
67 exit()
68
69 else:
70 print('\n')
71 print('Error Message Here')
72
1from tkinter import (
2 Tk,
3 Label,
4 Menu,
5 messagebox,
6)
7
8
9# some example functions
10def Res():
11 root.geometry("333x222")
12
13
14def B2N():
15 root.geometry("444x444")
16
17
18def Full():
19 root.geometry("1000x700")
20
21
22def custom_quit():
23 answer = messagebox.askokcancel(
24 "Are you Sure?", "Your Data will be lost if you exit"
25 )
26 if answer:
27 quit()
28
29
30# the main window
31root = Tk()
32root.geometry("444x444")
33text = Label(root, text="*my first menu*").pack()
34# the actual menu (used a big example for better understanding of menu and sub-menu)
35menu1 = Menu(root)
36root.configure(menu=menu1)
37
38submenu1 = Menu(menu1)
39menu1.add_cascade(label="File", menu=submenu1)
40sub2menu = Menu(submenu1)
41sub2menu.add_command(label="Recent")
42sub2menu.add_command(label="Browse")
43
44submenu1.add_command(label="New File")
45submenu1.add_cascade(label="Open File", menu=sub2menu)
46submenu1.add_command(label="Save As")
47submenu1.add_command(label="Print")
48submenu1.add_command(
49 label="Quit", command=custom_quit
50) # adding command to menu elements
51
52submenu2 = Menu(menu1)
53menu1.add_cascade(label="Edit", menu=submenu2)
54submenu2.add_command(label="Undo")
55submenu2.add_cascade(label="Redo")
56submenu2.add_command(label="Cut")
57submenu2.add_command(label="Copy")
58submenu2.add_command(label="Paste")
59submenu2.add_command(label="Select All")
60
61submenu3 = Menu(menu1)
62menu1.add_cascade(label="Format", menu=submenu3)
63submenu3.add_command(label="Indent Region")
64submenu3.add_cascade(label="Comment")
65submenu3.add_command(label="All")
66submenu3.add_command(label="Selected")
67submenu3.add_command(label="Format Paragraph")
68
69submenu4 = Menu(menu1)
70menu1.add_cascade(label="Run", menu=submenu4)
71submenu4.add_command(label="Python Shell")
72submenu4.add_cascade(label="Run Module")
73submenu4.add_command(label="Check Module")
74
75submenu5 = Menu(menu1)
76menu1.add_cascade(label="Options", menu=submenu5)
77submenu5.add_command(label="Configure IDLE")
78submenu5.add_cascade(label="Show Code Context")
79submenu5.add_command(label="Zoom Height")
80
81submenu6 = Menu(menu1)
82menu1.add_cascade(label="Window", menu=submenu6)
83submenu6.add_command(label="Python 3.7.3 shell")
84submenu6.add_cascade(label="Untitled")
85submenu6.add_command(label="Resize window", command=Res)
86submenu6.add_command(label="Norml size window", command=B2N)
87submenu6.add_command(label="Large window", command=Full)
88
89submenu7 = Menu(menu1)
90menu1.add_cascade(label="Help", menu=submenu7)
91submenu7.add_command(label="About IDLE")
92submenu7.add_cascade(label="IDLE Help")
93submenu7.add_command(label="FAQ")
94submenu7.add_command(label="Contact US")
95
96root.mainloop()
97