Tkinter: Menu

Source: Internet
Author: User

This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811321

'''Menu of tkinter tutorials '''
'''1. Create a simple menu '''
# Add the "hello" and "quit" menus, bind the "hello" menu to the "hello" function, and bind the "quit" menu to the "root. Quit" menu.
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
Def Hello ():
Print 'Hello menu'
Menubar = menu (Root)
# Create a main menu. The callback function corresponding to each menu is hello.
For item in ['python', 'php', 'cpp ', 'C', 'java', 'javascript', 'vbscript']:
Menubar. add_command (Label = item, command = Hello)
# Set the root menu attribute to menubar
Root ['menu '] = menubar
Root. mainloop ()
# This menu has no drop-down menu and contains only two menu items

'''2. Add drop-down menu '''
From tkinter import *
Root = TK ()
Def Hello ():
Print 'Hello menu'
Menubar = menu (Root)

Filemenu = menu (menubar, tearoff = 0)
For item in ['python', 'php', 'cpp ', 'C', 'java', 'javascript', 'vbscript']:
Filemenu. add_commad (Label = item, command = Hello)
# Specify the menu attribute of menubar as filemenu, that is, filemenu as the drop-down menu of menubar.
Menubar. add_cascade (Label = 'local', menu = filemenu)
Root ['menu '] = menubar
Root. mainloop ()

''' 3. Add the checkbutton item ''' to the menu '''
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
# Print the current value of each variable each time
Def printitem ():
Print 'python = ', vpython. Get ()
Print 'php = ', vphp. Get ()
Print 'cpp = ', vcpp. Get ()
Print 'C = ', VC. Get ()
Print 'java = ', vjava. Get ()
Print 'javascript = ', vjavascript. Get ()
Print 'vbscript = ', vvbscript. Get ()

Menubar = menu (Root)

Vpython = stringvar ()
Vphp = stringvar ()
Vcpp = stringvar ()
VC = stringvar ()
Vjava = stringvar ()
Vjavascript = stringvar ()
Vvbscript = stringvar ()

Filemenu = menu (menubar, tearoff = 0)
For K, V in {'python': vpython,
'Php': vphp,
'Cpp ': vcpp,
'C': VC,
'Java': vjava,
'Javascript ': vjavascript,
'Vbscript': vvbscript}. Items ():
# Bind variables and callback Functions
Filemenu. add_checkbutton (Label = K, command = printitem, variable = V)
# Specify the menu attribute of menubar as filemenu, that is, filemenu as the drop-down menu of menubar.
Menubar. add_cascade (Label = 'local', menu = filemenu)
Root ['menu '] = menubar
Root. mainloop ()
# Run the program, use the checkbutton, and print each checkbutton in the current value through printitem.

'''4. Add the radiobutton item to the menu '''
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()

Menubar = menu (Root)
Vlang = stringvar ()
# Print the selected language each time
Def printitem ():
Print 'vlang = ', vlang. Get ()
Filemenu = menu (menubar, tearoff = 0)
For k in ['python', 'php', 'cpp ', 'C', 'java', 'javascript', 'vbscript']:
# Bind variables and callback functions. The specified variable vlang groups these items into a group.
Filemenu. add_radiobutton (Label = K, command = printitem, variable = vlang)
# Specify the menu attribute of menubar as filemenu, that is, filemenu as the drop-down menu of menubar.
Menubar. add_cascade (Label = 'local', menu = filemenu)
Root ['menu '] = menubar
Root. mainloop ()
# Each time the program prints the selected language
# Unlike checkbutton, only one in the same group is selected.

'''5. Add a separator to the menu '''
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
Menubar = menu (Root)

# Print the selected language each time
Def printitem ():
Print 'add _ separator'

Filemenu = menu (menubar, tearoff = 0)
For k in ['python', 'php', 'cpp ', 'C', 'java', 'javascript', 'vbscript']:
Filemenu. add_command (Label = K, command = printitem)
# Use separators to separate menu items
Filemenu. add_separator ()
Menubar. add_cascade (Label = 'local', menu = filemenu)
Root ['menu '] = menubar
Root. mainloop ()
# The delimiter groups related menu items. It is only implemented on the UI without any changes in the program and does not execute any commands.

'''6. Change the menu in Example 5 to right-click the pop-up menu '''
# By binding the right mouse button, this menu is displayed whenever you click it to remove the association with root.
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
Menubar = menu (Root)

Def printitem ():
Print 'popup menu'

Filemenu = menu (menubar, tearoff = 0)
For k in ['python', 'php', 'cpp ', 'C', 'java', 'javascript', 'vbscript']:
Filemenu. add_command (Label = K, command = printitem)
Filemenu. add_separator ()
Menubar. add_cascade (Label = 'local', menu = filemenu)
# In this case, do not set the root menu to menubar.
# Root ['menu '] = menubar
Def popup (event ):
# Display menu
Menubar. Post (event. x_root, event. y_root)
# Right-click the corresponding event and call popup when you right-click it. At this time, it is bound to the menu as root. You can set it to another control. Right-click the bound control to bring up the menu.
Root. BIND ('<button-3>', popup)
Root. mainloop ()
# Run a test. You can see that the functions of each menu item are usable. Therefore, the pop-up menu is the same as the general menu function, but the pop-up method is different.

'''7. The following Code demonstrates how to operate a menu item, including adding various menu items and deleting one or more menu items '''
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
Menubar = menu (Root)

Def printitem ():
Print 'add _ separator'

Filemenu = menu (menubar, tearoff = 0)
For k in range (5 ):
Filemenu. add_command (Label = STR (K), command = printitem)
Menubar. add_cascade (Label = 'local', menu = filemenu)

'''Operation for adding items to the menu '''
# Add a menu command item in index 1
Filemenu. insert_command (1, label = '201312', command = printitem)
# Add a menu checkbutton item in index 2
Filemenu. insert_checkbutton (2, label = '2013', command = printitem)
# Add a menu radiobutton item in index 3
Filemenu. insert_radiobutton (3, label = '000000', command = printitem)
# Use separators to separate newly added menu items
Filemenu. insert_separator (1)
Filemenu. insert_separator (5)

'''Operation for deleting menu items '''
# Deleting menu items of indexes 6-9
Filemenu. Delete (6, 9)
# Deleting menu items with an index of 0
Filemenu. Delete (0)

Root ['menu '] = menubar
Root. mainloop ()
# The delimiter groups related menu items. It is only implemented on the UI without any changes in the program and does not execute any commands.

Tkinter: Menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.