目录
1、鼠标点击事件:
2、鼠标移动事件:
3、鼠标释放事件:
4、组合按键:
5、相对布局:
6、绝对布局:
7、表格布局:
8、表格数据:
9、树状层级目录:
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
#鼠标左键
#鼠标右键
#鼠标滑轮
#鼠标左键双击
#鼠标中键双击
#鼠标右键双击
#鼠标左键三击
def func(event):
print(event.x,event.y)
button1 = tkinter.Label(win,text="leftmouse button")
#bind 给控件绑定事件
button1.bind("",func)
button1.pack()
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
label = tkinter.Label(win,text="hello")
label.pack()
#左键移动
#右键移动
#中键移动
def func(event):
print(event.x,event.y)
label.bind("",func)
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
label = tkinter.Label(win,text="sunck is a man",bg="red")
label.pack()
#释放鼠标左键
#释放鼠标右键
#释放鼠标中键
def func(event):
print(event.x,event.y)
label.bind("",func)
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
def func(e):
print("e.char=",e.char)
print("e.keycode=",e.keycode)
win.bind("",func)
#同时按下shift和方向上键
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
label1 = tkinter.Label(win,text="good",bg="blue")
label2 = tkinter.Label(win,text="nice",bg="red")
label3 = tkinter.Label(win,text="cool",bg="pink")
#相对布局
#Both = Y
label1.pack(fill = tkinter.Y,side=tkinter.LEFT)
label2.pack(fill = tkinter.X,side=tkinter.TOP)
label3.pack()
win.mainloop()
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
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()
import tkinter
win = tkinter.Tk()
win.title("窗体")
win.geometry("400x400+200+50")
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="green")
#表格布局:
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()
import tkinter
from tkinter import ttk
win = tkinter.Tk()
win.title("窗体")
win.geometry("600x400+200+50")
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="体重-wight")
#添加数据
tree.insert("",0,text="line1",values=("dx","22","166","55"))
tree.insert("",1,text="line1",values=("ll","21","170","66"))
win.mainloop()
主函数:
import tkinter
import os
from treeWindows import TreeWindows
from infoWindows import InfoWindows
win = tkinter.Tk()
win.title("窗体")
win.geometry("600x400+200+50")
path = "F:\\学习存放\\untitled"
infoWin = InfoWindows(win)
treeWin = TreeWindows(win,path,infoWin)
信息窗口:
import tkinter
from tkinter import ttk
import os
class InfoWindows(tkinter.Frame):
def __init__(self,master):
frame = tkinter.Frame(master)
frame.grid(row=0, column=1)
self.ev = tkinter.Variable()
#创建一个Entry
self.entry = tkinter.Entry(frame,textvariable=self.ev)
self.entry.pack()
#创建一个text
self.txt=tkinter.Text(frame)
self.txt.pack()
树状目录窗口:
import tkinter
from tkinter import ttk
import os
class TreeWindows(tkinter.Frame):
def __init__(self,master,path,otherWin):
frame = tkinter.Frame(master)
frame.grid(row=0, column=0)
self.otherWin = otherWin
self.tree = ttk.Treeview(frame)
self.tree.pack(side=tkinter.LEFT,fill=tkinter.Y)
self.tree.pack()
root = self.tree.insert("","end",text=self.getLastPath(path),open=True,
values=(path))
self.loadTree(root,path)
#滚动条
self.sy = tkinter.Scrollbar(frame)
self.sy.pack(side=tkinter.RIGHT,fill=tkinter.Y)
self.sy.config(command=self.tree.yview)
self.tree.config(yscrollcommand=self.sy.set)
#绑定事件
self.tree.bind("<>",self.func)
def func(self,event):
#widget触发事件的构建
self.v = event.widget.selection()
for sv in self.v:
file = self.tree.item(sv)["text"]
self.otherWin.ev.set(file)
apath = self.tree.item(sv)["values"][0]
print(apath)
def loadTree(self,parent,parentPath):
for FileName in os.listdir(parentPath):
absPath = os.path.join(parentPath,FileName)
#插入树枝
treey = self.tree.insert(parent,"end",text=self.getLastPath(absPath),values=(absPath))
#判断是否是目录
if os.path.isdir(absPath):
self.loadTree(treey,absPath)
def getLastPath(self,path):
pathList = os.path.split(path)
return pathList[-1]
效果图: