00表格数据
import tkinter
from tkinter import ttkwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")#表格
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 = ("001","21","183","70"))
tree.insert("",1,text = "line2",values = ("002","21","183","70"))win.mainloop()
01树状数据
import tkinter
from tkinter import ttk
win = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")tree = ttk.Treeview(win)
tree.pack()#添加一级树枝
treeF1 = tree.insert("",0,"中国",text = "China",values= ("F1"))
treeF2 = tree.insert("",1,"美国",text = "USA",values= ("F2"))
treeF3 = tree.insert("",2,"英国",text = "UK",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 = "黑龙江哈尔滨",values=("F1_1"))win.mainloop()
02绝对布局
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")label1 = tkinter.Label(win,text = "good ",bg = "blue")
label2 = tkinter.Label(win,text = "nice ",bg = "red")
label3 = tkinter.Label(win,text = "cool ",bg = "black")#绝对布局,窗口的变化对位置没有影响
label1.place(x= 10,y=10)
label2.place(x= 50,y=50)
label3.place(x= 100,y=100)win.mainloop()
03相对布局
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")label1 = tkinter.Label(win,text = "good ",bg = "blue")
label2 = tkinter.Label(win,text = "nice ",bg = "red")
label3 = tkinter.Label(win,text = "cool ",bg = "black")
# tkinter.X \Y \BOTH
label1.pack(fill = tkinter.Y,side= tkinter.LEFT)
label2.pack(fill = tkinter.X,side= tkinter.TOP)
label3.pack()win.mainloop()表格布局!!!import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")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 = "handesome ",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()
04鼠标点击事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")#鼠标左键<Button-1>
# 鼠标中键<Button-2>
# 鼠标右键<Button-3>
# 鼠标左键双击<Double-Button-1>
# 鼠标左键三击<Triple-Button-1>def func(event):print(event.x,event.y)
button1= tkinter.Button(win,text="leftmouse button")
button1.bind("<Triple-Button-1>",func)button1.pack()win.mainloop()
05鼠标移动事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
#左键按住移动
label = tkinter.Label(win,text = "liuwang is handsome")
label.pack()
#绑定事件
def func(event):print(event.x,event.y)
label.bind("<B1-Motion>",func)win.mainloop()
06鼠标释放事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")#label.bind("<ButtonRelease-1>",func) 释放鼠标左键
#label.bind("<ButtonRelease-2>",func) 释放鼠标中键
#label.bind("<ButtonRelease-3",func) 释放鼠标右键
label = tkinter.Label(win,text = "liuwang is handsome ",bg = "blue")
def func(event):print(event.x,event.y)
label.bind("<ButtonRelease-1>",func)
label.pack()win.mainloop()
07进入与离开事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
# 进入事件
#<Enter>鼠标光标进入空间是触发
#<Leave>鼠标光标来开空间是触发
label = tkinter.Label(win,text = "liuwang is handsome ",bg = "blue")
def func(event):print(event.x,event.y)
label.bind("<Enter>",func)
label.pack()win.mainloop()
08响应所有按键事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")label = tkinter.Label(win,text = "liuwang is handsome ",bg = "blue")#设置焦点(键盘事件作用在焦点身上)
label.focus_set()
label.pack()def func(event):print("event.char = ",event.char)print("event.keycode = ", event.keycode)
label.bind("<Key>",func)
win.mainloop()
09响应特殊按键事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")label = tkinter.Label(win,text = "liuwang is handsome ",bg = "blue")#设置焦点(键盘事件作用在焦点身上)
label.focus_set()
label.pack()# label.bind("<Shift_L>) 左侧shift响应 ***下划线
# label.bind("<Shift_L>)
# label.bind("<F5>)
# label.bind("<Return>") 回车
# <BackeSpcae>def func(event):print("event.char = ",event.char)print("event.keycode = ", event.keycode)
label.bind("<Shift_L>",func)
win.mainloop()
10指定按键事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")def func(event):print("event.char = ",event.char)print("event.keycode = ", event.keycode)win.bind("a",func)win.mainloop()
11组合按键事件
import tkinterwin = tkinter.Tk()
win.title("Liuwang")
win.geometry("400x400+200+20")
#<Control-Alt-b> 组合按键
# <Control-Alt> 无法实现def func(event):print("event.char = ",event.char)print("event.keycode = ", event.keycode)win.bind("<Control-Alt-b>",func)win.mainloop()