This Tkinter Learning Tutorial focuses on the following uses 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 '
From Tkinter Import *
root = TK () # Initialize TK ()
Root.title ("Label-test") # Setting the window caption
Root.geometry ("200x300") # Set 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 True
L = label (Root, text= "label", bg= "Pink", font= ("Arial", "n"), Width=8, height=3)
L.pack (side=left) # side here can be assigned to left Rtght TOP BOTTOM
Root.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 '
From Tkinter Import *
root = TK () # Initialize TK ()
Root.title ("Frame-test") # Setting the window caption
Root.geometry ("300x200") # Set 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 True
Label (Root, text= "frame", bg= "Red", font= ("Arial"). Pack ()
frm = Frame (root)
#left
frm_l = Frame (frm)
Label (frm_l, text= "top left", bg= "Pink", font= ("Arial",). Pack (Side=top)
Label (frm_l, text= "bottom left", bg= "green", font= ("Arial"). Pack (Side=top)
Frm_l.pack (Side=left)
#right
Frm_r = Frame (frm)
Label (Frm_r, text= "top right", bg= "Yellow", font= ("Arial",). Pack (Side=top)
Label (Frm_r, text= "bottom right", bg= "Purple", font= ("Arial",). Pack (Side=top)
Frm_r.pack (Side=right)
Frm.pack () # Side here can be assigned to the left Rtght TOP BOTTOM
Root.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 '
From Tkinter Import *
root = TK () # Initialize TK ()
Root.title ("Entry-test") # Setting the window caption
Root.geometry ("300x200") # Set 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 True
var = Variable ()
E = Entry (root, Textvariable=var)
Var.set ("entry") # Set the value in the text box
E.pack () # Side here can be assigned to the left Rtght TOP BOTTOM
Root.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 '
From Tkinter Import *
root = TK () # Initialize TK ()
Root.title ("Text-test") # Setting the window caption
Root.geometry ("300x200") # Set 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 True
T = Text (Root)
T.insert (' 1.0 ', "text1\n") # Insert
T.insert (end, "text2\n") # end: The last character of this textbuffer
T.insert (' 1.0 ', "text3\n")
#t. Delete (' 1.0 ', ' 2.0 ') # Delete
T.pack () # Side here can be assigned to the left Rtght TOP BOTTOM
Root.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 '
From Tkinter Import *
root = TK () # Initialize TK ()
Root.title ("Button-test") # Setting the window caption
Root.geometry () # Set Window size Note: X is not *
Def Printhello ():
T.insert (END, "hello\n")
t = Text ()
T.pack () # Side here can be assigned to the left Rtght TOP BOTTOM
button (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 '
From Tkinter Import *
root = Tk ()
Root.title ("Listbox-test")
Root.geometry ()
def print_item (event):
Print Lb.get (lb.curselection ())
var = stringvar ()
LB = Listbox (root, listvariable = var)
List_item = [1,2,3,4]
For item in List_item:
Lb.insert (End,item)
Lb.delete (2,4)
Var.set (' A ', ' B ', ' C ', ' d ')
Print Var.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 '
From Tkinter Import *
root = TK () # Initialize TK ()
Root.title ("Scrl-test") # Setting the window caption
Root.geometry () # Set Window size Note: X is not *
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,item)
Scrl = Scrollbar (Root)
Scrl.pack (side=right,fill=y)
Lb.configure (yscrollcommand=scrl.set) # Specifies that the Yscrollbar callback function for the listbox is a set of ScrollBar, indicating that the scroll bar is updated in real time when the window changes
Lb.pack (Side=left,fill=both)
scrl[' command ' = lb.yview # The callback function that specifies the command of ScrollBar is Listbar Yview
Root.mainloop ()
Operation Result:
From: Blog Park/python Inn
Tkinter Easy Tutorial