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'按鈕的時候,退出窗口程序。
我想現(xiàn)在你對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模塊可以創(chuàng)建一個窗口控件,如Java中的Swing 10 功能描述: 11 根據(jù)Python 3.3.2 IDEL的菜單,創(chuàng)建出一個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
========================================================
轉載于:https://www.cnblogs.com/hongten/p/hongten_python_tkinter_idel.html
總結
以上是生活随笔為你收集整理的python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SATA硬盘有固件问题的硬盘型号以及购买
- 下一篇: 27条好赚钱副业的途径:自己也能不用上班