Different options for Python development _tkinter_ menu

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The Tkinter module of python, the menu can also be made by you to customize your style

Below is my demo

Effect of operation:

======================================

Part of the code:

======================================

 1 from tkinter import *
 2 
 3 '''
 4     Different forms of menu options
 5 '''
 6 #global var
 7 number = 0
 8 # some miscellaneous callbacks
 9 def new_file():
10     global number
11     number += 1
12     tk = Tk()
13     tk.geometry('300x300')
14     tk.title('Demo %s'%number)
15 
16 def open_file():
17     print("opening OLD file")
18 
19 def print_something():
20     print("picked a menu item")
21 
22 def makeCommandMenu():
23     # make menu button
24     Command_button = Menubutton(mBar, text='Simple Button Commands',
25                                 underline=0)
26     Command_button.pack(side=LEFT, padx="2m")
27 
28     # make the pulldown part of the File menu. The parameter passed is the master.
29     # we attach it to the button as a python attribute called "menu" by convention.
30     # hopefully this isn't too confusing...
31     Command_button.menu = Menu(Command_button)
32 
33     # just to be cute, let's disable the undo option:
34     Command_button.menu.add_command(label="Undo")
35     # undo is the 0th entry...
36     Command_button.menu.entryconfig(0, state=DISABLED)
37 
38     Command_button.menu.add_command(label='New...', underline=0,
39                                     command=new_file)
40     Command_button.menu.add_command(label='Open...', underline=0,
41                                     command=open_file)
42     Command_button.menu.add_command(label='Different Font', underline=0,
43                                     font='-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
44                                     command=print_something)
45 
46     # we can make bitmaps be menu entries too. File format is X11 bitmap.
47     # if you use XV, save it under X11 bitmap format. duh-uh.,..
48     Command_button.menu.add_command(
49         bitmap="info")
50         #bitmap='@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm')
51 
52     # this is just a line
53     Command_button.menu.add('separator')
54 
55     # change the color
56     Command_button.menu.add_command(label='Quit', underline=0,
57                                     background='red',
58                                     activebackground='green',
59                                     command=Command_button.quit)
60 
61     # set up a pointer from the file menubutton back to the file menu
62     Command_button['menu'] = Command_button.menu
63 
64     return Command_button
65 
66 #################################################
67 #### Main starts here ...
68 root = Tk()
69 root.geometry('300x300')
70 
71 
72 # make a menu bar
73 mBar = Frame(root, relief=RAISED, borderwidth=2)
74 mBar.pack(fill=X)
75 
76 Command_button = makeCommandMenu()
77 
78 
79 # finally, install the buttons in the menu bar.
80 # This allows for scanning from one menubutton to the next.
81 mBar.tk_menuBar(Command_button)
82 
83 
84 root.title('menu demo')
85 root.iconname('menu demo')
86 
87 root.mainloop()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Joseph at November 20, 2013 - 7:56 PM