python 窗体控件_python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)...
1 #-*- coding: UTF-8 -*-
2 #python tkinter menu
3 #python version 3.3.2
4 #EN = Window 7
5
6
7 from tkinter import *
8 from tkinter.messagebox import *
9 from tkinter.filedialog import *
10 from tkinter.colorchooser import *
11 '''
12 在python 3.3.2中,tkinter模塊可以創建一個窗口控件,如Java中的Swing13 功能描述:14 根據Python 3.3.2 IDEL的菜單,創建出一個tkinter窗口15 File-Exit : 退出功能完成16 Help-About IDEL : 打印相應信息17 其他的菜單項,當點擊時,會打印出相應菜單項的名稱18 '''
19
20 __author__ = 'Hongten'
21 MENU_ITEMS = ['File', 'Edit', 'Format', 'Run', 'Options', 'Windows', 'Help', 'Test']22 #菜單File中的選項
23 MENU_FILE_ITEMS = ['New Window Ctrl+N',24 'Open... Ctrl+O',25 'Recent Files',26 'Open Module... Alt+M',27 'Class Browser Alt+C',28 'Path Browser',29 'Save Ctrl+S',30 'Save As... Ctrl+Shift+S',31 'Save Copy As... Ctrl+Alt+S',32 'Print Window Ctrl+P',33 'Close Alt+F4',34 'Exit Ctrl+Q']35 #菜單Edit中的選項
36 MENU_EDIT_ITEMS = ['Undo Ctrl+Z',37 'Redo Ctrl+Shift+Z',38 'Cut Ctrl+X',39 'Copy Ctrl+C',40 'Paste Ctrl+V',41 'Select All Ctrl+A',42 'Find... Ctrl+F',43 'Find Again Ctrl+G',44 'Find Selections Ctrl+F3',45 'Find in Files Alt+F3',46 'Replace... Ctrl+H',47 'Go to Line Alt+G',48 'Expend Word Alt+/',49 'Show call tip Ctrl+backslash',50 'Show surerounding parens Ctrl+0',51 'Show Completions Ctrl+space']52 #菜單Format中的選項
53 MENU_FORMAT_ITEMS = ['Check Module Alt+X',54 'Ident Region Ctrl+]',55 'Dedent Region Ctrl+[',56 'Commemt Out Region Alt+3',57 'Uncomment Region Alt+4',58 'Tabify Region Alt+5',59 'Untabify Region Alt+6',60 'Toggle Tabs Alt+T',61 'New Ident Width Alt+U',62 'Format Paragraph Alt+Q',63 'Strip trailing whitespace']64 #菜單Run中的選項
65 MENU_RUN_ITEMS = [ 'Python Shell',66 'Check Module Alt+X',67 'Run Module F5']68 #菜單Options中的選項
69 MENU_OPTIONS_ITEMS = ['Config IDEL...',70 'Code Context']71 #菜單Windows中的選項
72 MENU_WINDOWS_ITEMS = ['Zoom Height Alt+2']73 #菜單Help中的選項
74 MENU_HELP_ITEMS = ['About IDEL',75 'IDEL Help',76 'Python Docs F1']77 #菜單Test中的選項
78 MENU_TEST_ITEMS = ['about',79 'askokcancel messagebox',80 'askquestion messagebox',81 'askretrycancel messagebox',82 'askyesno messagebox',83 'showerror messagebox',84 'showinfo messagebox',85 'showwarning messagebox',86 'open file',87 'save as file',88 'colorchooser']89
90 #help-About IDEL
91 ABOUT_MESSAGE = '''
92 Author : Hongten93 Author_email : hongtenzone@foxmail.com94 Blog : http://www.cnblogs.com/hongten95 QQ : 64871981996 Created : 2013-09-0597 Version : 1.098 '''
99 defget_tk():100 '''獲取一個Tk對象'''
101 returnTk()102
103 defset_tk_title(tk, title):104 '''給窗口定義title'''
105 if title is not None and title != '':106 tk.title(title)107 else:108 tk.title('Hongten v1.0')109
110 defset_tk_geometry(tk, size):111 '''設置窗口大小,size的格式為:widthxheight,如:size = '200x100'.'''
112 if size is not None and size != '':113 tk.geometry(size)114 else:115 tk.geometry('670x600')116
117 defget_menu(tk):118 '''獲取一個菜單條'''
119 returnMenu(tk)120
121 defmenu_file(menubar):122 '''定義菜單File'''
123 filemenu = Menu(menubar, tearoff=1)124 filemenu.add_command(label=MENU_FILE_ITEMS[0], command=lambda:print(MENU_FILE_ITEMS[0]))125 filemenu.add_command(label=MENU_FILE_ITEMS[1], command=lambda:print(MENU_FILE_ITEMS[1]))126 filemenu.add_command(label=MENU_FILE_ITEMS[2], command=lambda:print(MENU_FILE_ITEMS[2]))127
128 filemenu.add_command(label=MENU_FILE_ITEMS[3], command=lambda:print(MENU_FILE_ITEMS[3]))129 filemenu.add_command(label=MENU_FILE_ITEMS[4], command=lambda:print(MENU_FILE_ITEMS[4]))130 filemenu.add_command(label=MENU_FILE_ITEMS[5], command=lambda:print(MENU_FILE_ITEMS[5]))131 filemenu.add_separator()132 filemenu.add_command(label=MENU_FILE_ITEMS[6], command=lambda:print(MENU_FILE_ITEMS[6]))133 filemenu.add_command(label=MENU_FILE_ITEMS[7], command=lambda:print(MENU_FILE_ITEMS[7]))134 filemenu.add_command(label=MENU_FILE_ITEMS[8], command=lambda:print(MENU_FILE_ITEMS[8]))135 filemenu.add_separator()136 filemenu.add_command(label=MENU_FILE_ITEMS[9], command=lambda:print(MENU_FILE_ITEMS[9]))137 filemenu.add_separator()138 filemenu.add_command(label=MENU_FILE_ITEMS[10], command=lambda:print(MENU_FILE_ITEMS[10]))139 filemenu.add_command(label=MENU_FILE_ITEMS[11], command=root.destroy)140 menubar.add_cascade(label=MENU_ITEMS[0], menu=filemenu)141
142 defmenu_edit(menubar):143 '''定義菜單Edit'''
144 edit_menu = Menu(menubar, tearoff=1)145 edit_menu.add_command(label=MENU_EDIT_ITEMS[0], command=lambda:print(MENU_EDIT_ITEMS[0]))146 edit_menu.add_command(label=MENU_EDIT_ITEMS[1], command=lambda:print(MENU_EDIT_ITEMS[1]))147 edit_menu.add_separator()148 edit_menu.add_command(label=MENU_EDIT_ITEMS[2], command=lambda:print(MENU_EDIT_ITEMS[2]))149 edit_menu.add_command(label=MENU_EDIT_ITEMS[3], command=lambda:print(MENU_EDIT_ITEMS[3]))150 edit_menu.add_command(label=MENU_EDIT_ITEMS[4], command=lambda:print(MENU_EDIT_ITEMS[4]))151 edit_menu.add_command(label=MENU_EDIT_ITEMS[5], command=lambda:print(MENU_EDIT_ITEMS[5]))152 edit_menu.add_separator()153 edit_menu.add_command(label=MENU_EDIT_ITEMS[6], command=lambda:print(MENU_EDIT_ITEMS[6]))154 edit_menu.add_command(label=MENU_EDIT_ITEMS[7], command=lambda:print(MENU_EDIT_ITEMS[7]))155 edit_menu.add_command(label=MENU_EDIT_ITEMS[8], command=lambda:print(MENU_EDIT_ITEMS[8]))156 edit_menu.add_command(label=MENU_EDIT_ITEMS[9], command=lambda:print(MENU_EDIT_ITEMS[9]))157 edit_menu.add_command(label=MENU_EDIT_ITEMS[10], command=lambda:print(MENU_EDIT_ITEMS[10]))158 edit_menu.add_command(label=MENU_EDIT_ITEMS[11], command=lambda:print(MENU_EDIT_ITEMS[11]))159 edit_menu.add_command(label=MENU_EDIT_ITEMS[12], command=lambda:print(MENU_EDIT_ITEMS[12]))160 edit_menu.add_command(label=MENU_EDIT_ITEMS[13], command=lambda:print(MENU_EDIT_ITEMS[13]))161 edit_menu.add_command(label=MENU_EDIT_ITEMS[14], command=lambda:print(MENU_EDIT_ITEMS[14]))162 edit_menu.add_command(label=MENU_EDIT_ITEMS[15], command=lambda:print(MENU_EDIT_ITEMS[15]))163 menubar.add_cascade(label=MENU_ITEMS[1], menu=edit_menu)164
165 defmenu_format(menubar):166 '''定義菜單Format'''
167 format_menu = Menu(menubar, tearoff=1)168 format_menu.add_command(label=MENU_FORMAT_ITEMS[0], command=lambda:print(MENU_FORMAT_ITEMS[0]))169 format_menu.add_command(label=MENU_FORMAT_ITEMS[1], command=lambda:print(MENU_FORMAT_ITEMS[1]))170 format_menu.add_command(label=MENU_FORMAT_ITEMS[2], command=lambda:print(MENU_FORMAT_ITEMS[2]))171 format_menu.add_command(label=MENU_FORMAT_ITEMS[3], command=lambda:print(MENU_FORMAT_ITEMS[3]))172 format_menu.add_command(label=MENU_FORMAT_ITEMS[4], command=lambda:print(MENU_FORMAT_ITEMS[4]))173 format_menu.add_command(label=MENU_FORMAT_ITEMS[5], command=lambda:print(MENU_FORMAT_ITEMS[5]))174 format_menu.add_command(label=MENU_FORMAT_ITEMS[6], command=lambda:print(MENU_FORMAT_ITEMS[6]))175 format_menu.add_command(label=MENU_FORMAT_ITEMS[7], command=lambda:print(MENU_FORMAT_ITEMS[7]))176 format_menu.add_command(label=MENU_FORMAT_ITEMS[8], command=lambda:print(MENU_FORMAT_ITEMS[8]))177 format_menu.add_command(label=MENU_FORMAT_ITEMS[9], command=lambda:print(MENU_FORMAT_ITEMS[9]))178 format_menu.add_separator()179 format_menu.add_command(label=MENU_FORMAT_ITEMS[10], command=lambda:print(MENU_FORMAT_ITEMS[10]))180 menubar.add_cascade(label=MENU_ITEMS[2], menu=format_menu)181
182 defmenu_run(menubar):183 '''定義菜單Run'''
184 run_menu = Menu(menubar, tearoff=1)185 run_menu.add_command(label=MENU_RUN_ITEMS[0], command=lambda:print(MENU_RUN_ITEMS[0]))186 run_menu.add_separator()187 run_menu.add_command(label=MENU_RUN_ITEMS[1], command=lambda:print(MENU_RUN_ITEMS[1]))188 run_menu.add_command(label=MENU_RUN_ITEMS[2], command=lambda:print(MENU_RUN_ITEMS[2]))189 menubar.add_cascade(label=MENU_ITEMS[3], menu=run_menu)190
191 defmeun_options(menubar):192 '''定義菜單Options'''
193 options_menu = Menu(menubar, tearoff=1)194 options_menu.add_command(label=MENU_OPTIONS_ITEMS[0], command=lambda:print(MENU_OPTIONS_ITEMS[0]))195 options_menu.add_separator()196 options_menu.add_command(label=MENU_OPTIONS_ITEMS[1], command=lambda:print(MENU_OPTIONS_ITEMS[1]))197 menubar.add_cascade(label=MENU_ITEMS[4], menu=options_menu)198
199 defmenu_windows(menubar):200 '''定義菜單Windows'''
201 windows_menu = Menu(menubar, tearoff=1)202 windows_menu.add_command(label=MENU_WINDOWS_ITEMS[0], command=lambda:print(MENU_WINDOWS_ITEMS[0]))203 windows_menu.add_separator()204 menubar.add_cascade(label=MENU_ITEMS[5], menu=windows_menu)205
206 defmeun_help(menubar):207 '''定義菜單Help'''
208 help_menu = Menu(menubar, tearoff=1)209 help_menu.add_command(label=MENU_HELP_ITEMS[0], command=lambda:print(MENU_HELP_ITEMS[0]))210 help_menu.add_separator()211 help_menu.add_command(label=MENU_HELP_ITEMS[1], command=lambda:print(MENU_HELP_ITEMS[1]))212 help_menu.add_command(label=MENU_HELP_ITEMS[2], command=lambda:print(MENU_HELP_ITEMS[2]))213 menubar.add_cascade(label=MENU_ITEMS[6], menu=help_menu)214
215 defmeun_test(menubar):216 '''定義菜單Help'''
217 test_menu = Menu(menubar, tearoff=1)218 test_menu.add_command(label=MENU_TEST_ITEMS[0], command=about)219 test_menu.add_separator()220 test_menu.add_command(label=MENU_TEST_ITEMS[1], command=help_test1)221 test_menu.add_command(label=MENU_TEST_ITEMS[2], command=help_test2)222 test_menu.add_command(label=MENU_TEST_ITEMS[3], command=help_test3)223 test_menu.add_command(label=MENU_TEST_ITEMS[4], command=help_test4)224 test_menu.add_command(label=MENU_TEST_ITEMS[5], command=help_test5)225 test_menu.add_command(label=MENU_TEST_ITEMS[6], command=help_test6)226 test_menu.add_command(label=MENU_TEST_ITEMS[7], command=help_test7)227 test_menu.add_separator()228 test_menu.add_command(label=MENU_TEST_ITEMS[8], command=help_test8)229 test_menu.add_command(label=MENU_TEST_ITEMS[9], command=help_test9)230 test_menu.add_separator()231 test_menu.add_command(label=MENU_TEST_ITEMS[10], command=help_test10)232 menubar.add_cascade(label=MENU_ITEMS[7], menu=test_menu)233
234 ############################################################
235 #Test Menu Items Functions
236 defabout():237 '''Help-About IDEL function'''
238 label = Label(root, text=ABOUT_MESSAGE, fg='red')239 label.pack(side='top')240
241 defhelp_test1():242 ask = askokcancel('askokcancel messagebox','你確定要這樣做嗎?')243 ifask:244 #to do something
245 print('你選擇的是:確定')246 else:247 #to do something
248 print('你選擇的是:取消')249
250 defhelp_test2():251 ask = askquestion('askquestion messagebox', '你很喜歡那個女孩嗎?')252 if 'yes' ==ask:253 #to do something
254 print('是的,我很喜歡')255 elif 'no' ==ask:256 #to do something
257 print('不是這樣的,我不喜歡她')258
259 defhelp_test3():260 ask = askretrycancel('askretrycancel messagebox', '該程序被其他程序占用,請重試..')261 ifask:262 #to do something
263 print('重試')264 else:265 #to do something
266 print('取消重試')267
268 defhelp_test4():269 ask = askyesno('askyesno messagebox', 'Can you cut down a tree with a herrign?')270 print(ask)271 if 'yes' ==ask:272 #to do something
273 print('yes')274 elif 'no' ==ask:275 #to do something
276 print('no')277
278 defhelp_test5():279 error = showerror('showerror messagebox', 'ClassNotFoundException...')280 if 'ok' ==error:281 #to do something
282 print('你選擇的是確定...')283
284 defhelp_test6():285 info = showinfo('showinfo messagebox', 'This is an ex-parrot')286 if 'ok' ==info:287 #to do something
288 print('ok')289
290 defhelp_test7():291 warn = showwarning('showwarning messagebox', '內存溢出...')292 if 'ok' ==warn:293 #to do something
294 print('確認內存溢出...')295
296 defhelp_test8():297 '''打開文件'''
298 #('All files', '*')
299 openfilename = askopenfilename(filetypes=[('xml', '*.xml')])300 try:301 with open(openfilename, 'r') as fp:302 for line infp:303 print(line)304 fp.close()305 except:306 print('Could not open File:%s'%openfilename)307
308 defhelp_test9():309 '''打開文件'''
310 saveasfilename =asksaveasfilename()311 print('saves', saveasfilename.encode('utf-8'))312
313 defhelp_test10():314 '''顏色面板選擇器'''
315 color = askcolor(title='顏色面板')316 print(color)317
318 ############################################################
319 #init menu bar
320 definit_menu_bar(menubar):321 '''初始化菜單條'''
322 menu_file(menubar) #file
323 menu_edit(menubar) #edit
324 menu_format(menubar) #format
325 menu_run(menubar) #run
326 meun_options(menubar) #options
327 menu_windows(menubar) #windows
328 meun_help(menubar) #help
329 meun_test(menubar) #test
330
331 #獲得窗口對象
332 root =get_tk()333 #設置窗口大小
334 set_tk_geometry(root, '')335 #設置窗口title
336 set_tk_title(root, 'Python 3.3.2 Shell')337 #獲取菜單對象
338 menubar =get_menu(root)339 #初始化菜單
340 init_menu_bar(menubar)341 #加載菜單配置
342 root.config(menu=menubar)343
344 mainloop()
總結
以上是生活随笔為你收集整理的python 窗体控件_python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: visio 取消跨线
- 下一篇: PowerPoint2003制作抛物线动