1.创建一个窗口
# 导入模块
import tkinterwin.mainloop()
2.文本控件 Lable
#创建并显示窗口
import tkinter
win = tkinter.Tk()
win.title("Label标签")
win.geometry("300x300+300+300")
# 创建label标签
# 参数1表示的就是父容器, 就是窗口win
# text 指定的是文本内容
# height 指定的是标签的高度, 单位约是字符的高度
# weidth 指定标签的宽度,
# bg 指定背景色
# fg 指定字体颜色
# font 设置字体,和大小
label = tkinter.Label(win,text="胖胖 is a good man",width=6,height = 1,bg="#ffcccc",
fg="#ff0000",font=("宋体",30)
)
# 显示label
label.pack()
win.mainloop()
3.button按钮
import tkinter
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("300x300+300+300")
# def func():
# print("别摸我脚Y子")
def func(name):
print("别摸%s脚Y子"%(name))
# 创建一个button
# command用来指定点击按钮后调用的函数
# btn = tkinter.Button(win,text="登陆",width=7,bg="#ffcccc",command=func)
# func1 = lambda :func("小明")
# 通过lambda传值
# btn = tkinter.Button(win,text="登陆",width=7,bg="#ffcccc",command=lambda :func("小明"))
# 退出
btn = tkinter.Button(win,text="登陆",width=7,bg="#ffcccc",command=win.quit)
# 显示
btn.pack()
win.mainloop()
4.entry输入控件
import tkinter
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("300x300+300+300")
variable = tkinter.Variable()
# 创建一个输入控件
entry = tkinter.Entry(win,textvariable = variable)
# 设置输入框中的字符串
variable.set("余钟炜是个啥")
# 获取输入框中输入的字符串
res = variable.get()
print(res)
# 显示
entry.pack()
win.mainloop()
5.点击后获取输入的值
import tkinter
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("300x300+300+300")
def func():
# 获取输入框中的值
print(variable.get())
variable = tkinter.StringVar()
entry = tkinter.Entry(win,textvariable=variable)
button = tkinter.Button(win,text="获取值",width=7,bg="#ffcccc",command=func)
entry.pack()
button.pack()
win.mainloop()
6.Combobox下拉框控件
import tkinter
from tkinter import ttk
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("300x300+300+300")
# 创建一个下拉框控件
combox = ttk.Combobox(win)
# 给下拉控件设置值(多个)
combox["values"] = ("北京","上海","深圳","广州","杭州")
# 设置默认选中值
# 参数写 索引
combox.current(2)
# 获取当前的值
# res = combox.get()
# print(res)
def func(event):
print(combox.get())
# 绑定事件
combox.bind("<
# 显示
combox.pack()
win.mainloop()
7.radiobutton单选按钮
import tkinter
from tkinter import ttk
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("300x300+300+300")
def func():
# print("1")
# pass
# print(group.get())
value = group.get()
if value == 1:
print("男")
if value == 2:
print("女")
if value == 3:
print("不明")
group = tkinter.IntVar()
# 创建radiobutton
radio1 = ttk.Radiobutton(win,text="男",variable = group,value = 1,command = func)
radio2 = ttk.Radiobutton(win,text="女",variable = group,value = 2,command = func)
radio3 = ttk.Radiobutton(win,text="不明",variable= group,value = 3,command = func)
# 显示
radio1.pack()
radio2.pack()
radio3.pack()
win.mainloop()
8.Checkbutton 多选按钮
import tkinter
from tkinter import ttk
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("300x300+300+300")
def getLikes():
# print("1")
# 获取选中的爱好
likesList = []
if va1.get() == True: #money被选中
likesList.append("money")
if va2.get() == True:
likesList.append("power")
if va3.get() == True:
likesList.append("beautifulgirl")
print(likesList)
va1 = tkinter.BooleanVar()
va2 = tkinter.BooleanVar()
va3 = tkinter.BooleanVar()
# 创建多个Checkbutton
cb1 = ttk.Checkbutton(win,text="money",variable = va1,command=getLikes)
cb2 = ttk.Checkbutton(win,text="power",variable = va2,command=getLikes)
cb3 = ttk.Checkbutton(win,text="beautifulgirl",variable = va3,command=getLikes)
cb1.pack()
cb2.pack()
cb3.pack()
win.mainloop()
9.表格数据
import tkinter
from tkinter import ttk
win = tkinter.Tk()
win.title("button按钮控件")
win.geometry("600x200+300+300")
# 创建表格控件
table = ttk.Treeview(win)
# 设置表头
table["columns"] = ("name","age","address")
# 设置每一列的宽度
table.column("name",width = 100)
table.column("age",width = 100)
table.column("address",width = 100)
# 给表头设置值
table.heading("name",text="姓名/name")
table.heading("age",text="年龄/age")
table.heading("address",text="地址/address")
# 插入数据
table.insert("",1,text="line1",value=("奥巴马",65,"华盛顿"))
table.insert("",2,text="line2",value=("普京",60,"莫斯科"))
table.insert("",2,text="line3",value=("莫迪",68,"新德里"))
table.pack()
win.mainloop()
10.相对布局
#创建并显示窗口
import tkinter
win = tkinter.Tk()
win.title("Label标签")
win.geometry("600x600+300+300")
#
label1 = tkinter.Label(win,text="马克思",width=6,height=2,bg="#800000")
label2 = tkinter.Label(win,text="马巴巴",width=6,height=2,bg="#f0e68c")
label3 = tkinter.Label(win,text="马蓉",width=6,height=2,bg="#00ff00")
label4 = tkinter.Label(win,text="马赛克",width=6,height=2,bg="#5f9ea0")
# 默认是在中间位置
# label1.pack()
# label2.pack(side=tkinter.TOP)11.绝对布局
#创建并显示窗口
import tkinter
win = tkinter.Tk()
win.title("Label标签")
win.geometry("600x600+300+300")
#
label1 = tkinter.Label(win,text="马克思",width=6,height=2,bg="#800000")
label2 = tkinter.Label(win,text="马巴巴",width=6,height=2,bg="#f0e68c")
label3 = tkinter.Label(win,text="马蓉",width=6,height=2,bg="#00ff00")
label4 = tkinter.Label(win,text="马赛克",width=6,height=2,bg="#5f9ea0")
#绝对布局
# x指定 宽度上的偏移量
# y指定 高度上的偏移量
label1.place(x=100,y=0)
label2.place(x=500,y=0)
label3.place(x=0,y=100)
label4.place(x=100,y=100)
win.mainloop()
12.表格布局
#创建并显示窗口
import tkinter
win = tkinter.Tk()
win.title("Label标签")
win.geometry("600x600+300+300")
#
label1 = tkinter.Label(win,text="马克思",width=6,height=2,bg="#800000")
label2 = tkinter.Label(win,text="马巴巴",width=6,height=2,bg="#f0e68c")
label3 = tkinter.Label(win,text="马蓉",width=6,height=2,bg="#00ff00")
label4 = tkinter.Label(win,text="马赛克",width=6,height=2,bg="#5f9ea0")
# 表格布局, column 指定放在哪一列, row表示放在哪一行
label1.grid(column=1,row=1)
label2.grid(column=2,row=1)
label3.grid(row=2,column=2)
win.mainloop()
13.鼠标事件
#创建并显示窗口
import tkinter
win = tkinter.Tk()
win.title("Label标签")
win.geometry("300x300+300+300")
#
#
#
#
#
#
#
#
#
btn = tkinter.Button(win,text="点我Y",width=7,bg="#ffcccc")
btn.pack()
# event 就是鼠标事件, 包含鼠标的操作信息
def func(event):
print("触发了鼠标右键")
print("x:%d,y:%d"%(event.x,event.y))
# 给btn 绑定一个鼠标右键
# btn.bind("
btn.bind("
win.mainloop()