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

在了解python中的tkinter模块的时候,你需要了解一些tkinter的相关知识

下面是python的API文档中的一个简单例子:

 1 import tkinter as tk
 2 
 3 class Application(tk.Frame):
 4     def __init__(self, master=None):
 5         tk.Frame.__init__(self, master)
 6         self.pack()
 7         self.createWidgets()
 8 
 9     def createWidgets(self):
10         self.hi_there = tk.Button(self)
11         self.hi_there["text"] = "Hello World\n(click me)"
12         self.hi_there["command"] = self.say_hi
13         self.hi_there.pack(side="top")
14 
15         self.QUIT = tk.Button(self, text="QUIT", fg="red",
16                                             command=root.destroy)
17         self.QUIT.pack(side="bottom")
18 
19     def say_hi(self):
20         print("hi there, everyone!")
21 
22 root = tk.Tk()
23 app = Application(master=root)
24 app.mainloop()

运行效果

当点击'Hello World(click me)'按钮的时候,打印出:'hi there, everyone!'

当点击'QUIT'按钮的时候,退出窗口程序

我想现在你对python的tkinter模块有一点的认识了...这是好事儿...

下面是我做的demo

运行效果

1.点击‘New Window’菜单选项,打印出:'New Window       Ctrl+N'信息

2.点击''About IDEL'菜单选项,在窗体中打印作者相关信息

3.点击‘Exit’菜单项,退出窗口程序

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

代码部分:

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

  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 '''
  9     在python 3.3.2中,tkinter模块可以创建一个窗口控件,如Java中的Swing
 10     功能描述:
 11         根据Python 3.3.2 IDEL的菜单,创建出一个tkinter窗口
 12         File-Exit    :  退出功能完成
 13         Help-About IDEL     : 打印相应信息
 14         其他的菜单项,当点击时,会打印出相应菜单项的名称
 15 '''
 16 
 17 __author__ = 'Hongten'
 18 MENU_ITEMS = ['File', 'Edit', 'Format', 'Run', 'Options', 'Windows', 'Help']
 19 #菜单File中的选项
 20 MENU_FILE_ITEMS = ['New Window      Ctrl+N      ',
 21                    'Open...         Ctrl+O      ',
 22                    'Recent Files                ',
 23                    'Open Module...  Alt+M       ',
 24                    'Class Browser   Alt+C       ',
 25                    'Path Browser                ',
 26                    'Save            Ctrl+S      ',
 27                    'Save As...      Ctrl+Shift+S',
 28                    'Save Copy As... Ctrl+Alt+S  ',
 29                    'Print Window    Ctrl+P      ',
 30                    'Close           Alt+F4      ',
 31                    'Exit            Ctrl+Q      ']
 32 #菜单Edit中的选项
 33 MENU_EDIT_ITEMS = ['Undo                     Ctrl+Z        ',
 34                    'Redo                     Ctrl+Shift+Z  ',
 35                    'Cut                      Ctrl+X        ',
 36                    'Copy                     Ctrl+C        ',
 37                    'Paste                    Ctrl+V        ',
 38                    'Select All               Ctrl+A        ',
 39                    'Find...                  Ctrl+F        ',
 40                    'Find Again               Ctrl+G        ',
 41                    'Find Selections          Ctrl+F3       ',
 42                    'Find in Files            Alt+F3        ',
 43                    'Replace...               Ctrl+H        ',
 44                    'Go to Line               Alt+G         ',
 45                    'Expend Word              Alt+/         ',
 46                    'Show call tip            Ctrl+backslash',
 47                    'Show surerounding parens Ctrl+0        ',
 48                    'Show Completions         Ctrl+space    ']
 49 #菜单Format中的选项
 50 MENU_FORMAT_ITEMS = ['Check Module        Alt+X   ',
 51                    'Ident Region        Ctrl+]  ',
 52                    'Dedent Region       Ctrl+[  ',
 53                    'Commemt Out Region  Alt+3   ',
 54                    'Uncomment Region    Alt+4   ',
 55                    'Tabify Region       Alt+5   ',
 56                    'Untabify Region     Alt+6   ',
 57                    'Toggle Tabs         Alt+T   ',
 58                    'New Ident Width     Alt+U   ',
 59                    'Format Paragraph    Alt+Q   ',
 60                    'Strip trailing whitespace   ']
 61 #菜单Run中的选项
 62 MENU_RUN_ITEMS = [ 'Python Shell                ',
 63                    'Check Module    Alt+X       ',
 64                    'Run Module      F5          ']
 65 #菜单Options中的选项
 66 MENU_OPTIONS_ITEMS = ['Config IDEL...              ',
 67                    'Code Context                ']
 68 #菜单Windows中的选项
 69 MENU_WINDOWS_ITEMS = ['Zoom Height     Alt+2       ']
 70 #菜单Help中的选项
 71 MENU_HELP_ITEMS = ['About IDEL                  ',
 72                    'IDEL Help                   ',
 73                    'Python Docs     F1          ']
 74 #help-About IDEL
 75 ABOUT_MESSAGE = '''
 76     Author       : Hongten
 77     Author_email : hongtenzone@foxmail.com
 78     Blog         : http://www.cnblogs.com/hongten
 79     QQ           : 648719819
 80     Created      : 2013-09-05
 81     Version      : 1.0
 82     '''
 83 def get_tk():
 84     '''获取一个Tk对象'''
 85     return Tk()
 86 
 87 def set_tk_title(tk, title):
 88     '''给窗口定义title'''
 89     if title is not None and title != '':
 90         tk.title(title)
 91     else:
 92         tk.title('Hongten v1.0')
 93 
 94 def set_tk_geometry(tk, size):
 95     '''设置窗口大小,size的格式为:widthxheight,如:size = '200x100'.'''
 96     if size is not None and size != '':
 97         tk.geometry(size)
 98     else:
 99         tk.geometry('670x600')
100 
101 def get_menu(tk):
102     '''获取一个菜单条'''
103     return Menu(tk)
104 
105 def menu_file(menubar):
106     '''定义菜单File'''
107     filemenu = Menu(menubar, tearoff=1)
108     filemenu.add_command(label=MENU_FILE_ITEMS[0], command=lambda:print(MENU_FILE_ITEMS[0]))
109     filemenu.add_command(label=MENU_FILE_ITEMS[1], command=lambda:print(MENU_FILE_ITEMS[1]))
110     filemenu.add_command(label=MENU_FILE_ITEMS[2], command=lambda:print(MENU_FILE_ITEMS[2]))
111     
112     filemenu.add_command(label=MENU_FILE_ITEMS[3], command=lambda:print(MENU_FILE_ITEMS[3]))
113     filemenu.add_command(label=MENU_FILE_ITEMS[4], command=lambda:print(MENU_FILE_ITEMS[4]))
114     filemenu.add_command(label=MENU_FILE_ITEMS[5], command=lambda:print(MENU_FILE_ITEMS[5]))
115     filemenu.add_separator()
116     filemenu.add_command(label=MENU_FILE_ITEMS[6], command=lambda:print(MENU_FILE_ITEMS[6]))
117     filemenu.add_command(label=MENU_FILE_ITEMS[7], command=lambda:print(MENU_FILE_ITEMS[7]))
118     filemenu.add_command(label=MENU_FILE_ITEMS[8], command=lambda:print(MENU_FILE_ITEMS[8]))
119     filemenu.add_separator()
120     filemenu.add_command(label=MENU_FILE_ITEMS[9], command=lambda:print(MENU_FILE_ITEMS[9]))
121     filemenu.add_separator()
122     filemenu.add_command(label=MENU_FILE_ITEMS[10], command=lambda:print(MENU_FILE_ITEMS[10]))
123     filemenu.add_command(label=MENU_FILE_ITEMS[11], command=root.destroy)
124     menubar.add_cascade(label=MENU_ITEMS[0], menu=filemenu)
125 
126 def menu_edit(menubar):
127     '''定义菜单Edit'''
128     edit_menu = Menu(menubar, tearoff=1)
129     edit_menu.add_command(label=MENU_EDIT_ITEMS[0], command=lambda:print(MENU_EDIT_ITEMS[0]))
130     edit_menu.add_command(label=MENU_EDIT_ITEMS[1], command=lambda:print(MENU_EDIT_ITEMS[1]))
131     edit_menu.add_separator()
132     edit_menu.add_command(label=MENU_EDIT_ITEMS[2], command=lambda:print(MENU_EDIT_ITEMS[2]))
133     edit_menu.add_command(label=MENU_EDIT_ITEMS[3], command=lambda:print(MENU_EDIT_ITEMS[3]))
134     edit_menu.add_command(label=MENU_EDIT_ITEMS[4], command=lambda:print(MENU_EDIT_ITEMS[4]))
135     edit_menu.add_command(label=MENU_EDIT_ITEMS[5], command=lambda:print(MENU_EDIT_ITEMS[5]))
136     edit_menu.add_separator()
137     edit_menu.add_command(label=MENU_EDIT_ITEMS[6], command=lambda:print(MENU_EDIT_ITEMS[6]))
138     edit_menu.add_command(label=MENU_EDIT_ITEMS[7], command=lambda:print(MENU_EDIT_ITEMS[7]))
139     edit_menu.add_command(label=MENU_EDIT_ITEMS[8], command=lambda:print(MENU_EDIT_ITEMS[8]))
140     edit_menu.add_command(label=MENU_EDIT_ITEMS[9], command=lambda:print(MENU_EDIT_ITEMS[9]))
141     edit_menu.add_command(label=MENU_EDIT_ITEMS[10], command=lambda:print(MENU_EDIT_ITEMS[10]))
142     edit_menu.add_command(label=MENU_EDIT_ITEMS[11], command=lambda:print(MENU_EDIT_ITEMS[11]))
143     edit_menu.add_command(label=MENU_EDIT_ITEMS[12], command=lambda:print(MENU_EDIT_ITEMS[12]))
144     edit_menu.add_command(label=MENU_EDIT_ITEMS[13], command=lambda:print(MENU_EDIT_ITEMS[13]))
145     edit_menu.add_command(label=MENU_EDIT_ITEMS[14], command=lambda:print(MENU_EDIT_ITEMS[14]))
146     edit_menu.add_command(label=MENU_EDIT_ITEMS[15], command=lambda:print(MENU_EDIT_ITEMS[15]))
147     menubar.add_cascade(label=MENU_ITEMS[1], menu=edit_menu)
148 
149 def menu_format(menubar):
150     '''定义菜单Format'''
151     format_menu = Menu(menubar, tearoff=1)
152     format_menu.add_command(label=MENU_FORMAT_ITEMS[0], command=lambda:print(MENU_FORMAT_ITEMS[0]))
153     format_menu.add_command(label=MENU_FORMAT_ITEMS[1], command=lambda:print(MENU_FORMAT_ITEMS[1]))
154     format_menu.add_command(label=MENU_FORMAT_ITEMS[2], command=lambda:print(MENU_FORMAT_ITEMS[2]))
155     format_menu.add_command(label=MENU_FORMAT_ITEMS[3], command=lambda:print(MENU_FORMAT_ITEMS[3]))
156     format_menu.add_command(label=MENU_FORMAT_ITEMS[4], command=lambda:print(MENU_FORMAT_ITEMS[4]))
157     format_menu.add_command(label=MENU_FORMAT_ITEMS[5], command=lambda:print(MENU_FORMAT_ITEMS[5]))
158     format_menu.add_command(label=MENU_FORMAT_ITEMS[6], command=lambda:print(MENU_FORMAT_ITEMS[6]))
159     format_menu.add_command(label=MENU_FORMAT_ITEMS[7], command=lambda:print(MENU_FORMAT_ITEMS[7]))
160     format_menu.add_command(label=MENU_FORMAT_ITEMS[8], command=lambda:print(MENU_FORMAT_ITEMS[8]))
161     format_menu.add_command(label=MENU_FORMAT_ITEMS[9], command=lambda:print(MENU_FORMAT_ITEMS[9]))
162     format_menu.add_separator()
163     format_menu.add_command(label=MENU_FORMAT_ITEMS[10], command=lambda:print(MENU_FORMAT_ITEMS[10]))
164     menubar.add_cascade(label=MENU_ITEMS[2], menu=format_menu)
165 
166 def menu_run(menubar):
167     '''定义菜单Run'''
168     run_menu = Menu(menubar, tearoff=1)
169     run_menu.add_command(label=MENU_RUN_ITEMS[0], command=lambda:print(MENU_RUN_ITEMS[0]))
170     run_menu.add_separator()
171     run_menu.add_command(label=MENU_RUN_ITEMS[1], command=lambda:print(MENU_RUN_ITEMS[1]))
172     run_menu.add_command(label=MENU_RUN_ITEMS[2], command=lambda:print(MENU_RUN_ITEMS[2]))
173     menubar.add_cascade(label=MENU_ITEMS[-4], menu=run_menu)
174 
175 def meun_options(menubar):
176     '''定义菜单Options'''
177     options_menu = Menu(menubar, tearoff=1)
178     options_menu.add_command(label=MENU_OPTIONS_ITEMS[0], command=lambda:print(MENU_OPTIONS_ITEMS[0]))
179     options_menu.add_separator()
180     options_menu.add_command(label=MENU_OPTIONS_ITEMS[1], command=lambda:print(MENU_OPTIONS_ITEMS[1]))
181     menubar.add_cascade(label=MENU_ITEMS[-3], menu=options_menu)
182 
183 def menu_windows(menubar):
184     '''定义菜单Windows'''
185     windows_menu = Menu(menubar, tearoff=1)
186     windows_menu.add_command(label=MENU_WINDOWS_ITEMS[0], command=lambda:print(MENU_WINDOWS_ITEMS[0]))
187     windows_menu.add_separator()
188     menubar.add_cascade(label=MENU_ITEMS[-2], menu=windows_menu)
189 
190 def meun_help(menubar):
191     '''定义菜单Help'''
192     help_menu = Menu(menubar, tearoff=1)
193     help_menu.add_command(label=MENU_HELP_ITEMS[0], command=about_idel)
194     help_menu.add_separator()
195     help_menu.add_command(label=MENU_HELP_ITEMS[1], command=lambda:print(MENU_HELP_ITEMS[1]))
196     help_menu.add_command(label=MENU_HELP_ITEMS[2], command=lambda:print(MENU_HELP_ITEMS[2]))
197     menubar.add_cascade(label=MENU_ITEMS[-1], menu=help_menu)
198 
199 def about_idel():
200     '''Help-About IDEL function'''
201     label = Label(root, text=ABOUT_MESSAGE, fg='red')
202     label.pack(side='top')
203 
204 def init_menu_bar(menubar):
205     '''初始化菜单条'''
206     menu_file(menubar)     #file
207     menu_edit(menubar)     #edit
208     menu_format(menubar)   #format
209     menu_run(menubar)      #run
210     meun_options(menubar)  #options
211     menu_windows(menubar)  #windows
212     meun_help(menubar)     #help
213 
214 #获得窗口对象
215 root = get_tk()
216 #设置窗口大小
217 set_tk_geometry(root, '')
218 #设置窗口title
219 set_tk_title(root, 'Python 3.3.2 Shell')
220 #获取菜单对象
221 menubar = get_menu(root)
222 #初始化菜单
223 init_menu_bar(menubar)
224 #加载菜单配置
225 root.config(menu=menubar)
226 
227 mainloop()

 

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

More reading,and english is important.

I'm Hongten

 

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

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

posted @ 2013-09-05 17:42  Hongten  阅读(25842)  评论(0编辑  收藏  举报
Fork me on GitHub