This article was reproduced from: http://www.cnblogs.com/kaituorensheng/p/3287652.html
Tkinter Module ("Tk interface") is the interface of Python's standard Tk GUI toolkit
As a practice, using Tkinter to do an ASCII code conversion query Table , this article from four point introduction
- Product introduction
- Design planning
- Related knowledge
- SOURCE Accessories
1. Product INTRODUCTION
Interface
Function
- Query for information by entering characters or numbers
- Query for information by selecting the information in the list
2. Design Planning
Planning diagram
3. Related knowledge
First look at how to produce the first window
From Tkinter import * #引用Tk模块root = Tk () #初始化Tk () root.mainloop () #进入消息循环
Several common properties
- Title: Setting the window title
- Geometry: Setting the window size
- Resizable (): Sets whether the window can vary in length and width
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry (' 200x100 ') #是x not *root. Resizable (Width=false, height=true) #宽不可变, high variable, default = Trueroot.mainloop ()
Describes the use of several controls
- Label
- Frame
- Entry
- Text
- Button
- Listbox
- Scrollbar
Explains that each control is finally added with a pack (). Otherwise the control cannot be displayed.
1. Label
Description
Label
Usage
Label (Root object, [property list])
Property
- Text to realistic texts
- BG background Color
- font fonts (color, size)
- Width control widths
- Height Control Heights
The controls described below have almost all of these properties, and more detailed Properties view the reference page
Example
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry (' 300x200 ') L = Label (root, Te Xt= "Show", bg= "green", font= ("Arial", "a"), Width=5, height=2) l.pack (side=left) #这里的side可以赋值为LEFT rtght TOP Bottomroot.mainloop ()
Effect
2. Frame
Description
Create a rectangular area on the screen to layout the form as a container
Usage
Frame (Root object, [property list])
Example
to appear in the control, such four words
Motto
Thick German dedicated
Learned Music Group
can be planned as
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry (' 300x200 ') Label (root, text= ' School motto '. Decode (' GBK '). Encode (' UTF8 '), font= (' Arial ')). Pack () frm = frame (root) #leftfrm_L = Frame (frm) Label (frm_l, Text= ' decode (' GBK '). Encode (' UTF8 '), font= (' Arial ', ()). Pack (Side=top) Label (frm_l, text= ' learned '. Decode (' GBK '). Encode (' UTF8 '), font= (' Arial ', ()). Pack (Side=top) frm_l.pack (side=left) #rightfrm_R = Frame (frm) Label (frm_r, text= ' Dedicated '. Decode (' GBK '). Encode (' UTF8 '), font= (' Arial '), "pack" (side=top) Label (Frm_r, text= ' Le Qun '). Decode (' GBK '). Encode (' UTF8 '), font= (' Arial ', ()). Pack (Side=top) frm_r.pack (side=right) Frm.pack () Root.mainloop ()
Effect
3. Entry
Description
Create a single-line text box
Usage
- Create: LB =entry (Root object, [property list])
- Binding variable Var=stringvar () lb=entry (root object, textvariable = var)
- Gets the value in the text box Var.get ()
- Set the value in the text box Var.set (item1)
Example
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry () var = stringvar () e = Entry ( Root, textvariable = var) var.set ("Hello") e.pack () Root.mainloop ()
Effect
4. Text
Description
Enter text into the space
Usage
T = Text (Root object)
Insert: T.insert (mark, content)
Deleted: T.delete (Mark1, MARK2)
Where mark can be a line number, or a special identifier, such as
- Insert: The insertion point of the cursor current: The position of the character that corresponds to the position of the mouse
- End: The last character of this textbuffer
- Sel_first: Selects the first character of the text field, and throws an exception if no area is selected
- Sel_last: Selects the last character of the text field and throws an exception if no area is selected
Example
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry (' 300x200 ') T = Text (root) t.in SERT (1.0, ' hello\n ') t.insert (end, ' hello000000\n ') t.insert (end, ' Nono ') T.pack () Root.mainloop ()
Effect
5. Button
Description
Create button
Usage
Button (Root object, [property list])
Example
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry () def Printhello (): t.in SERT (' 1.0 ', "hello\n") t = Text () t.pack () Button (Root, text= "Press", command = Printhello). Pack () Root.mainloop ()
Effect
6. Listbox
Description
List control, can contain one or more text to think, can be single-choice or multi-select
Usage
- Create: lb = ListBox (root object, [property list])
- Binding variable Var=stringvar () Lb=listbox (root object, listvariable = var)
- Get all the values in the list var.get ()
- Set all values in the list Var.set ((item1, item2, ...))
- Added: Lb.insert (item)
- Deleted: Lb.delete (item,...)
- Binding event Lb.bind (' <ButtonRelease-1> ', function)
- Get the selected option Lbl.get (Lb.curselection ())
Property
SelectMode can be MULTIPL single for Browse
Example
#-*-coding:cp936-*-from Tkinter Import *root = Tk () root.title ("Hello World") root.geometry () def print_item (event):
print Lb.get (Lb.curselection ()) var = stringvar () lb = Listbox (root, listvariable = var) list_item = [1, 2, 3, 4]FO R item in List_item: lb.insert (END, Item) Lb.delete (2, 4) Var.set ((' A ', ' ab ', ' C ', ' d ')) print var.get () lb.bind (' < Buttonrelease-1> ', Print_item) lb.pack () Root.mainloop ()
Effect
7. Scrollbar
Description
Create a rectangular area on the screen to layout the form as a container
Usage
Frame (Root object, [property list]), the longest usage is to use with other controls.
Example
From Tkinter Import *root = Tk () root.title ("Hello World") root.geometry () def print_item (event): print Lb.get ( Lb.curselection ()) var = stringvar () lb = Listbox (root, Height=5, selectmode=browse, listvariable = var) lb.bind (' <ButtonRelease-1> ', print_item) List_item = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]for item in List_item: lb.insert (END, I TEM) scrl = Scrollbar (root) scrl.pack (side=right, fill=y) lb.configure (Yscrollcommand = scrl.set) Lb.pack (side= Left, Fill=both) scrl[' command '] = Lb.yviewroot.mainloop ()
Effect
4. Source code Attachment
Code
View Code
Text
View Code
Resources
General Control Properties: http://my.oschina.net/TyLucifer/blog/112961
Python's Tkinter module learning