python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)

在上一篇blog:python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐

中介绍了python中的tkinter的一些东西,你可能对tkinter有一定的了解了。这篇blog主要是介绍tkinter的对话框(Pop-up Dialog

查看tkinter的API的时候,接口资源:

Tkinter 8.5 reference: a GUI for Python

先来看看运行的效果:

这里主要对 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 进行增加了一个菜单选项:'Test'

1 def help_test1():
2     ask = askokcancel('askokcancel messagebox','你确定要这样做吗?')
3     if ask:
4         # to do something
5         print('你选择的是:确定')
6     else:
7         # to do something
8         print('你选择的是:取消')

运行效果:

1 def help_test2():
2     ask = askquestion('askquestion messagebox', '你很喜欢那个女孩吗?')
3     if 'yes' == ask:
4         #to do something
5         print('是的,我很喜欢')
6     elif 'no' == ask:
7         #to do something
8         print('不是这样的,我不喜欢她')

运行效果:

接下来的功能差不多...

 1 def help_test8():
 2     '''打开文件'''
 3     #('All files', '*')
 4     openfilename = askopenfilename(filetypes=[('xml', '*.xml')])
 5     try:
 6         with open(openfilename, 'r') as fp:
 7             for line in fp:
 8                 print(line)
 9             fp.close()
10     except:
11         print('Could not open File:%s'%openfilename)

运行效果:

1 def help_test10():
2     '''颜色面板选择器'''
3     color = askcolor(title='颜色面板')
4     print(color)

运行效果:

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

代码部分:

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

  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中的Swing
 13     功能描述:
 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       : Hongten
 93     Author_email : hongtenzone@foxmail.com
 94     Blog         : http://www.cnblogs.com/hongten
 95     QQ           : 648719819
 96     Created      : 2013-09-05
 97     Version      : 1.0
 98     '''
 99 def get_tk():
100     '''获取一个Tk对象'''
101     return Tk()
102 
103 def set_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 def set_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 def get_menu(tk):
118     '''获取一个菜单条'''
119     return Menu(tk)
120 
121 def menu_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 def menu_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 def menu_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 def menu_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 def meun_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 def menu_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 def meun_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 def meun_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 def about():
237     '''Help-About IDEL function'''
238     label = Label(root, text=ABOUT_MESSAGE, fg='red')
239     label.pack(side='top')
240 
241 def help_test1():
242     ask = askokcancel('askokcancel messagebox','你确定要这样做吗?')
243     if ask:
244         # to do something
245         print('你选择的是:确定')
246     else:
247         # to do something
248         print('你选择的是:取消')
249 
250 def help_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 def help_test3():
260     ask = askretrycancel('askretrycancel messagebox', '该程序被其他程序占用,请重试..')
261     if ask:
262         #to do something
263         print('重试')
264     else:
265         #to do something
266         print('取消重试')
267 
268 def help_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 def help_test5():
279     error = showerror('showerror messagebox', 'ClassNotFoundException...')
280     if 'ok' == error:
281         #to do something
282         print('你选择的是确定...')
283         
284 def help_test6():
285     info = showinfo('showinfo messagebox', 'This is an ex-parrot')
286     if 'ok' == info:
287         #to do something
288         print('ok')
289 
290 def help_test7():
291     warn = showwarning('showwarning messagebox', '内存溢出...')
292     if 'ok' == warn:
293         #to do something
294         print('确认内存溢出...')
295 
296 def help_test8():
297     '''打开文件'''
298     #('All files', '*')
299     openfilename = askopenfilename(filetypes=[('xml', '*.xml')])
300     try:
301         with open(openfilename, 'r') as fp:
302             for line in fp:
303                 print(line)
304             fp.close()
305     except:
306         print('Could not open File:%s'%openfilename)
307 
308 def help_test9():
309     '''打开文件'''
310     saveasfilename = asksaveasfilename()
311     print('saves', saveasfilename.encode('utf-8'))
312 
313 def help_test10():
314     '''颜色面板选择器'''
315     color = askcolor(title='颜色面板')
316     print(color)
317     
318 ############################################################
319 #init menu bar
320 def init_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()

 

posted @ 2013-09-06 11:24  Hongten  阅读(9454)  评论(1编辑  收藏  举报
Fork me on GitHub