1 Small example a minimal application
#-*-Coding:utf-8-*-
#!/usr/bin/env python # #正确安装python后, double-click to automatically execute
Import Tkinter as TK # #导入Tkinter包命名为tk
Class Application (TK. Frame): # #自定义类继承自Frame
def __init__ (Self,master=none): # #自定义类的构造函数
tk. Frame.__init__ (self,master) # #调用父类的构造函数
Self.grid () # #确保框架正确的显示在窗体内
Self.createwidgets () # #调用自定义方法
def createwidgets (self):
self.quitbutton= tk. Button (self, # #创建按钮
text= ' exit ', Command=self.close)
Self.quitButton.grid () # #将按钮显示到窗体
def close (self): # #获取顶级窗体然后销毁, Button event
Self.winfo_toplevel (). Destroy () # # Command=self.quit Resolve button to die
App = Application () # #创建实例
App.master.title (' SampleApplication ') # #设置窗体的标题
App.mainloop () # #启动窗体mainloop, waiting for keyboard and mouse events
Terms
widget-components, controls such as: Buttons, RadioButtons, text fields, frames, and text labels
frame-contains the rectangular region of the component, a rectangular area, can contain other widgets
Child, parent-When you create a component and put it into a frame, you create a parent-son relationship
2 form Layouts Layout management
Tkinter contains the layout in 3, the author strongly recommends using the grid layout, dividing the form's inner region into similar forms, and creating the component is only displayed in the form after it has been registered using layout mode.
2.1grid () method parameters
column--the number of columns of the control drop position, starting from 0, the default is 0;
clomnspan--setting the number of columns that the cell spans horizontally
in_--Reset W to the subform of the form W2, method: In_=w2.w2 must be the parent form subclass of W;
ipadx--sets the size of the X-direction white space within the control;
ipady--sets the size of the white space in the Y direction of the control;
padx--set the X-direction white space around the control to preserve the size;
pady--sets the size of the white space in the Y-direction around the control;
row--controls the number of rows placed, starting with 0, the default is the number of rows occupied by the previous bit
rowspan--sets the number of columns vertically spanned by the cell rowspan=4, columnspan=5);
sticky--set alignment, default to center
Sticky=tk.ne (upper right corner) tk.se (lower right) TK.SW (lower left) Tk. NW (upper left corner)
Sticky=tk. N (upper middle), Tk. E (middle right), Tk. S (lower middle), Tk. W (left middle).
Sticky=tk. N+tk. S centered and stretched vertically
Sticky=tk. E+tk. W Center and stretch horizontally
Sticky=tk. N+tk. E+tk. S+tk. W Stretch Fill table cell
2.2 Form Redraw
Use the grid layout to populate the component with the entire cell and redraw it as the size of the form changes
#-*-Coding:utf-8-*-
#!/usr/bin/env python
Import Tkinter as TK
Class Application (TK. Frame):
def __init__ (Self,master=none):
tk. frame.__init__ (Self,master)
Self.grid (STICKY=TK. N+tk. S+tk. E+tk. W
Self.createwidgets ()
def close (self):
Self.winfo_toplevel (). Destroy ()
def createwidgets (self):
Top=self.winfo_toplevel ()
Top.rowconfigure (0, Weight=1)
Top.columnconfigure (0, Weight=1)
Self.rowconfigure (0, Weight=1)
Self.columnconfigure (0, Weight=1)
Self.quit =TK. Button (self, text= ' Quit ', command=self.close)
Self.quit.grid (row=0, column=0, STICKY=TK. N+tk. S+tk. E+tk. W
App = Application ()
App.master.title (' SampleApplication ')
App.mainloop ()
Tkinter 8.5 GUI for Python-----01