python3之Tkinter的使用

一,tkinter使用

1,tkinter介绍

Tkinter模块(“Tk 接口”)是Python的标准Tk GUI工具包的接口,TK和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Max系统里,Tk8.0的后续版本可以实现本地窗口风格,并良好的运行在绝大多数平台中,

Tkinter是内置到python的安装包中、只要安装好Python之后就能import tkinter库,而且IDLE也是用Tkinter编写而成

步骤

1,导入Tkinter模块

2,创建控件

3,指定这个控件的master,即这个控件属于哪一个

4, 告诉GM(geometry manager)有一个控件产生了

2,tkinter使用

0,Tkinter组件

1
2
3
4
5
6
7
8
9
10
11
12
Label      	  显示文本   
Button 显示按钮
Entry 输入内容
Text 显示多行文本
CheckButton 多选框控件按钮
RadioButton 单选框控件
Listbox 列表框控件
Scale 供用户通过拖拽指示器改变变量的值
Spinbox 数值范围控件
Menu 顶层菜单,常用
Combobox 下拉控件
Frame 框架容器控件

1,创建示列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


#进入消息循环





win.mainloop()

2,labe控件显示文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


#进入消息循环

'''
Laber:标签控件可以显示文本

'''
#win: 父窗体
#text 显示的文本内容
#bg 背景色
#fg 字体颜色
#wraplength 指定text文本中多宽进行换行
#justify 设置换行后的对齐方法。left左对齐
#anchor 位置 n北 e东 s南 w西 center 居中 ne东北 se东南 sw 西南 nw 西北 默认居中
label = tkinter.Label(win,
text= "yichen ",
bg="blue",
fg="red",
font = ("黑体",20),
width = 20,
height= 4,
wraplength= 100,
justify="left",
anchor="nw")
#显示出来
label.pack()




win.mainloop()

3,Button控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import tkinter


def func():
print("yichen is a goo man ")
#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


#进入消息循环

button1 = tkinter.Button(win, text ="按钮",
command=func,
width= 5 ,height = 1)

button1.pack()
button2 = tkinter.Button(win, text="按钮",command=lambda:print("yichenis a nice mam"))

button2.pack()
#退出
button3 = tkinter.Button(win, text="退出", command=win.quit)

button3.pack()

win.mainloop()

4,Entry控件输入内容,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

'''
Entry
输入控件
用于显示简单的文本内容
'''
#绑定变量
e = tkinter.Variable()
#show 密文显示 显示的内容可以自定义 show="*"
entry = tkinter.Entry(win, textvariable= e )

entry.pack()

#e就代表输入框这个对象
#设置值
e.set("yichen is a good man")

#取值 e.get() entry.get()
print(e.get())
print(entry.get())


win.mainloop()

5,点击按钮输出输入框的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

def showInfo():
print(entry.get())

entry = tkinter.Entry(win)

entry.pack()

button = tkinter.Button(win, text="按钮" ,command = showInfo)
button.pack()



win.mainloop()

6, Text控件显示多行文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

'''
文本控件, 用于显示多行文本
'''

str = '''On behalf of the great state of Illinois, crossroads of a nation,
land of Lincoln, let me express my deep gratitude for the privilege of addressing this
convention. Tonight is a particular honor for me because, let's face it,
my presence on this stage is pretty unlikely.
'''
#height显示的行数
text = tkinter.Text(win, width = 30, height = 4)

text.insert(tkinter.INSERT, str)
text.pack()




win.mainloop()

7,带滚动条的text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
#win.geometry("400x400+200+30")

'''
文本控件, 用于显示多行文本
'''

str = '''On behalf of the great state of Illinois, crossroads of a nation,
land of Lincoln, let me express my deep gratitude for the privilege of addressing this
convention. Tonight is a particular honor for me because, let's face it,
my presence on this stage is pretty unlikely.
'''

#创建滚动条
scroll = tkinter.Scrollbar()
#height显示的行数
text = tkinter.Text(win, width = 80, height = 4)

#side放到窗体的哪一侧,RIGHT右侧 fill填充
scroll.pack(side = tkinter.RIGHT, fill = tkinter.Y)
text.pack(side = tkinter.LEFT, fill= tkinter.Y)

#关联
scroll.config(command=text.yview) #滚动条动控制文本动
text.config(yscrollcommand=scroll.set) #文本动滚动条也关联着动

text.insert(tkinter.INSERT, str)
text.pack()



win.mainloop()

8,多选框控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import tkinter

win = tkinter.Tk()
win.title("逸尘")
win.geometry("400x400+200+20")


def updata():
message =""
if hobby1.get() == True:
message += "money\n"
if hobby2.get() == True:
message +="power\n"
if hobby3.get() == True:
message += "proson\n "
#清除text中的所有内容
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 ="person",variable = hobby3,command=updata)
check3.pack()

#多行文本宽度高度
text = tkinter.Text(win, width= 50, height = 6)
text.pack()

win.mainloop()

9,单选框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


def updata():
print(r.get())

#绑定变量 一组单选框要绑定同一个变量
r = tkinter.IntVar()

radio1 = tkinter.Radiobutton(win, text ="one", value= 1,variable= r,
command = updata)
radio1.pack()

radio2 = tkinter.Radiobutton(win, text ="two", value= 2, variable = r,
command= updata)
radio2.pack()

radio3 = tkinter.Radiobutton(win, text ="two", value= 3, variable = r,
command= updata)
radio3.pack()



win.mainloop()

10, Listbox列表框控件

1 BROWSE模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

'''
列表框控件,可以包含一个或多个文本框
作用: 在listbox控件的小窗口显示一个字符串

'''

#1,创建一个listbox,添加几个元素
lb = tkinter.Listbox(win, selectmode=tkinter.BROWSE)

lb.pack()

for item in ["item","nice", "handsome"]:
#按顺序添加
lb.insert(tkinter.END, item)
#在开始添加
lb.insert(tkinter.ACTIVE, "cool")
#将列表当成一个元素添加的
lb.insert(tkinter.ACTIVE, ["very good", "very nice"])

#删除 参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只删除第一个索引的内容
#lb.delete(1,3)
#lb.delete(1)

#选中 参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只选中第一个索引处的内容
#lb.select_set(2,5)
#lb.select_set(2)

#取消选中
#lb.select_clear(2,4)
#lb.select_clear(3)

#获取到列表中的元素的个数
#print(lb.size())
#从列表中取值 参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只获取第一个索引处的内容
#print(lb.get(2,4))
#print(lb.get(2))

#返回当前选中的的索引项,不是item元素
print(lb.curselection())

#判断 一个选项是否被选中
print(lb.selection_includes(1))
print(lb.selection_includes(3))

win.mainloop()

2, SINGLE模式,不支持鼠标移动选中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

#绑定变量
lbv = tkinter.StringVar()


#与BORWSE相似,但是不支持鼠标移动选中的位置
lb = tkinter.Listbox(win, selectmode=tkinter.SINGLE,listvariable=lbv)
lb.pack()

for item in ["item","nice", "handsome","vg","vn"]:
#按顺序添加
lb.insert(tkinter.END, item)

#打印当前列表中的选项
print(lbv.get( ))

#设置选项
#lbv.set(("1","2","3"))

#绑定事件

def myPrint(event):
print(lb.get(lb.curselection())) #打印内容

lb.bind("<Double-Button-1>",myPrint)


win.mainloop()

3, EXTENDED模式,支持shift连选,和control多选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
#win.geometry("400x400+200+30")

#绑定变量
lbv = tkinter.StringVar()
#EXTENDED 可以使listbox支持shift和Control
lb = tkinter.Listbox(win, selectmode=tkinter.EXTENDED,listvariable=lbv)


for item in ["item","nice", "handsome","vg","vn","good1","good2","hallo1","hello2","good2","hallo1","hello2"]:
#按顺序添加
lb.insert(tkinter.END, item)

#按住shift,可以实现连选
#按住control,可以实现多选

#滚动条
sc = tkinter.Scrollbar(win)
sc.pack(side=tkinter.RIGHT,fill = tkinter.Y)
lb.configure(yscrollcommand=sc.set)
lb.pack(side=tkinter.LEFT,fill= tkinter.BOTH)
sc['command'] = lb.yview




win.mainloop()

4, MULTIPLE 模式 支持多选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

#绑定变量
lbv = tkinter.StringVar()


#MULTIPLE 支持多选
lb = tkinter.Listbox(win, selectmode=tkinter.MULTIPLE,listvariable=lbv)
lb.pack()

for item in ["item","nice", "handsome","vg","vn"]:
#按顺序添加
lb.insert(tkinter.END, item)




win.mainloop()

11,Scale控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


'''
供用户通过拖拽指示器改变变量的值,可以水平,也可以竖直
tkinter.HORIZONTAL 水平方向
tkinter.VERTICAL 竖直 默认
length 水平是表示宽度,竖直是表示高度
tickinterval 选择值将会为改值的倍数
'''
scale = tkinter.Scale(win, from_=0 ,to = 100,
orient = tkinter.HORIZONTAL,tickinterval = 100,
length= 200)

scale.pack()

#设置值
scale.set(20)
def showNum():
print(scale.get())
tkinter.Button(win, text="打印", command=(showNum)).pack()



win.mainloop()

12, Spinbox 数值范围控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

'''
数值范围控件
to是最大值
from_最小值
increment 步长,默认是1
'''
# values 最好不要与from_=0,to=100,increment = 2同时使用
def updata():
print(v.get())
#command 只要值改变就会执行对应的方法
v = tkinter.StringVar()
sp = tkinter.Spinbox(win,from_=0 , to = 100,increment = 2 ,textvariable=v,command= updata)
#sp = tkinter.Spinbox(win,values=(0,2,4,6,8) )

sp.pack()

#设置值
v.set(20)
#取值
print(v.get())




win.mainloop()

13,Menu菜单

1,顶层菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

v = tkinter.StringVar()
#菜单条
menubar = tkinter.Menu(win)

win.config(menu=menubar)

def func():

print("逸尘 is a good man")
#创建一个菜单选项
menu1 = tkinter.Menu(menubar,tearoff=False)
menu2 = tkinter.Menu(menubar,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)
#向菜单条添加菜单选项
menubar.add_cascade(label="语言", menu=menu1)
menu2.add_command(label="red")
menu2.add_command(label="blue")
menu2.add_command(label="green")

menubar.add_cascade(label="颜色", menu=menu2)





win.mainloop()

2,鼠标右击菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


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)
if item == "退出":
#添加分隔线
menu.add_separator()
menu.add_command(label=item, command=win.quit)
else:
pass
menubar.add_cascade(label="语言", menu =menu)

def showMenu(event):
menubar.post(event.x_root,event.y_root)
win.bind("<Button-3>",showMenu)






win.mainloop()

14,Combobox下拉控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import tkinter
from tkinter import ttk
#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")
#绑定变量
cv = tkinter.StringVar()

com = ttk.Combobox(win,textvariable = cv)
com.pack()
#设置下拉数据
com["value"] = ("黑龙江","吉林","辽宁")
#设置默认值
com.current(0)

def func(envent):
print(com.get())
print(cv.get())
print("yichen is a good man")
#绑定事件
com.bind("<<ComboboxSelected>>", func)



win.mainloop()

15, Frame控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

'''
框架控件
在屏幕上显示一个矩形区域,多作为容器控件
'''

frm = tkinter.Frame(win)

frm.pack()

#left
frm_1 =tkinter.Frame(frm)
tkinter.Label(frm_1, text="左上",bg="pink").pack(side=tkinter.TOP)
tkinter.Label(frm_1, text="左下",bg="blue").pack(side=tkinter.TOP)
frm_1.pack(side= tkinter.LEFT)
#Right
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()

3,数据显示

1,表格数据显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tkinter
from tkinter import ttk
#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("600x400+200+30")

#表格
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="身高-height")
tree.heading("体重", text="体重-weight")

#添加数据
tree.insert("",0,text="line1", values=("路延续","29","178","80",))
tree.insert("",1,text="line2", values=("范冰冰","29","170","55",))
tree.insert("",2,text="line3", values=("罗总","29","177","67",))
tree.insert("",3,text="line4", values=("刘德华","29","169","57",))
win.mainloop()

2,树状数据显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tkinter
from tkinter import ttk
#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

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"))
treeF4 = tree.insert("",3, "日本", text= "日本JP", values=("F4"))

#二级树枝
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")
treeF1_4 =tree.insert(treeF1,3,"北京",text="中国北京", values="F1_4")

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")
treeF2_4 =tree.insert(treeF2,3,"华盛顿",text="美国华盛顿", values="F2_4")

#三级树枝
treeF1_1_1 = tree.insert(treeF1_1, 0, "哈尔滨", text = "黑龙江哈尔滨")
treeF1_1_2 = tree.insert(treeF1_1,1,"无常",text = "黑龙江无常")
win.mainloop()

3,绝对布局,相对布局,表格布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


label1 = tkinter.Label(win,text ="good",bg = "blue")
label2 = tkinter.Label(win,text ="nice",bg = "red")
label3 = tkinter.Label(win,text ="cool",bg = "pink")
label3 = tkinter.Label(win,text ="yichen",bg = "yellow")

#绝对布局 ,窗口的变化对位置没有变化
#label1.place(x=10,y = 10)
#label2.place(x=50,y = 50)
#label3.place(x=100, y = 100)

label4 = tkinter.Label(win,text ="good",bg = "blue")
label5 = tkinter.Label(win,text ="nice",bg = "red")
label6 = tkinter.Label(win,text ="cool",bg = "pink")

#相对布局 ,窗体改变对控件有影响
#tkinter.BOTH也是Y轴
#label4.pack(fill = tkinter.Y,side= tkinter.LEFT)
#label5.pack(fill = tkinter.X,side= tkinter.TOP)
#label6.pack(fill = tkinter.BOTH,side= tkinter.RIGHT)



#表格布局
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()

4,鼠标点击事件

事件 描述
<Bi-Motion> 当鼠标左键被按住在小控件且移动鼠标是事件发生i表示1,2,3
<Button-i> Button-1、Button-2、Button-3表明左键、中间键和右键、当在小控件上单击鼠标左键时,Tkinter 会自动抓到鼠标指针的位置,buttonPressed-i 是Button-i 的代名词
<ButtonRelease-i> 当释放鼠标左键时事件发生
<Double-Button-i> 当双击鼠标左键时事件发生分别对应(1,2,3)
<Enter> 当鼠标光标进入小控件时事件发生
<Key> 当单击一个键时事件发生
<Leave> 当鼠标光标离开小控件时事件发生
<Return> 当单击”Enter”键是事件发生,可以将键盘上的任意键(像“A”,”B”,”Up”,”Down”,”Left”,”Right”)和一个事件绑定
<Shift-A> 当单击”Shift-A” 键时事件发生,可以将Alt、Shift和Control和其他键组合
<Triple-Button-i> 当三次单击鼠标左键时事件发生

事件属性

事件属性 描述
char 从键盘输入的和按键事件相关的字符
keycode 从键盘输入的和按键事件相关的键的键代码(即统一码)
keysym 从键盘输入的和按键事件相关的键的键符号(即字符)
num 按键数字(1,2,3)表明按下的是哪个鼠标键
widget 触发这个事件的小控件对象
x和y 当前鼠标在小控件中以像素为单位的位置
x_root和y_root 当前鼠标相对于屏幕左上角的以像素为单位的位置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

#<Button-1>鼠标左键
#<Button-3>鼠标右键
#<Button-2> 鼠标中间
#当鼠标光标进入小控件是事件发生<Enter>
#<Leave>当鼠标光标离开小控件时事件发生
#<Triple-Button-i>当三次单击鼠标左键时事件发生i表示1,2,3
def func(event):
#参考鼠标事件属性,x和y ,输出对于小控件的坐标
print(event.x, event.y)
# tkinter.Button换成其他也可以,比如tkinter.Label
button1 = tkinter.Button(win, text="鼠标左键 点击")
#bind 给控件绑定事件
#button1.bind("<Button-1>",func)
#button1.bind("<Button-3>",func)
#button1.bind("<Button-2>",func)
button1.bind("<Key>",func)
#button1.bind("<Triple-Button-1>",func)

button1.pack()



win.mainloop()

5,鼠标移动和释放,进,离开时事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")


label = tkinter.Label(win,text="yichen is a good man" ,bg="red")
label.pack()
#鼠标移动事件
#<B1-Motion> 鼠标左键移动
#<B2-Motion> 鼠标中键移动
#<B3-Motion> 鼠标右键移动
def func(event):
print(event.x,event.y)

#label.bind("<B1-Motion>" ,func)
#label.bind("<B2-Motion>" ,func)
#label.bind("<B3-Motion>" ,func)

#鼠标释放事件
#<ButtonRelease-1>释放鼠标左键
#<ButtonRelease-2>释放鼠标中键
#<ButtonRelease-3>释放鼠标右键
label.bind("<ButtonRelease-1>" ,func)


#进入事件
#<Enter>当鼠标光标进入控件时触发

label.bind("<Enter>" ,func)
#鼠标离开控件事件

label.bind("<Leave>" ,func)

win.mainloop()

6,响应所有按键和特殊事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

def func(event):
#键盘输入什么就显示什么
print("event.char=", event.char)
print("你输入的keycode值==", event.keycode)
# tkinter.Button换成其他也可以,比如tkinter.Label

#<Key>响应所有的按键
#button1 = tkinter.Button(win, text="响应鼠标所有事件的测试")
label2 = tkinter.Label(win, text="响应鼠标特殊事件的测试")
#必须设置焦点
#button1.focus_set()

#button1.bind("<Key>",func)
#button1.bind("<Triple-Button-1>",func)
#给主窗口绑定控件可以不用设置焦点
#win.bind("<Key>",func)
#button1.pack()

#响应特殊按键事件
#<Shift_L> 左shift
#<Shift_R> 右shift
#<F5>
#<Return> 回车
#<BackSpace 退格
label2.pack()
label2.bind("<Shift_L>",func)
label2.bind("<Shift_R>",func)
label2.bind("<Return>",func)
label2.bind("<BackSpace>",func)

win.mainloop()

7,响应指定按键和组合按键事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import tkinter

#创建主窗口
win = tkinter.Tk()

#设置标题
win.title("逸尘")
#设置大小和位置
win.geometry("400x400+200+30")

def func(event):
#键盘输入什么就显示什么
print("event.char=", event.char)
print("你输入的keycode值==", event.keycode)

#指定a
win.bind("a",func)

#组合按键事件
#<Control-Alt-a>"
#<Shift-Up>
#<Control-p>
win.bind("<Control-p>",func)
win.bind("<Shift-Up>",func)
win.bind("<Shift-Up>",func)




win.mainloop()

评论


:D 一言句子获取中...

加载中,最新评论有1分钟缓存...