tkinter python(图形开发界面)

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

1、简单实例

  下面的代码是创建出一个窗口,其他的操作就在这个平台上进行。执行之后会在桌面弹出一个窗口,窗口的标题就是代码中设置的win.title。这里说一下,我使用的版本是python3.6。后面的内容尽量按顺序看,后面的控件也许用到前面写到的东西。

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import tkinter
 5 
 6 
 7 # 创建主窗口
 8 win = tkinter.Tk()
 9 # 设置标题
10 win.title("yudanqu")
11 # 设置大小和位置
12 win.geometry("400x400+200+50")
13 
14 # 进入消息循环,可以写控件
15 
16 win.mainloop()

2、Label控件

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import tkinter
 5 
 6 win = tkinter.Tk()
 7 win.title("yudanqu")
 8 win.geometry("400x400+200+50")
 9 
10 '''
11 Label:标签控件,可以显示文本
12 '''
13 # win:父窗体
14 # text:显示的文本内容
15 # bg:背景色
16 # fg:字体颜色
17 # font:字体
18 # wraplength:指定text文本中多宽之后换行
19 # justify:设置换行后的对齐方式
20 # anchor:位置 n北,e东,w西,s南,center居中;还可以写在一起:ne东北方向
21 label = tkinter.Label(win,
22                       text="this is a word",
23                       bg="pink", fg="red",
24                       font=("黑体", 20),
25                       width=20,
26                       height=10,
27                       wraplength=100,
28                       justify="left",
29                       anchor="ne")
30 
31 # 显示出来
32 label.pack()
33 
34 
35 win.mainloop()

3、Button控件

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import tkinter
 5 
 6 
 7 def func():
 8     print("aaaaaaaaaaaaaaaaaaaaaaa")
 9 
10 
11 win = tkinter.Tk()
12 win.title("yudanqu")
13 win.geometry("400x400+200+50")
14 
15 
16 # 创建按钮
17 button1 = tkinter.Button(win, text="按钮", command=func, width=10, height=10)
18 button1.pack()
19 
20 button2 = tkinter.Button(win, text="按钮", command=lambda: print("bbbbbbbbbbbb"))
21 button2.pack()
22 
23 button3 = tkinter.Button(win, text="退出", command=win.quit)
24 button3.pack()
25 
26 win.mainloop()

4、Entry控件

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import tkinter
 5 
 6 win = tkinter.Tk()
 7 win.title("yudanqu")
 8 win.geometry("400x400+200+50")
 9 
10 '''
11 Entry:输入控件,用于显示简单的文本内容
12 '''
13 
14 # 密文显示
15 entry1 = tkinter.Entry(win, show="*") # show="*" 可以表示输入密码
16 entry1.pack()
17 
18 # 绑定变量
19 e = tkinter.Variable()
20 
21 entry2 = tkinter.Entry(win, textvariable=e)
22 entry2.pack()
23 
24 # e就代表输入框这个对象
25 # 设置值
26 e.set("wewewewewewe")
27 # 取值
28 print(e.get())
29 print(entry2.get())
30 
31 win.mainloop()

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

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import tkinter
 4 
 5 win = tkinter.Tk()
 6 win.title("yudanqu")
 7 win.geometry("400x400+200+50")
 8 
 9 def showinfo():
10     # 获取输入的内容
11     print(entry.get())
12 
13 entry = tkinter.Entry(win)
14 entry.pack()
15 
16 button = tkinter.Button(win, text="按钮", command=showinfo)
17 button.pack()
18 
19 win.mainloop()

6、Text控件

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import tkinter
 4 
 5 win = tkinter.Tk()
 6 win.title("yudanqu")
 7 win.geometry("400x400+200+50")
 8 
 9 '''
10 文本控件:用于显示多行文本
11 '''
12 
13 # height表示的是显示的行数
14 text = tkinter.Text(win, width=30, height=10)
15 text.pack()
16 
17 str = '''盖闻天地之数,有十二万九千六百岁为一元。将一元分为十二会,乃子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥之十二支也。每会该一万八百岁。且就一日而论:子时得阳气,而丑则鸡鸣;寅不通光,而卯则日出;辰时食后,而巳则挨排;日午天中,而未则西蹉;申时晡而日落酉;戌黄昏而人定亥。譬于大数,若到戌会之终,则天地昏蒙而万物否矣。再去五千四百岁,交亥会之初,则当黑暗,而两间人物俱无矣,故曰混沌。又五千四百岁,亥会将终,贞下起元,近子之会,而复逐渐开明。邵康节曰:“冬至子之半,天心无改移。一阳初动处,万物未生时。”到此,天始有根。再五千四百岁,正当子会,轻清上腾,有日,有月,有星,有辰。日、月、星、辰,谓之四象。故曰,天开于子。又经五千四百岁,子会将终,近丑之会,而逐渐坚实。易曰:“大哉乾元!至哉坤元!万物资生,乃顺承天。”至此,地始凝结。再五千四百岁,正当丑会,重浊下凝,有水,有火,有山,有石,有土。水、火、山、石、土谓之五形。故曰,地辟于丑。又经五千四百岁,丑会终而寅会之初,发生万物。'''
18 
19 text.insert(tkinter.INSERT, str)
20 
21 win.mainloop()

7、带滚动条的Text

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 
 5 import tkinter
 6 
 7 win = tkinter.Tk()
 8 win.title("yudanqu")
 9 # win.geometry("400x400+200+50")
10 
11 '''
12 文本控件:用于显示多行文本
13 '''
14 
15 # 创建滚动条
16 scroll = tkinter.Scrollbar()
17 
18 text = tkinter.Text(win, width=30, height=10)
19 # side放到窗体的哪一侧,  fill填充
20 scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
21 text.pack(side=tkinter.LEFT, fill=tkinter.Y)
22 # 关联
23 scroll.config(command=text.yview)
24 text.config(yscrollcommand=scroll.set)
25 
26 text.pack()
27 
28 str = '''盖闻天地之数,有十二万九千六百岁为一元。将一元分为十二会,乃子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥之十二支也。每会该一万八百岁。且就一日而论:子时得阳气,而丑则鸡鸣;寅不通光,而卯则日出;辰时食后,而巳则挨排;日午天中,而未则西蹉;申时晡而日落酉;戌黄昏而人定亥。譬于大数,若到戌会之终,则天地昏蒙而万物否矣。再去五千四百岁,交亥会之初,则当黑暗,而两间人物俱无矣,故曰混沌。又五千四百岁,亥会将终,贞下起元,近子之会,而复逐渐开明。邵康节曰:“冬至子之半,天心无改移。一阳初动处,万物未生时。”到此,天始有根。再五千四百岁,正当子会,轻清上腾,有日,有月,有星,有辰。日、月、星、辰,谓之四象。故曰,天开于子。又经五千四百岁,子会将终,近丑之会,而逐渐坚实。易曰:“大哉乾元!至哉坤元!万物资生,乃顺承天。”至此,地始凝结。再五千四百岁,正当丑会,重浊下凝,有水,有火,有山,有石,有土。水、火、山、石、土谓之五形。故曰,地辟于丑。又经五千四百岁,丑会终而寅会之初,发生万物。'''
29 
30 text.insert(tkinter.INSERT, str)
31 
32 win.mainloop()

8、Checkbutton多选框控件

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import tkinter
 4 
 5 win = tkinter.Tk()
 6 win.title("yudanqu")
 7 win.geometry("400x400+200+50")
 8 
 9 def updata():
10     message = ""
11     if hobby1.get() ==True:
12         message += "money\n"
13     if hobby2.get() == True:
14         message += "power\n"
15     if hobby3.get() == True:
16         message += "people\n"
17 
18     # 清空text中所有内容
19     text.delete(0.0, tkinter.END)
20     text.insert(tkinter.INSERT, message)
21 
22 # 要绑定的变量
23 hobby1 = tkinter.BooleanVar()
24 # 多选框
25 check1 = tkinter.Checkbutton(win, text="money", variable=hobby1, command=updata)
26 check1.pack()
27 hobby2 = tkinter.BooleanVar()
28 check2 = tkinter.Checkbutton(win, text="power", variable=hobby2, command=updata)
29 check2.pack()
30 hobby3 = tkinter.BooleanVar()
31 check3 = tkinter.Checkbutton(win, text="people", variable=hobby3, command=updata)
32 check3.pack()
33 
34 text = tkinter.Text(win, width=50, height=5)
35 text.pack()
36 
37 win.mainloop()

9、Radiobutton单选框

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 def updata():
 8     print(r.get())
 9 
10 # 绑定变量,一组单选框要绑定同一个变量,就能区分出单选框了
11 r = tkinter.IntVar()
12 
13 radio1 = tkinter.Radiobutton(win, text="one", value=1, variable=r, command=updata)
14 radio1.pack()
15 radio2 = tkinter.Radiobutton(win, text="two", value=2, variable=r, command=updata)
16 radio2.pack()
17 
18 win.mainloop()

10、Listbox控件一

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import tkinter
 4 
 5 win = tkinter.Tk()
 6 win.title("yudanqu")
 7 win.geometry("400x400+200+50")
 8 
 9 '''
10 列表框控件:可以包含一个或多个文本框
11 作用:在listbox控件的小窗口显示一个字符串
12 '''
13 
14 # 创建一个listbox,添加几个元素
15 lb = tkinter.Listbox(win, selectmode=tkinter.BROWSE)
16 lb.pack()
17 
18 for item in ["good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd"]:
19     # 按顺序添加
20     lb.insert(tkinter.END, item)
21 
22 # 在开始添加
23 lb.insert(tkinter.ACTIVE, "cool")
24 
25 # 将列表当做一个元素添加
26 # lb.insert(tkinter.END, ["very good", "very nice"])
27 
28 # 删除:参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只删除第一个索引处的内容
29 lb.delete(1,3)
30 
31 # 选中:参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只选中第一个索引处的内容
32 lb.select_set(2,3)
33 
34 # 取消选中:参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只取消第一个索引处的内容
35 lb.select_clear(2)
36 
37 # 获取到列表中的元素个数
38 print(lb.size())
39 # 获取值
40 print(lb.get(2,3))
41 
42 # 返回当前的索引项,不是item元素
43 print(lb.curselection())
44 
45 # 判断:一个选项是否被选中
46 print(lb.selection_includes(3))
47 
48 win.mainloop()

 11、Listbox控件二

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # 绑定变量
 8 lbv = tkinter.StringVar()
 9 
10 # 与BORWSE相似,但是不支持鼠标按下后移动选中位置
11 lb = tkinter.Listbox(win, selectmode=tkinter.SINGLE, listvariable=lbv)
12 lb.pack()
13 
14 for item in ["good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd"]:
15     # 按顺序添加
16     lb.insert(tkinter.END, item)
17 
18 # 打印当前列表中的选型
19 print(lbv.get())
20 
21 # 设置选项
22 # lbv.set(("1","2","3"))
23 
24 # 绑定事件
25 def myprint(event):
26     # print(lb.curselection()) # 返回下标
27     print(lb.get(lb.curselection())) # 返回值
28 lb.bind("<Double-Button-1>", myprint)
29 
30 win.mainloop()

12、Listbox控件三

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 # win.geometry("400x400+200+50")
 6 
 7 # EXTENDED:可以使listbox支持shift和Ctrl
 8 lb = tkinter.Listbox(win, selectmode=tkinter.EXTENDED)
 9 lb.pack()
10 
11 for item in ["good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd", "good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd", "good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd", "good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd", "good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd"]:
12     # 按顺序添加
13     lb.insert(tkinter.END, item)
14 
15 # 滚动条
16 sc = tkinter.Scrollbar(win)
17 sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
18 # 配置
19 lb.configure(yscrollcommand=sc.set)
20 lb.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
21 # 额外给属性赋值
22 sc["command"] = lb.yview
23 
24 win.mainloop()

13、Listbox四

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # MULTIPLE支持多选
 8 lb = tkinter.Listbox(win, selectmode=tkinter.MULTIPLE)
 9 lb.pack()
10 
11 for item in ["good", "nice", "handsome", "aaa", "bbb", "ccc", "ddd"]:
12     # 按顺序添加
13     lb.insert(tkinter.END, item)
14 
15 win.mainloop()

14、Scale控件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 '''
 8 供用户通过拖拽指示器来改变变量的值,可以水平,也可以竖直
 9 '''
10 
11 # tkinter.HORIZONTAL水平
12 # tkinter.VERTICAL 竖直(默认)
13 # length:水平时表示宽度,竖直时表示高度
14 # tickintervar :选择值将会为该值得倍数
15 
16 scale1 = tkinter.Scale(win, from_=0, to=100, orient=tkinter.HORIZONTAL, tickinterval=10, length=200)
17 scale1.pack()
18 
19 # 设置值
20 scale1.set(20)
21 
22 # 取值
23 # print(scale1.get())
24 
25 def showNum():
26     print(scale1.get())
27 tkinter.Button(win, text="按钮", command=showNum).pack()
28 
29 win.mainloop()

15、Spinbox控件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 '''
 8 数值范围控件
 9 '''
10 
11 # 绑定变量
12 v = tkinter.StringVar()
13 
14 def updata():
15     print(v.get())
16 
17 # increment:步长,默认为1
18 # values要输入一个元组 最好不要和from和to同时使用,而且步长也没用
19 # command 只要值改变就会执行updata方法
20 sp = tkinter.Spinbox(win, from_=0, to=100, increment=5,
21                      textvariable=v, command=updata)
22 # sp = tkinter.Spinbox(win, values=(0,2,4,6,8))
23 sp.pack()
24 
25 # 赋值
26 v.set(20)
27 # 取值
28 print(v.get())
29 
30 win.mainloop()

16、Menu顶层菜单

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # 菜单条
 8 menubar = tkinter.Menu(win)
 9 win.config(menu=menubar)
10 
11 def func():
12     print("**********")
13 
14 # 创建一个菜单选项
15 menu1 = tkinter.Menu(menubar, tearoff=False)
16 
17 # 给菜单选项添加内容
18 for item in ['python','c','java','c++', 'c#','php','B','退出']:
19     if item == '退出':
20         # 添加分割线
21         menu1.add_separator()
22         menu1.add_command(label=item, command=win.quit)
23     else:
24         menu1.add_command(label=item, command=func)
25 
26 # 向菜单条上添加菜单选项
27 menubar.add_cascade(label='语言', menu=menu1)
28 
29 win.mainloop()

17、Menu鼠标右键菜单

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # 菜单条
 8 menubar = tkinter.Menu(win)
 9 
10 def func():
11     print("**********")
12 
13 # 菜单
14 menu = tkinter.Menu(menubar, tearoff=False)
15 # 给菜单选项添加内容
16 for item in ['python','c','java','c++', 'c#','php','B','退出']:
17     if item == '退出':
18         # 添加分割线
19         menu.add_separator()
20         menu.add_command(label=item, command=win.quit)
21     else:
22         menu.add_command(label=item, command=func)
23 
24 menubar.add_cascade(label="语言", menu=menu)
25 
26 def showMenu(event):
27     menubar.post(event.x_root, event.y_root)
28 
29 win.bind("<Button-3>", showMenu)
30 
31 win.mainloop()

18、Combobox下拉控件

 1 import tkinter
 2 from tkinter import ttk
 3 
 4 win = tkinter.Tk()
 5 win.title("yudanqu")
 6 win.geometry("400x400+200+50")
 7 
 8 # 绑定变量
 9 cv = tkinter.StringVar()
10 
11 com = ttk.Combobox(win, textvariable=cv)
12 com.pack()
13 
14 # 设置下拉数据
15 com["value"] = ("黑龙江", "吉林", "辽宁")
16 
17 # 设置默认值
18 com.current(0)
19 
20 # 绑定事件
21 
22 def func(event):
23     print(com.get())
24     print(cv.get())
25 com.bind("<<ComboboxSelected>>", func)
26 
27 win.mainloop()

19、Frame控件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 '''
 8 框架控件:在桌面上显示一个矩形区域,多作为一个容器控件
 9 '''
10 
11 frm = tkinter.Frame(win)
12 frm.pack()
13 
14 # left
15 frm_l = tkinter.Frame(frm)
16 tkinter.Label(frm_l, text="左上", bg="pink").pack(side=tkinter.TOP)
17 tkinter.Label(frm_l, text="左下", bg="blue").pack(side=tkinter.TOP)
18 frm_l.pack(side=tkinter.LEFT)
19 
20 # right
21 frm_r = tkinter.Frame(frm)
22 tkinter.Label(frm_r, text="右上", bg="green").pack(side=tkinter.TOP)
23 tkinter.Label(frm_r, text="右下", bg="red").pack(side=tkinter.TOP)
24 frm_r.pack(side=tkinter.RIGHT)
25 
26 win.mainloop()

20、表格数据

 1 import tkinter
 2 from tkinter import ttk
 3 
 4 win = tkinter.Tk()
 5 win.title("yudanqu")
 6 win.geometry("600x400+200+50")
 7 
 8 # 表格
 9 tree = ttk.Treeview(win)
10 tree.pack()
11 
12 # 定义列
13 tree["columns"] = ("姓名","年龄","身高","体重")
14 # 设置列,列还不显示
15 tree.column("姓名", width=100)
16 tree.column("年龄", width=100)
17 tree.column("身高", width=100)
18 tree.column("体重", width=100)
19 
20 # 设置表头
21 tree.heading("姓名", text="姓名-name")
22 tree.heading("年龄", text="年龄-age")
23 tree.heading("身高", text="身高-height")
24 tree.heading("体重", text="体重-weight")
25 
26 # 添加数据
27 tree.insert("", 0, text="line1", values=("小郑","34","177cm","70kg"))
28 tree.insert("", 1, text="line2", values=("小张","43","188cm","90kg"))
29 
30 win.mainloop()

21、树状数据

 1 import tkinter
 2 from tkinter import ttk
 3 
 4 win = tkinter.Tk()
 5 win.title("yudanqu")
 6 win.geometry("400x400+200+50")
 7 
 8 tree = ttk.Treeview(win)
 9 tree.pack()
10 
11 # 添加一级树枝
12 treeF1 = tree.insert("",0,"中国",text= "中国Chi", values=("F1"))
13 treeF2 = tree.insert("",1,"美国",text= "美国USA", values=("F1"))
14 treeF3 = tree.insert("",2,"英国",text= "英国UK", values=("F1"))
15 
16 # 二级树枝
17 treeF1_1 = tree.insert(treeF1,0,"黑龙江",text="中国黑龙江",values=("F1_1"))
18 treeF1_2 = tree.insert(treeF1,1,"吉林",text="中国吉林",values=("F1_2"))
19 treeF1_3 = tree.insert(treeF1,2,"辽宁",text="中国辽宁",values=("F1_3"))
20 
21 treeF2_1 = tree.insert(treeF2,0,"aaa",text="aaaa",values=("F2_1"))
22 treeF2_2 = tree.insert(treeF2,1,"bbb",text="bbbb",values=("F2_2"))
23 treeF2_3 = tree.insert(treeF2,2,"ccc",text="cccc",values=("F2_3"))
24 
25 # 三级树枝
26 treeF1_1_1 = tree.insert(treeF1_1,0,"哈尔滨",text="黑龙江哈尔滨")
27 treeF1_1_2 = tree.insert(treeF1_1,1,"五常",text="黑龙江五常")
28 
29 win.mainloop()

22、绝对布局

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 label1 = tkinter.Label(win, text="good", bg="blue")
 8 label2 = tkinter.Label(win, text="nice", bg="red")
 9 label3 = tkinter.Label(win, text="cool", bg="green")
10 
11 # 绝对布局,窗口的变化对位置没有影响
12 label1.place(x=10,y=10)
13 label2.place(x=50,y=50)
14 label3.place(x=100,y=100)
15 
16 win.mainloop()

23、相对布局

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 label1 = tkinter.Label(win, text="good", bg="blue")
 8 label2 = tkinter.Label(win, text="nice", bg="red")
 9 label3 = tkinter.Label(win, text="cool", bg="green")
10 
11 # 相对布局,窗体改变对控件有影响
12 label1.pack(fill=tkinter.Y, side=tkinter.LEFT)
13 label2.pack(fill=tkinter.X, side=tkinter.TOP)
14 label3.pack()
15 
16 win.mainloop()

24、表格布局

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 label1 = tkinter.Label(win, text="good", bg="blue")
 8 label2 = tkinter.Label(win, text="nice", bg="red")
 9 label3 = tkinter.Label(win, text="cool", bg="green")
10 label4 = tkinter.Label(win, text="handsome", bg="yellow")
11 
12 # 表格布局
13 label1.grid(row=0,column=0)
14 label2.grid(row=0,column=1)
15 label3.grid(row=1,column=0)
16 label4.grid(row=1,column=1)
17 
18 win.mainloop()

25、鼠标点击事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 
 8 def func(event):
 9     print(event.x, event.y)
10 
11 # <Button-1>  鼠标左键
12 # <Button-2>  鼠标滚轮
13 # <Button-1>  鼠标右键
14 # <Double-Button-1>  鼠标双击左键
15 # <Triple-Button-1>  鼠标三击左键
16 
17 button1 = tkinter.Button(win, text="leftmouse button")
18 # bind 给控件绑定数据(参数一是绑定的事件,参数二是触发事件的函数)
19 button1.bind("<Button-1>", func)
20 button1.pack()
21 
22 win.mainloop()

26、鼠标移动事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 
 8 # <B1-Motion>  左键移动
 9 # <B2-Motion>  中键移动
10 # <B3-Motion>  右键移动
11 
12 
13 label = tkinter.Label(win, text="*********")
14 label.pack()
15 
16 def func(event):
17     print(event.x, event.y)
18 
19 label.bind("<B1-Motion>", func)
20 
21 win.mainloop()

27、鼠标释放事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # <ButtonRelease-1> 释放鼠标左键
 8 # <ButtonRelease-2> 释放鼠标中键
 9 # <ButtonRelease-3> 释放鼠标右键
10 
11 
12 label = tkinter.Label(win, text="*********", bg="red")
13 label.pack()
14 
15 def func(event):
16     print(event.x, event.y)
17 
18 label.bind("<ButtonRelease-1>", func)
19 
20 win.mainloop()

28、进入和离开事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # <Enter>  当鼠标进入控件时触发事件
 8 # <Leave>  当鼠标离开控件时触发事件
 9 
10 label = tkinter.Label(win, text="*********", bg="red")
11 label.pack()
12 
13 def func(event):
14     print(event.x, event.y)
15 
16 label.bind("<Enter>", func)
17 label.bind("<Leave>", func)
18 
19 
20 win.mainloop()

29、响应所有按键的事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # <Key>  响应所有的按键(要有焦点)
 8 
 9 label = tkinter.Label(win, text="*********", bg="red")
10 # 设置焦点
11 label.focus_set()
12 label.pack()
13 
14 def func(event):
15     print("event.char=", event.char)
16     print("event.keycode=", event.keycode)
17 
18 label.bind("<Key>", func)
19 
20 win.mainloop()

30、响应特殊按键事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 # <Shift_L>  只响应左侧的shift键
 8 # <Shift_R>
 9 # <F5>
10 # <Return>  也就是回车键
11 # <BackSpace>  返回,也就是退格键
12 
13 label = tkinter.Label(win, text="*********", bg="red")
14 # 设置焦点
15 label.focus_set()
16 label.pack()
17 
18 def func(event):
19     print("event.char=", event.char)
20     print("event.keycode=", event.keycode)
21 
22 
23 label.bind("<Shift_L>", func)
24 
25 
26 win.mainloop()

31、指定按键事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 
 8 label = tkinter.Label(win, text="*********", bg="red")
 9 # 设置焦点
10 label.focus_set()
11 label.pack()
12 
13 def func(event):
14     print("event.char=", event.char)
15     print("event.keycode=", event.keycode)
16 
17 
18 win.bind("a", func) # 注意前面改成了win,只需要写出按键名即可
19 
20 
21 win.mainloop()

32、组合按键事件

 1 import tkinter
 2 
 3 win = tkinter.Tk()
 4 win.title("yudanqu")
 5 win.geometry("400x400+200+50")
 6 
 7 
 8 # <Control-Alt-a>
 9 # <Shift-Up>
10 # 只是control+alt不行
11 
12 label = tkinter.Label(win, text="*********", bg="red")
13 # 设置焦点
14 label.focus_set()
15 label.pack()
16 
17 def func(event):
18     print("event.char=", event.char)
19     print("event.keycode=", event.keycode)
20 
21 
22 win.bind("<Control-Alt-a>", func) # 注意前面改成了win,只需要写出按键名即可
23 
24 
25 win.mainloop()

 

posted @ 2018-08-14 21:05 渔单渠 阅读(...) 评论(...) 编辑 收藏