Python GUI Learning--tkinter

Source: Internet
Author: User

Common GUI toolkits that support python:

Tkinter using the TK platform makes it easy to get semi-standard
Wxpython based on Wxwindows. Cross-platform is becoming more and more popular
Python Win can only use native Windows GUI features on Windows
Java Swing can only be used for Jython's Java GUI using native
PyGTK is popular on Linux using the GTK platform
PyQt using QT platforms across platforms

Tkinter Learning
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 display
BG Background Color
width of 3D boundary of BD Perimeter
Font fonts (color, size)
Width Control widths
Height Control Heights
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root= Tk ()#initialize TK ()Root.title ("label-test")#Set Window captionRoot.geometry ("200x300")#Setting the window size Note: X is not *Root.resizable (Width=true, Height=false)#sets whether the window can vary long/wide, false immutable, true variable, default TrueL = Label (root, text="label", bg="Pink", Font= ("Arial", Width=8, height=3) L.pack (Side=left)#the side here can be assigned to the left Rtght TOP BOTTOMRoot.mainloop ()#Enter the message loop

Operation Result:

2.Frame
Description: Creates a rectangular area on the screen that is used as a container to lay out the form
Usage: Frame (Root object, "property list")
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root= Tk ()#initialize TK ()Root.title ("frame-test")#Set Window captionRoot.geometry ("300x200")#Setting the window size Note: X is not *Root.resizable (Width=true, Height=false)#sets whether the window can vary long/wide, false immutable, true variable, default TrueLabel (Root, text="Frame", bg="Red", Font= ("Arial", 15) . Pack () frm=Frame (Root)# Leftfrm_l =Frame (frm) Label (frm_l, Text="Upper left", bg="Pink", Font= ("Arial", ()). Pack (side=TOP) Label (frm_l, Text="Lower left", bg="Green", Font= ("Arial", ()). Pack (side=TOP) frm_l.pack (Side=Left )# RightFrm_r =Frame (frm) Label (frm_r, Text="Top Right", bg="Yellow", Font= ("Arial", ()). Pack (side=TOP) Label (frm_r, Text="Lower Right", bg="Purple", Font= ("Arial", ()). Pack (side=TOP) frm_r.pack (Side=Right ) Frm.pack ()#the side here can be assigned to the left Rtght TOP BOTTOMRoot.mainloop ()#Enter the message loop

Operation Result:

3.Entry
Description: Create a single-line text box
Usage: Create Entry (Root object, "property list")
  Binding variable Var=stringvar () e=entry (Root object, textvariable = var)
   Gets the value in the text box Var.get ()
   Set the value in the text box Var.set (item1)
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root= Tk ()#initialize TK ()Root.title ("entry-test")#Set Window captionRoot.geometry ("300x200")#Setting the window size Note: X is not *Root.resizable (Width=true, Height=false)#sets whether the window can vary long/wide, false immutable, true variable, default Truevar =Variable () e= Entry (Root, textvariable=var) var.set ("entry")#set the value in the text boxE.pack ()#the side here can be assigned to the left Rtght TOP BOTTOMRoot.mainloop ()#Enter the message loop

Operation Result:

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
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root= Tk ()#initialize TK ()Root.title ("text-test")#Set Window captionRoot.geometry ("300x200")#Setting the window size Note: X is not *Root.resizable (Width=true, Height=false)#sets whether the window can vary long/wide, false immutable, true variable, default Truet =Text (Root) T.insert ('1.0',"text1\n")#InsertT.insert (END,"text2\n")#end: The last character of this textbufferT.insert ('1.0',"text3\n")#t.delete (' 1.0 ', ' 2.0 ') # DeleteT.pack ()#the side here can be assigned to the left Rtght TOP BOTTOMRoot.mainloop ()#Enter the message loop

Operation Result:

5.Button
Description: Create a button
Usage: button (Root object, "property list")
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root= Tk ()#initialize TK ()Root.title ("button-test")#Set Window captionRoot.geometry ()#Setting the window size Note: X is not *defPrinthello (): T.insert (END,"hello\n") T=Text () t.pack ()#the side here can be assigned to the left Rtght TOP BOTTOMButton (Root, text=" Press", command=Printhello). Pack () Root.mainloop ()#Enter the message loop

Operation Result:

6.Listbox
Description: List control, can contain one or more text boxes, can be selected or multiple
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 ()
Sets 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 Lb.get (Lb.curselection ())
Properties: SelectMode can be multipl single for Browse
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root=Tk () root.title ("listbox-test") root.geometry ()defPrint_item (event):PrintLb.get (Lb.curselection ()) var=stringvar () lb= Listbox (root, listvariable =var) list_item= [1,2,3,4] forIteminchList_item:lb.insert (End,item) lb.delete (2,4) Var.set ('a','b','C','D'))PrintVar.get () lb.bind ('<ButtonRelease-1>', Print_item) lb.pack () Root.mainloop ()

Operation Result:

7.Scrollbar
Description: Vertical scrolling control
Usage: ListBox (Root object, property list
Instance:
#-*-coding:utf-8-*-__author__='007'__date__='2016/4/7' fromTkinterImport*Root= Tk ()#initialize TK ()Root.title ("scrl-test")#Set Window captionRoot.geometry ()#Setting the window size Note: X is not *defPrint_item (event):PrintLb.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] forIteminchList_item:lb.insert (end,item) scrl=Scrollbar (Root) scrl.pack (side=right,fill=Y) lb.configure (Yscrollcommand=scrl.set)#The callback function that specifies the Yscrollbar of the listbox is the set of ScrollBar, which indicates that the scroll bar is updated in real time when the window changesLb.pack (side=left,fill=BOTH) scrl['Command'] = Lb.yview#The callback function that specifies the command for scrollbar is Listbar YviewRoot.mainloop ()

Operation Result:


Generic control Property Reference

Python GUI Learning--tkinter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.