# 测试Tkinter包是否好用
import tkinter
tkinter._test()
# hello world
import tkinter
base = tkinter.Tk()
# 消息循环
base.mainloop()
# Label的例子
import tkinter
base = tkinter.Tk()
# 负责标题
base.wm_title("Label Test")
lb = tkinter.Label(base, text = "Python Label")
# 给相应组件指定布局
lb.pack()
base.mainloop()
# 设置Label的例子
import tkinter
base = tkinter.Tk()
base.wm_title("Label Test")
# 支持属性很多background,font,underline等
# 第一个参数,指定属性
lb1 = tkinter.Label(base, text = "Python AI")
lb1.pack()
lb2 = tkinter.Label(base, text = "绿色背景", background = "green")
lb2.pack()
lb3 = tkinter.Label(base, text = "蓝色背景", background = "blue")
lb3.pack()
base.mainloop()
# Button的案例
import tkinter
def showLabel():
global baseFrame
# 在函数中定义了一个label
# label的父组件是baseFrame
lb = tkinter.Label(baseFrame, text = "显示Label")
lb.pack()
baseFrame = tkinter.Tk()
# 生成一个按钮
# command参数指示,当按钮按下的时候,执行哪个函数
btn = tkinter.Button(baseFrame, text = "Show Label", command = showLabel)
# btn = tkinter.Button(baseFrame, text = "Show Label")
btn.pack()
baseFrame.mainloop()
'''
Button的属性:
anchor 设置按钮中文字的对其方式,相对于按钮的中心位置
background(bg) 设置按钮的背景颜色
foreground(fg) 设置按钮的前景色(文字的颜色)
borderwidth(bd) 设置按钮边框宽度
cursor 设置鼠标在按钮上的样式
command 设定按钮点击时触发的函数
bitmap 设置按钮上显示的位图
font 设置按钮上文本的字体
width 设置按钮的宽度 (字符个数)
height 设置按钮的高度 (字符个数)
state 设置按钮的状态
text 设置按钮上的文字
image 设置按钮上的图片
'''
控制组件的摆放方式
三种布局
pack布局
# pack布局案例
import tkinter
baseFrame = tkinter.Tk()
# 以下所有代码都是创建一个组件,然后看布局
btn1 = tkinter.Button(baseFrame, text = "A")
btn1.pack(side=tkinter.LEFT, expand=tkinter.YES, fill = tkinter.Y)
btn2 = tkinter.Button(baseFrame, text = "B")
btn2.pack(side=tkinter.TOP, expand=tkinter.YES, fill = tkinter.BOTH)
btn3 = tkinter.Button(baseFrame, text = "C")
btn3.pack(side=tkinter.RIGHT, expand=tkinter.YES, fill = tkinter.NONE, anchor=tkinter.NE)
btn4 = tkinter.Button(baseFrame, text = "D")
btn4.pack(side=tkinter.LEFT, expand=tkinter.NO, fill = tkinter.Y)
btn5 = tkinter.Button(baseFrame, text = "E")
btn5.pack(side=tkinter.TOP, expand=tkinter.NO, fill = tkinter.BOTH)
btn6 = tkinter.Button(baseFrame, text = "F")
btn6.pack(side=tkinter.BOTTOM, expand=tkinter.YES)
btn7 = tkinter.Button(baseFrame, text = "G")
btn7.pack(anchor=tkinter.SE)
baseFrame.mainloop()
grid布局
# grid布局案例
import tkinter
baseFrame = tkinter.Tk()
# 下面被注释掉的一行代码跟下面两行代码等效
#lb1 = tkinkter.Label(baseFrame, text="账号:").grid(row=0, sticky=tkinter.W)
lb1 = tkinter.Label(baseFrame, text = "账号:")
lb1.grid(row=0, sticky=tkinter.W)
en1 = tkinter.Entry(baseFrame)
en1.grid(row=0, column=1, sticky=tkinter.E)
lb2 = tkinter.Label(baseFrame, text = "密码:").grid(row=1, sticky=tkinter.W)
en2 = tkinter.Entry(baseFrame).grid(row=1, column=1, sticky=tkinter.E)
btn = tkinter.Button(baseFrame, text = "登录").grid(row=2, column=1, sticky=tkinter.W)
baseFrame.mainloop()
place布局
# 事件简单的例子
import tkinter
def baseLabel(event):
global baseFrame
print("哈哈,我被点击了")
lb = tkinter.Label(baseFrame, text = "谢谢点击")
lb.pack()
# 画出程序的总框架
baseFrame = tkinter.Tk()
lb = tkinter.Label(baseFrame, text = "模拟按钮")
# label绑定相应的消息和处理函数
# 自动获取左键点击,并启动相应的处理函数baseLabel
lb.bind("", baseLabel)
lb.pack()
# 启动消息循环
# 到此,表示程序开始运行
baseFrame.mainloop()
Entry
# 输入框案例
import tkinter
# 模拟的是登录函数
def reg():
# 从相应的输入框中,得到用户的输入
name = e1.get()
pwd = e2.get()
t1 = len(name)
t2 = len(pwd)
if name == "111" and pwd == "222":
# 需要理解下面代码的含义
lb3["text"] = "登录成功"
else:
lb3["text"] = "用户名或密码错误"
# 输入框删除掉用户输入的内容
# 注意delete的两个参数,表示从第几个删除到第几个
e1.delete(0, t1)
e2.delete(0, t2)
# 启动舞台
baseFrame = tkinter.Tk()
lb1 = tkinter.Label(baseFrame, text = "用户名:")
lb1.grid(row = 0, column = 0, stick = tkinter.W)
e1 = tkinter.Entry(baseFrame)
e1.grid(row = 0, column = 1, stick = tkinter.E)
lb2 = tkinter.Label(baseFrame, text = "密码:")
lb2.grid(row = 1, column = 0, stick = tkinter.W)
e2 = tkinter.Entry(baseFrame)
e2.grid(row = 1, column = 1, stick = tkinter.E)
#e2['show'] = '*'
# Button参数command的意思是,当按钮被点击后启动相应的处理函数
btn = tkinter.Button(baseFrame, text = "登录", command = reg)
btn.grid(row = 2, column = 1, stick = tkinter.E)
lb3 = tkinter.Label(baseFrame, text = "")
lb3.grid(row = 3)
# 启动主Frame
baseFrame.mainloop()
普通菜单
# 普通菜单代码
import tkinter
baseFrame = tkinter.Tk()
menubar = tkinter.Menu(baseFrame)
for item in ['File', 'Edit', 'View', 'About']:
menubar.add_command(label=item)
baseFrame['menu'] = menubar
baseFrame.mainloop()
级联菜单
import tkinter
baseFrame = tkinter.Tk()
menubar = tkinter.Menu(baseFrame)
emenu = tkinter.Menu(menubar)
for item in ['Copy', 'Past', 'Cut']:
emenu.add_command(label = item)
menubar.add_cascade(label='File')
menubar.add_cascade(label='Edit', menu = emenu)
menubar.add_cascade(label='About')
baseFrame['menu'] = menubar
baseFrame.mainloop()
弹出式菜单
import tkinter
def makeLabel():
global baseFrame
tkinter.Label(baseFrame, text = "PHP是最好的编程语言,我用python").pack()
baseFrame = tkinter.Tk()
menubar = tkinter.Menu(baseFrame)
for x in ['麻辣香菇', '汽锅鸡', '东坡肘子']:
menubar.add_separator()
menubar.add_command(label=x)
menubar.add_command(label='重庆火锅', command = makeLabel)
# 事件处理函数一定要至少有一个参数,且第一个参数表示的是系统事件
def pop(event):
# 注意使用event.x和event.x_root的区别
# menubar.post(event.x_root, event.y_root)
menubar.post(event.x_root, event.y_root)
baseFrame.bind("", pop)
baseFrame.mainloop()
画布
# 简单的画布
import tkinter
baseFrame = tkinter.Tk()
cvs = tkinter.Canvas(baseFrame, width = 300, height = 200)
cvs.pack()
# 一条线需要两个点指明起始
# 参数数字的单位是px
cvs.create_line(23, 23, 190, 234)
cvs.create_text(56, 67, text = "I Love LiuYiFei")
baseFrame.mainloop()
# 画一个五角星
import tkinter
import math as m
baseFrame = tkinter.Tk()
w = tkinter.Canvas(baseFrame, width = 300, height = 300, background = 'red')
w.pack()
center_x = 150
center_y = 150
r = 150
# 依次存放五个点的位置
points = [
# 做上点
# pi是一个常量数字,3.1415926
center_x - int(r * m.sin(2 * m.pi / 5)),
center_y - int(r * m.cos(2 * m.pi / 5)),
# 右上点
center_x + int(r * m.sin(2 * m.pi / 5)),
center_y - int(r * m.cos(2 * m.pi / 5)),
# 左下点
center_x - int(r * m.sin(m.pi / 5)),
center_y + int(r * m.cos(m.pi / 5)),
# 顶点
center_x,
center_y - r,
# 右下点
center_x + int(r * m.sin(m.pi / 5)),
center_y + int(r * m.cos(m.pi /5)),
]
# 创建一个多边形
w.create_polygon(points, outline="green", fill = "yellow")
w.create_text(150, 150, text = "五角星")
baseFrame.mainloop()
import tkinter
baseFrame = tkinter.Tk()
def btnClick(event):
global w
w.move(id_ball, 12, 5)
w.move("fall", 0, 5)
w = tkinter.Canvas(baseFrame, width = 500, height = 400)
w.pack()
w.bind("", btnClick)
# 创建组件后返回id
id_ball = w.create_oval(20, 20, 50, 50, fill = "green")
# 创建组件使用tag属性
w.create_text(123, 56, fill = "red", text = "I Love LiuYiFei", tag = "fall")
# 创建的时候如果没有指定tag可以利用addtag_withtag添加
# 同类函数还有addtag_all, addtag_above, addtag_xxx等等
id_rectangle = w.create_rectangle(56, 78, 173, 110, fill = "gray")
w.addtag_withtag("fall", id_rectangle)
baseFrame.mainloop()