案例一
Label & Button 标签和按钮
import tkinter as tk
window = tk. Tk( )
window. title( "muy window" )
window. geometry( "200x100" )
var1 = tk. StringVar( )
l1 = tk. Label( window, textvariable= var1, bg= "green" , font= ( "Arial" , 12 ) , width= 15 , height= 2 )
l1. pack( )
hit_me1 = False
def hit_me ( ) :
global hit_me1
if not hit_me1:
hit_me1 = True
var1. set ( "this is tk" )
else :
hit_me1 = False
var1. set ( "this is tk, too" )
b1 = tk. Button( window, text= "hit me" , width= 15 , height= 2 , command= hit_me)
b1. pack( )
window. mainloop( )
案例二
Entry & Text 输入和文本框
import tkinter as tk
window = tk. Tk( )
window. title( "muy window" )
window. geometry( "200x150" )
e = tk. Entry( window, show= None )
e. pack( )
def insert_point ( ) :
var = e. get( )
t. insert( "insert" , var)
def insert_end ( ) :
var = e. get( )
t. insert( "end" , var)
b1 = tk. Button( window, text= "insert point" , width= 15 , height= 2 , command= insert_point) . pack( )
b2 = tk. Button( window, text= "insert end" , width= 15 , height= 2 , command= insert_end) . pack( )
t = tk. Text( window, height= 2 , width= 15 )
t. pack( )
window. mainloop( )
案例三
Listbox 部件
import tkinter as tk
window = tk. Tk( )
window. title( "muy window" )
window. geometry( "200x150" )
var1 = tk. StringVar( )
l = tk. Label( window, bg= "yellow" , width= 4 , textvariable= var1)
l. pack( )
def print_selection ( ) :
var = lb. get( lb. curselection( ) )
var1. set ( var)
b1 = tk. Button( window, text= "insert point" , width= 15 , height= 2 ,
command= print_selection) . pack( )
var2 = tk. StringVar( )
var2. set ( ( 1 , 2 , 3 , 4 ) )
lb = tk. Listbox( window, listvariable= var2)
list_item = [ 1 , 2 , 11 , 54 ]
for i in list