tkinter学习系列(四)之Button 控件

目录

前言

Button小部件是一个标准的Tkinter的控件,用于实现各种按钮。按钮可以包含文本或图像,您可以调用Python函数或方法用于每个按钮。Tkinter的按钮被按下时,会自动调用该函数或方法

(一)基本用法和可选属性

==1.基本用法==

基本用法:Button(根对象, [属性列表])

根对象:在那个窗体显示,例如主窗体。

属性列表:是可选的属性=属性值组成。

==2.可选属性==

属性 说明
text 标签显示的文本
font 设置文本的字体和大小
fg(foreground) 字体的颜色,
bg (background) 标签的背景色
width 标签的宽度(一个中文的字体宽为单位)
height 标签的高度(一个中文的字体高为单位)
cursor 鼠标的样式
command 绑定事件
padx 文字到边框的距离,水平方向
pady 文字到边框的距离,垂直方向
bd(borderwidth) 边框的宽度
relief 边框的样式
justify 文本对齐方式
image 图片
compound 图片与文字的混搭
anchor 方位

(二)属性的具体实现和案例

==1.常用属性==

(1)font

font:设置字体与字体的大小

用法:font=("字体名",大小) 例如:font=(“黑体”, 20)

(2)fg 与 bg

fg 前景色,也就是字体的颜色,bg 背景颜色

用法:fg="red", fg="#121234"

(3)width 与 height

width height 标签的宽度与高度,都是以系统默认的中文的一个字体宽高为单位

用法:width = 5, height=2

==案例一==

(1)源代码

import tkinter as tk

win = tk.Tk()

# 普通的按钮
button1 = tk.Button(win, text="Button1")
button1.pack()

# 背景色与前景色
button2 = tk.Button(win, text="Button2", bg="green", fg="blue")
button2.pack()

# 宽度与高度
button3 = tk.Button(win, text="Button3", width=10, height=2)
button3.pack()

# 边距
button4 = tk.Button(win, text="Button4", padx=10, pady=10)
button4.pack()
win.mainloop()

(2)输出效果

01.png

==2.按钮里的图片==

(1)只放图片,没有文字

需要先导入图片的路径:img1 = tk.PhotoImage(file="image/01.png")

再使用:image=img1

注:目前支持 .png 与 .gif 格式, 还不支持 .jpg格式,==Button的大小是根据图片的大小来确定的。==

==案例二==

(1)源代码:

import tkinter as tk

win = tk.Tk()

img1 = tk.PhotoImage(file="image/01.png")
img2 = tk.PhotoImage(file="image/03.png")
img3 = tk.PhotoImage(file="image/04.png")

# 300像素大小的图片
button1 = tk.Button(win, text="Button1", image=img1)
button1.pack()

# 150像素大小的图片
button2 = tk.Button(win, image=img2)
button2.pack()

# 50像素大小的图片
button3 = tk.Button(win, image=img3)
button3.pack()

win.mainloop()

(2)输出效果:

02.png

(3)图片与文字混搭

需要使用:compound="对齐方式",

对齐方式有:'left', "right", "center"

==案例三==

(1)源代码

import tkinter as tk

win = tk.Tk()

img1 = tk.PhotoImage(file="image/01.png")
img2 = tk.PhotoImage(file="image/03.png")
img3 = tk.PhotoImage(file="image/04.png")


button1 = tk.Button(win, text="Button1", image=img1, compound="left")
button1.pack()

button2 = tk.Button(win, text="Button2", image=img2, compound="center")
button2.pack()

button3 = tk.Button(win, text="Button3", image=img3, compound="right")
button3.pack()

win.mainloop()

(2)输出效果

03.png

==3.鼠标的样式==

cursor="鼠标的属性值"

pencil:笔型

circle:圆形

hand1:手型1

hand2:手型2

==案例四==

(1)源代码

import tkinter as tk

win = tk.Tk()

# 笔型
button1 = tk.Button(win, text="Button1", cursor="pencil")
button1.pack()

# 圆形
button2 = tk.Button(win, text="Button2", cursor="circle")
button2.pack()

# 手型1
button3 = tk.Button(win, text="Button3", cursor="hand1")
button3.pack()

# 手型2
button4 = tk.Button(win, text="Button4", cursor="hand2")
button4.pack()

win.mainloop()

(2)输出效果

当我们把鼠标放在按钮上时,鼠标的形状会显示不同的样式。

04.png

==4.边框样式==

relief= "边框样式值"

flat 无边框

groove 中间凹

ridge 中间凸

raised 往中间凸

solid 往中间凹

sunken 不可以

==案例五==

(1)源代码

import tkinter as tk

win = tk.Tk()

# flat 无边框
button1 = tk.Button(win, text="flat", relief="flat", bd=10)
button1.pack()

# groove 中间凹
button2 = tk.Button(win, text="groove", relief="groove", bd=10)
button2.pack()

# ridge 中间凸
button3 = tk.Button(win, text="raised", relief="ridge", bd=10)
button3.pack()

# raised 往中间凸
button4 = tk.Button(win, text="ridge", relief="raised", bd=10)
button4.pack()

# solid 往中间凹
button5 = tk.Button(win, text="solid", relief="solid", bd=10)
button5.pack()

# sunken 不可以
button6 = tk.Button(win, text="sunken", relief="sunken", bd=10)
button6.pack()

win.mainloop()

(2)输出效果

05.png

(三)按钮的事件绑定

==1.普通的Button绑定事件==

(1)说明:

Button 使用 command=功能函数 来绑定

Button(win, text="确定", command=功能函数)

==案例六==

(1)源代码:

我们创建一个简单的窗体,只有一个按钮控件,

我们绑定的事件是,当我们点击"确定"按钮时,会输出“你点击了按钮”

import tkinter as tk
win = tk.Tk()

# 定义功能函数, event是必须添加的参数,不知道来自哪里
def button_command():
    print("你点击了按钮")
# 绑定事件
btn = tk.Button(win, text="确定", command=button_command)

btn.place(relx=0.2, rely=0.2, relwidth=0.5, relheight=0.1)
win.geometry("300x300+200+200")
win.mainloop()

(2)输出效果:

06.png

==2.传参数Button绑定事件==

(1)说明:

我们使用Button传递数值时,需要用:

lambda: 功能函数(var1, var2, ……)

==案例七==

(1)源代码:

我们同样创建一个简单的窗体,只有一个控件按钮

我们绑定的事件是,当我们点击按钮时,会传入两个参数,并在功能函数进行计算。

import tkinter as tk
"""
    Button command 传值事件
    command= lambda: function(var1, var2, ...)
"""


def sum_fun(a, b):
    result = a + b
    print("%d + %d = %d" % (a, b, result))


win = tk.Tk()
button = tk.Button(win, text="传值事件", command=lambda: sum_fun(12, 13))
button.pack()
win.geometry("300x300+200+200")
win.mainloop()

(2)输出效果:

07.png

作者:Mark

日期:2019/02/01 周五

猜你喜欢

转载自www.cnblogs.com/zyg123/p/10351073.html