我使用的python版本为:3.3.2
如果你对python中tkinter模块的菜单操作不是很了解,你可以看看:
python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐
python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
如果你了解一些tkinter菜单操作,也许这篇blog对你有所帮助:
运行效果:
1.英文效果:
只需要修改如下代码,就可以实现中英文转换:
#选择中英文
if not FLAG:
MENU_BAR = CN_MENU_ITEMS
MENU_FILE_ITEMS = CN_FILE_MENU_ITEMS
MENU_EDIT_ITEMS = CN_EDIT_MENU_ITEMS
else:
MENU_BAR = EN_MENU_ITEMS
MENU_FILE_ITEMS = EN_FILE_MENU_ITEMS
MENU_EDIT_ITEMS = EN_EDIT_MENU_ITEMS
2.中文效果:
=================================================
代码部分:
=================================================
#python tkinter menu from tkinter import * # some vocabulary to keep from getting confused. This terminology
# is something I cooked up for this file, but follows the man pages
# pretty closely
#
#
#
# This is a MENUBUTTON
# V
# +-------------+
# | |
#
# +------------++------------++------------+
# | || || |
# | File || Edit || Options | <-------- the MENUBAR
# | || || |
# +------------++------------++------------+
# | New... |
# | Open... |
# | Print |
# | | <------ This is a MENU. The lines of text in the menu are
# | | MENU ENTRIES
# | +---------------+
# | Open Files > | file1 |
# | | file2 |
# | | another file | <------ this cascading part is also a MENU
# +----------------| |
# | |
# | |
# | |
# +---------------+ __author__ = {'name' : 'Hongten',
'Email' : 'hongtenzone@foxmail.com',
'Blog' : 'http://www.cnblogs.com/hongten',
'QQ' : '',
'Created' : '2013-09-10'} #global var
#flag
FLAG = True
MENU_BAR = []
MENU_FILE_ITEMS = []
MENU_EDIT_ITEMS = [] #菜单条选项
CN_MENU_ITEMS = ['文件', '编辑']
EN_MENU_ITEMS = ['File', 'Edit'] #File菜单选项子菜单
CN_FILE_MENU_ITEMS = ['创建...', '打开...', '退出']
EN_FILE_MENU_ITEMS = ['New...', 'Open...', 'Quit'] #Edit菜单选项子菜单
CN_EDIT_MENU_ITEMS = ['不可用', '剪切', '复制', '粘贴']
EN_EDIT_MENU_ITEMS = ['Undo', 'Cut', 'Copy', 'Paste'] def init():
global FLAG
FLAG = True
global MENU_BAR
global MENU_FILE_ITEMS
global MENU_EDIT_ITEMS
#选择中英文
if not FLAG:
MENU_BAR = CN_MENU_ITEMS
MENU_FILE_ITEMS = CN_FILE_MENU_ITEMS
MENU_EDIT_ITEMS = CN_EDIT_MENU_ITEMS
else:
MENU_BAR = EN_MENU_ITEMS
MENU_FILE_ITEMS = EN_FILE_MENU_ITEMS
MENU_EDIT_ITEMS = EN_EDIT_MENU_ITEMS root = Tk()
init()
root.geometry('250x200')
root.title('menu demo')
root.iconname('packer') def new_file():
print('File -- New...') def open_file():
print('File -- Open...') def create_file_menu():
'''创建Fiel菜单项,在File菜单中包含:
New...
Open...
Quit
等子菜单。'''
file_item = Menubutton(menu_bar, text=MENU_BAR[0], underline=0)
file_item.pack(side=LEFT, padx='1m')
file_item.menu = Menu(file_item) file_item.menu.add_command(label=MENU_FILE_ITEMS[0], underline=0, command=new_file)
file_item.menu.add_command(label=MENU_FILE_ITEMS[1], underline=0, command=open_file)
file_item.menu.add_command(label=MENU_FILE_ITEMS[2], underline=0, command=root.destroy)
file_item['menu'] = file_item.menu
return file_item def create_edit_menu():
'''创建Edit菜单项,在Edit菜单中包含:
Undo
Cut
Copy
Paste
等子菜单。'''
edit_item = Menubutton(menu_bar, text=MENU_BAR[1], underline=1)
edit_item.pack(side=LEFT, padx='1m')
edit_item.menu = Menu(edit_item)
#不可用状态
edit_item.menu.add('command', label=MENU_EDIT_ITEMS[0])
edit_item.menu.entryconfig(1, state=DISABLED) edit_item.menu.add_command(label=MENU_EDIT_ITEMS[1])
edit_item.menu.add_command(label=MENU_EDIT_ITEMS[2])
edit_item.menu.add_command(label=MENU_EDIT_ITEMS[3])
edit_item['menu'] = edit_item.menu
return edit_item menu_bar = Frame(root, relief=RAISED, borderwidth=2)
menu_bar.pack(fill=X) #创建File,Edit菜单项
file_meun = create_file_menu()
edit_menu = create_edit_menu() #加载File,Edit菜单项到窗口的菜单栏
menu_bar.tk_menuBar(file_meun, edit_menu) root.mainloop()
参考资料:
http://www.oschina.net/code/explore/Python-3.1.3/Demo/tkinter/matt/menu-simple.py