python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐
我使用的python版本为:3.3.2
如果你对python中tkinter模块的菜单操作不是很了解,你可以看看:
python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐
python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
如果你了解一些tkinter菜单操作,也许这篇blog对你有所帮助:
运行效果:
1.英文效果:
只需要修改如下代码,就可以实现中英文转换:
1 #选择中英文 2 if not FLAG: 3 MENU_BAR = CN_MENU_ITEMS 4 MENU_FILE_ITEMS = CN_FILE_MENU_ITEMS 5 MENU_EDIT_ITEMS = CN_EDIT_MENU_ITEMS 6 else: 7 MENU_BAR = EN_MENU_ITEMS 8 MENU_FILE_ITEMS = EN_FILE_MENU_ITEMS 9 MENU_EDIT_ITEMS = EN_EDIT_MENU_ITEMS
2.中文效果:
=================================================
代码部分:
=================================================
1 #python tkinter menu 2 3 from tkinter import * 4 5 # some vocabulary to keep from getting confused. This terminology 6 # is something I cooked up for this file, but follows the man pages 7 # pretty closely 8 # 9 # 10 # 11 # This is a MENUBUTTON 12 # V 13 # +-------------+ 14 # | | 15 # 16 # +------------++------------++------------+ 17 # | || || | 18 # | File || Edit || Options | <-------- the MENUBAR 19 # | || || | 20 # +------------++------------++------------+ 21 # | New... | 22 # | Open... | 23 # | Print | 24 # | | <------ This is a MENU. The lines of text in the menu are 25 # | | MENU ENTRIES 26 # | +---------------+ 27 # | Open Files > | file1 | 28 # | | file2 | 29 # | | another file | <------ this cascading part is also a MENU 30 # +----------------| | 31 # | | 32 # | | 33 # | | 34 # +---------------+ 35 36 __author__ = {'name' : 'Hongten', 37 'Email' : 'hongtenzone@foxmail.com', 38 'Blog' : 'http://www.cnblogs.com/hongten', 39 'QQ' : '648719819', 40 'Created' : '2013-09-10'} 41 42 #global var 43 #flag 44 FLAG = True 45 MENU_BAR = [] 46 MENU_FILE_ITEMS = [] 47 MENU_EDIT_ITEMS = [] 48 49 #菜单条选项 50 CN_MENU_ITEMS = ['文件', '编辑'] 51 EN_MENU_ITEMS = ['File', 'Edit'] 52 53 #File菜单选项子菜单 54 CN_FILE_MENU_ITEMS = ['创建...', '打开...', '退出'] 55 EN_FILE_MENU_ITEMS = ['New...', 'Open...', 'Quit'] 56 57 #Edit菜单选项子菜单 58 CN_EDIT_MENU_ITEMS = ['不可用', '剪切', '复制', '粘贴'] 59 EN_EDIT_MENU_ITEMS = ['Undo', 'Cut', 'Copy', 'Paste'] 60 61 def init(): 62 global FLAG 63 FLAG = True 64 global MENU_BAR 65 global MENU_FILE_ITEMS 66 global MENU_EDIT_ITEMS 67 #选择中英文 68 if not FLAG: 69 MENU_BAR = CN_MENU_ITEMS 70 MENU_FILE_ITEMS = CN_FILE_MENU_ITEMS 71 MENU_EDIT_ITEMS = CN_EDIT_MENU_ITEMS 72 else: 73 MENU_BAR = EN_MENU_ITEMS 74 MENU_FILE_ITEMS = EN_FILE_MENU_ITEMS 75 MENU_EDIT_ITEMS = EN_EDIT_MENU_ITEMS 76 77 root = Tk() 78 init() 79 root.geometry('250x200') 80 root.title('menu demo') 81 root.iconname('packer') 82 83 def new_file(): 84 print('File -- New...') 85 86 def open_file(): 87 print('File -- Open...') 88 89 def create_file_menu(): 90 '''创建Fiel菜单项,在File菜单中包含: 91 New... 92 Open... 93 Quit 94 等子菜单。''' 95 file_item = Menubutton(menu_bar, text=MENU_BAR[0], underline=0) 96 file_item.pack(side=LEFT, padx='1m') 97 file_item.menu = Menu(file_item) 98 99 file_item.menu.add_command(label=MENU_FILE_ITEMS[0], underline=0, command=new_file) 100 file_item.menu.add_command(label=MENU_FILE_ITEMS[1], underline=0, command=open_file) 101 file_item.menu.add_command(label=MENU_FILE_ITEMS[2], underline=0, command=root.destroy) 102 file_item['menu'] = file_item.menu 103 return file_item 104 105 def create_edit_menu(): 106 '''创建Edit菜单项,在Edit菜单中包含: 107 Undo 108 Cut 109 Copy 110 Paste 111 等子菜单。''' 112 edit_item = Menubutton(menu_bar, text=MENU_BAR[1], underline=1) 113 edit_item.pack(side=LEFT, padx='1m') 114 edit_item.menu = Menu(edit_item) 115 #不可用状态 116 edit_item.menu.add('command', label=MENU_EDIT_ITEMS[0]) 117 edit_item.menu.entryconfig(1, state=DISABLED) 118 119 edit_item.menu.add_command(label=MENU_EDIT_ITEMS[1]) 120 edit_item.menu.add_command(label=MENU_EDIT_ITEMS[2]) 121 edit_item.menu.add_command(label=MENU_EDIT_ITEMS[3]) 122 edit_item['menu'] = edit_item.menu 123 return edit_item 124 125 menu_bar = Frame(root, relief=RAISED, borderwidth=2) 126 menu_bar.pack(fill=X) 127 128 #创建File,Edit菜单项 129 file_meun = create_file_menu() 130 edit_menu = create_edit_menu() 131 132 #加载File,Edit菜单项到窗口的菜单栏 133 menu_bar.tk_menuBar(file_meun, edit_menu) 134 135 root.mainloop()
参考资料:
http://www.oschina.net/code/explore/Python-3.1.3/Demo/tkinter/matt/menu-simple.py