1、简单示例
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
win. mainloop( )
2、label空间
import tkinter
win = tkinter. Tk( )
win. title( "sunck" )
win. geometry( "400x400+200+0" )
'''
label:标签控件可以显示文本
'''
label = tkinter. Label( win,
text= "fanfan" ,
bg= "blue" ,
fg = "red" ,
font= ( "黑体" , 20 ) ,
width= 10 ,
height= 4 ,
wraplength= 100 ,
justify= "left" ,
anchor= "center" )
label. pack( )
win. mainloop( )
3、button控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
def func ( ) :
print ( "fanfan is a good man" )
button1 = tkinter. Button( win, text= "按钮" , command= func, width= 10 , height= 10 )
button1. pack( )
button2 = tkinter. Button( win, text= "按钮" , command= lambda : print ( "fanfan is a good man" ) , width= 10 , height= 10 )
button2. pack( )
button3 = tkinter. Button( win, text= "按钮" , command= win. quit, width= 10 , height= 10 )
button3. pack( )
win. mainloop( )
4、entry控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
'''
输入控件
用于显示简单的文本内容
'''
e = tkinter. Variable( )
entry = tkinter. Entry( win, textvariable= e)
entry. pack( )
e. set ( "fanfan is a good man" )
print ( e. get( ) )
print ( entry. get( ) )
win. mainloop( )
5、点击按钮输出输入框中的内容
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
def showInfo ( ) :
print ( entry. get( ) )
entry = tkinter. Entry( win)
entry. pack( )
button = tkinter. Button( win, text= "按钮" , command= showInfo)
button. pack( )
win. mainloop( )
6、text控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
'''
文本控件,用于显示多行文本
'''
text = tkinter. Text( win, width= 30 , height= 4 )
text. pack( )
str = """That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet."""
text. insert( tkinter. INSERT, str )
win. mainloop( )
7、带滚动条的text
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
'''
文本控件,用于显示多行文本
'''
scroll = tkinter. Scrollbar( )
text = tkinter. Text( win, width= 50 , height= 8 )
scroll. pack( side= tkinter. RIGHT, fill= tkinter. Y)
text. pack( side= tkinter. LEFT, fill= tkinter. Y)
str = """That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet.That we are in the midst of crisis is now well understood. Our nation is at war, against a far-reaching network of violence and hatred. Our economy is badly weakened, a consequence of greed and irresponsibility on the part of some, but also our collective failure to make hard choices and prepare the nation for a new age. Homes have been lost; jobs shed; businesses shuttered. Our health care is too costly; our schools fail too many; and each day brings further evidence that the ways we use energy strengthen our adversaries and threaten our planet."""
text. insert( tkinter. INSERT, str )
scroll. config( command= text. yview)
text. config( yscrollcommand= scroll. set )
win. mainloop( )
8、checkbutton多选框控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
def updata ( ) :
message = ""
if hobby1. get( ) == True :
message += "money\n"
if hobby2. get( ) == True :
message += "power\n"
if hobby3. get( ) == True :
message += "people\n"
text. delete( 0.0 , tkinter. END)
text. insert( tkinter. INSERT, message)
hobby1 = tkinter. BooleanVar( )
check1 = tkinter. Checkbutton( win, text= "money" , variable= hobby1, command= updata)
check1. pack( )
hobby2 = tkinter. BooleanVar( )
check2 = tkinter. Checkbutton( win, text= "power" , variable= hobby2, command= updata)
check2. pack( )
hobby3 = tkinter. BooleanVar( )
check3 = tkinter. Checkbutton( win, text= "people" , variable= hobby3, command= updata)
check3. pack( )
text = tkinter. Text( win, width= 50 , height= 5 )
text. pack( )
win. mainloop( )
9、radiobutton单选框控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
def updata ( ) :
print ( r. get( ) )
r = tkinter. StringVar( )
radio1 = tkinter. Radiobutton( win, text= "one" , value= "good" , variable= r, command= updata)
radio1. pack( )
radio2 = tkinter. Radiobutton( win, text= "two" , value= "nice" , variable= r, command= updata)
radio2. pack( )
radio3 = tkinter. Radiobutton( win, text= "two" , value= "cool" , variable= r, command= updata)
radio3. pack( )
win. mainloop( )
10、listbox控件1
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
'''
列表框控件,可以包含一个或多个文本框
作用:可以在listbox的小窗口显示一个字符串
'''
lb = tkinter. Listbox( win, selectmode= tkinter. BROWSE)
lb. pack( )
for item in [ "good" , "nice" , "handsome" , "vg" , "vn" ] :
lb. insert( tkinter. END, item)
lb. insert( tkinter. ACTIVE, "cool" )
lb. select_set( 2 , 5 )
print ( lb. curselection( ) )
print ( lb. selection_includes( 3 ) )
win. mainloop( )
11、listbox控件2
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
lbv = tkinter. StringVar( )
lb = tkinter. Listbox( win, selectmode= tkinter. SINGLE, listvariable= lbv)
lb. pack( )
for item in [ "good" , "nice" , "handsome" , "vg" , "vn" ] :
lb. insert( tkinter. END, item)
print ( lbv. get( ) )
def myPrint ( event) :
print ( lb. get( lb. curselection( ) ) )
lb. bind( '<Double-Button-1>' , myPrint)
win. mainloop( )
12、listbox控件3
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
lbv = tkinter. StringVar( )
lb = tkinter. Listbox( win, selectmode= tkinter. SINGLE, listvariable= lbv)
lb. pack( )
for item in [ "good" , "nice" , "handsome" , "vg" , "vn" ] :
lb. insert( tkinter. END, item)
print ( lbv. get( ) )
def myPrint ( event) :
print ( lb. get( lb. curselection( ) ) )
lb. bind( '<Double-Button-1>' , myPrint)
win. mainloop( )
13、listbox控件4
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
lb = tkinter. Listbox( win, selectmode= tkinter. MULTIPLE)
lb. pack( )
for item in [ "good" , "nice" , "handsome" , "vg" , "vn" ] :
lb. insert( tkinter. END, item)
win. mainloop( )
14、scale控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
'''
供用户拖拽指示器,改变变量的值,可以水平,
tkinter.HORIZONTAL 水平
tkinter.VERTICAL 竖直
length 水平时表示宽度,竖直时表示高度
tickinterval表示值将会是该值的倍数
'''
scale = tkinter. Scale( win, from_= 0 , to= 100 , orient= tkinter. VERTICAL, tickinterval= 10 , length= 200 )
scale. pack( )
scale. set ( 20 )
def showNum ( ) :
print ( scale. get( ) )
tkinter. Button( win, text= "按钮" , command= showNum) . pack( )
win. mainloop( )
15、sprinbox数值范围控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
'''
数值范围控件
increment 步长,默认为1
values 最好不要和from_=0, to=100, increment=1,同时使用
values=(0,2,4,6,8)
'''
def updata ( ) :
print ( v. get( ) )
v = tkinter. StringVar( )
sp = tkinter. Spinbox( win, from_= 0 , to= 100 , increment= 1 , textvariable= v, command= updata)
sp. pack( )
v. set ( 20 )
print ( v. get( ) )
win. mainloop( )
16、menu顶层菜单
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
menbar = tkinter. Menu( win)
win. config( menu= menbar)
def func ( ) :
print ( "fanfan is a good man" )
menu1 = tkinter. Menu( menbar, tearoff= False )
for item in [ "Python" , "C" , "C++" , "OC" , "Swift" , "C#" , "shell" , "Java" , "JS" , "PHP" , "汇编" , "NodeJS" , "退出" ] :
if item == "退出" :
menu1. add_separator( )
menu1. add_command( label= item, command= win. quit)
else :
menu1. add_command( label= item, command= func)
menbar. add_cascade( label= "语言" , menu = menu1)
menu2 = tkinter. Menu( menbar, tearoff= False )
menu2. add_command( label= "red" )
menu2. add_command( label= "blue" )
menbar. add_cascade( label= "颜色" , menu = menu2)
win. mainloop( )
17、Menu鼠标右键菜单
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
menubar = tkinter. Menu( win)
menu = tkinter. Menu( menubar, tearoff= False )
for item in [ "Python" , "C" , "C++" , "OC" , "Swift" , "C#" , "shell" , "Java" , "JS" , "PHP" , "汇编" , "NodeJS" , "退出" ] :
menu. add_command( label= item)
menubar. add_cascade( label= "语言" , menu= menu)
def showMenu ( event) :
menubar. post( event. x_root, event. y_root)
win. bind( "<Button-3>" , showMenu)
win. mainloop( )
18、Combobox下拉控件
import tkinter
from tkinter import ttk
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
cv = tkinter. StringVar( )
com = ttk. Combobox( win, textvariable= cv)
com. pack( )
com[ "value" ] = [ "黑龙江" , "吉林" , "辽宁" ]
com. current( 0 )
def func ( event) :
print ( com. get( ) )
print ( cv. get( ) )
print ( "sunck is a good man" )
com. bind( "<<ComboboxSelected>>" , func)
win. mainloop( )
19、Frame控件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
'''
框架控件
在屏幕上显示一个矩形区域,多作为一个容器
'''
frm = tkinter. Frame( win)
frm. pack( )
frm_l = tkinter. Frame( frm)
tkinter. Label( frm_l, text= "左上" , bg= "pink" ) . pack( side= tkinter. TOP)
tkinter. Label( frm_l, text= "左下" , bg= "blue" ) . pack( side= tkinter. TOP)
frm_l. pack( side= tkinter. LEFT)
frm_r = tkinter. Frame( frm)
tkinter. Label( frm_r, text= "右上" , bg= "red" ) . pack( side= tkinter. TOP)
tkinter. Label( frm_r, text= "右下" , bg= "yellow" ) . pack( side= tkinter. TOP)
frm_r. pack( side= tkinter. RIGHT)
win. mainloop( )
20、数据显示–表格数据
import tkinter
from tkinter import ttk
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "600x400+200+0" )
tree = ttk. Treeview( win)
tree. pack( )
tree[ "columns" ] = ( "姓名" , "年龄" , "身高" , "体重" )
tree. column( "姓名" , width= 100 )
tree. column( "年龄" , width= 100 )
tree. column( "体重" , width= 100 )
tree. column( "身高" , width= 100 )
tree. heading( "姓名" , text= "姓名-name" )
tree. heading( "年龄" , text= "姓名-age" )
tree. heading( "体重" , text= "姓名-weight" )
tree. heading( "身高" , text= "姓名-height" )
tree. insert( "" , 0 , text= "line1" , values= ( "卢彦旭" , "28" , "165" , "80" ) )
tree. insert( "" , 1 , text= "line2" , values= ( "范玉斌" , "29" , "167" , "70" ) )
win. mainloop( )
21、数据显示–树状数据
import tkinter
from tkinter import ttk
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
tree = ttk. Treeview( win)
tree. pack( )
treeF1 = tree. insert( "" , 0 , "中国" , text= "中国CHI" , values= ( "F1" ) )
treeF2 = tree. insert( "" , 1 , "美国" , text= "美国USA" , values= ( "F2" ) )
treeF3 = tree. insert( "" , 2 , "英国" , text= "英国ENG" , values= ( "F3" ) )
treeF1_1 = tree. insert( treeF1, 0 , "黑龙江" , text= "中国黑龙江" , values= ( "F1_1" ) )
treeF1_2 = tree. insert( treeF1, 1 , "吉林" , text= "中国吉林" , values= ( "F1_2" ) )
treeF1_3 = tree. insert( treeF1, 2 , "辽宁" , text= "中国辽宁" , values= ( "F1_3" ) )
treeF2_1 = tree. insert( treeF2, 0 , "德克萨斯州" , text= "美国德克萨斯州" , values= ( "F2_1" ) )
treeF2_2 = tree. insert( treeF2, 1 , "底特律" , text= "美国底特律" , values= ( "F2_2" ) )
treeF2_3 = tree. insert( treeF2, 2 , "旧金山" , text= "美国旧金山" , values= ( "F2_3" ) )
treeF1_1_1 = tree. insert( treeF1_1, 0 , "哈尔滨" , text= "黑龙江哈尔滨" )
treeF1_1_2 = tree. insert( treeF1_1, 0 , "五常" , text= "黑龙江五常" )
win. mainloop( )
22、绝对布局
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label1 = tkinter. Label( win, text= "good" , bg= "blue" )
label2 = tkinter. Label( win, text= "nice" , bg= "red" )
label3 = tkinter. Label( win, text= "cool" , bg= "pink" )
label1. place( x= 10 , y= 10 )
label2. place( x= 50 , y= 50 )
label3. place( x= 100 , y= 100 )
win. mainloop( )
23、相对布局
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label1 = tkinter. Label( win, text= "good" , bg= "blue" )
label2 = tkinter. Label( win, text= "nice" , bg= "red" )
label3 = tkinter. Label( win, text= "cool" , bg= "pink" )
label1. pack( fill= tkinter. Y, side= tkinter. LEFT)
label2. pack( fill= tkinter. X, side= tkinter. TOP)
label3. pack( )
win. mainloop( )
24、表格布局
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label1 = tkinter. Label( win, text= "good" , bg= "blue" )
label2 = tkinter. Label( win, text= "nice" , bg= "red" )
label3 = tkinter. Label( win, text= "cool" , bg= "pink" )
label4 = tkinter. Label( win, text= "handsome" , bg= "yellow" )
label1. grid( row= 0 , column= 0 )
label2. grid( row= 0 , column= 1 )
label3. grid( row= 1 , column= 0 )
label4. grid( row= 1 , column= 1 )
win. mainloop( )
25、鼠标点击事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
button1 = tkinter. Button( win, text= "leftmouse button" )
def func ( event) :
print ( event. x, event. y)
button1. bind( "<Triple Button-1>" , func)
button1. pack( )
win. mainloop( )
26、鼠标移动事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "fanfan is a good man" )
label. pack( )
def func ( event) :
print ( event. x, event. y)
label. bind( "<B1-Motion>" , func)
win. mainloop( )
27、鼠标释放事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "fanfan is a good man" , bg= "red" )
label. pack( )
def func ( event) :
print ( event. x, event. y)
label. bind( "<ButtonRelease-1>" , func)
win. mainloop( )
28、进入离开事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "fanfan is a good man" , bg= "red" )
label. pack( )
def func ( event) :
print ( event. x, event. y)
label. bind( "<Leave>" , func)
win. mainloop( )
29、响应所有按键的事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "fanfan is a good man" , bg= "red" )
label. focus_set( )
label. pack( )
def func ( event) :
print ( "event.char=" , event. char)
print ( "event.keycode=" , event. keycode)
label. bind( "<Key>" , func)
win. mainloop( )
30、响应特殊按键事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "fanfan is a good man" , bg= "red" )
label. focus_set( )
label. pack( )
def func ( event) :
print ( "event.char=" , event. char)
print ( "event.keycode=" , event. keycode)
label. bind( "<Shift_L>" , func)
win. mainloop( )
31、指定按键事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "fanfan is a good man" , bg= "red" )
label. focus_set( )
label. pack( )
def func ( event) :
print ( "event.char=" , event. char)
print ( "event.keycode=" , event. keycode)
label. bind( "a" , func)
win. mainloop( )
32、组合按键事件
import tkinter
win = tkinter. Tk( )
win. title( "fanfan" )
win. geometry( "400x400+200+0" )
label = tkinter. Label( win, text= "sunck is a good man" , bg= "red" )
label. focus_set( )
label. pack( )
def func ( event) :
print ( "event.char=" , event. char)
print ( "event.keycode=" , event. keycode)
label. bind( "<Shift-Up>" , func)
win. mainloop( )