Python之tkinter 功能按钮Button的基本应用

Button简介

功能按钮也可称作按钮,在窗口组件中可以设计,在单击功能按钮时执行某一个特定的动作,这个动作也称为callback方法,也就是说我们可以将功能按钮当做用户与程序间沟通的桥梁。

按钮上面也可以有文字,或是和标签一样可以有图像,如果是文字样式的功能按钮,可以设定此文字的字形

它的语法格式:

Botton(父对象, options,...)

参数:

  • 第一个参数:父对象,表示这个功能按钮将建立在哪一个窗口内
  • 第二个参数:options,参数如下
参数含义
borderwidth边界宽度
默认是两个像素
bd边界宽度
默认是两个像素
background背景色彩
bg背景色彩
command单机功能按钮时,执行此方法
cursor当鼠标光标移至按钮上时的形状
foreground前景色彩
fg前景色彩
font字形
height高,单位是字符高
highlightbackground当功能按钮获取焦点时的背景颜色
highlightcolor当功能按钮取得焦点时的颜色
image功能按钮上的图像
justify当有多行文字时,最后一行文字的对齐方式
padx可设置功能按钮与文字的间隔
默认是1
pady可设置功能按钮的上下间距
默认是1
relief可由此控制文字外框
默认是relief=FLAT
state若设置为DISABLED,则以灰阶显示功能按钮,表示暂时无法使用
默认是state=NORMAL
text功能按钮名称
underline可以设置第几个文字有下划线
从0开始计算,默认是-1,表示无下划线
width宽,单位是字符宽
wraplength限制每行的文字数
默认是0,表示只有"\n"才会换行
Button的基本应用

例子:单击按钮显示字符串

import tkinter

"""这里我们相当于后续添加了label的属性"""
def show():
    label["text"] = "students"
    label["bg"] = "lightblue"
    label["fg"] = "red"


root = tkinter.Tk()
"""这里我们先创建一个空的laber标签"""
label = tkinter.Label(root)
"""command调用函数时只需要写函数的名字,不需要加括号"""
button = tkinter.Button(root, text="打印", command=show)
label.pack()
button.pack()
root.mainloop()

运行结果:
在这里插入图片描述

当然,我们也可以使用config的方式来后续添加属性
例子:改用config的方式来添加

import tkinter


def show():
    label.config(text="python!!!", bg="lightblue", fg="red")


root = tkinter.Tk()
label = tkinter.Label(root)
button = tkinter.Button(root, text="打印", command=show)
label.pack()
button.pack()
root.mainloop()

运行结果:
在这里插入图片描述

这里我们可以进一步优化,我们可以使用root.destroy来退出程序
例子

import tkinter


def show():
    label.config(text="python!!!", bg="lightblue", fg="red")


root = tkinter.Tk()
label = tkinter.Label(root)
button = tkinter.Button(root, text="打印", width=15, command=show)
"""同理,这里的destroy也不需要加括号"""
buttonBye = tkinter.Button(root, text="退出", width=15, command=root.destroy)
label.pack()
"""这里的side=tkinter.LEFT相当于css里面的浮动,意思就是从左边开始排序"""
button.pack(side=tkinter.LEFT)
buttonBye.pack(side=tkinter.LEFT)
root.mainloop()

运行结果:
在这里插入图片描述

这里解释一下,root.destroy可以关闭root窗口对象,同时程序结束
另一个常用的方法是quit,可以让Python Shell执行的程序结束,但是root窗口则继续执行

例子:使用定时器功能,添加结束按钮

import tkinter

count = 1
def show(aa):
    def counting():
        global count
        count += 1
        aa.config(text=str(count))
        aa.after(1000, counting)
    counting()


root = tkinter.Tk()
label = tkinter.Label(root, bg="yellow", fg="red", height=3, width=15)
buttonBye = tkinter.Button(root, text="退出", width=15, command=root.destroy)
label.pack()
show(label)
buttonBye.pack(side=tkinter.LEFT)
root.mainloop()

运行结果:
在这里插入图片描述

例子:按按钮变颜色

import tkinter

def yellow():
    root.config(bg="lightyellow")

def blue():
    root.config(bg="lightblue")

root = tkinter.Tk()
root.geometry("300x200")
exitbutton = tkinter.Button(root, text="Exit", command=root.destroy)
bluebutton = tkinter.Button(root, text="Blue", command=blue)
yellowbutton = tkinter.Button(root, text="Yellow", command=yellow)
exitbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
bluebutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
yellowbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
root.mainloop()

运行结果:
在这里插入图片描述

使用Lambda 表达式

我们可以用Lambda表达式来添加参数

例子:改良计时器,触发开启计时功能

import tkinter

count = 1
def show(aa):
    def counting():
        global count
        count += 1
        aa.config(text=str(count))
        aa.after(1000, counting)
    counting()


root = tkinter.Tk()
label = tkinter.Label(root, bg="yellow", fg="red", height=3, width=15)
button = tkinter.Button(root, text="开始", width=15, command=lambda: show(label))
buttonBye = tkinter.Button(root, text="退出", width=15, command=root.destroy)
label.pack()
button.pack(side=tkinter.LEFT)
buttonBye.pack(side=tkinter.LEFT)
root.mainloop()

运行结果:
在这里插入图片描述

例子:改良变颜色按钮,简化代码

import tkinter


def color(bgcolor):
    root.config(bg=bgcolor)


root = tkinter.Tk()
root.geometry("300x200")
exitbutton = tkinter.Button(root, text="Exit", command=root.destroy)
bluebutton = tkinter.Button(root, text="Blue", command=lambda: color("lightblue"))
yellowbutton = tkinter.Button(root, text="Yellow", command=lambda: color("lightyellow"))
exitbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
bluebutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
yellowbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
root.mainloop()

运行结果:
在这里插入图片描述

建立含图像的功能按钮

一般功能按钮是用文字当按钮名称,其实也可以用图像当做按钮名称,若是使用图像当按钮名称,在Button()内可以省略text参数设置按钮名称,但是要在Button()内要增加image参数设置图像对象

例子:使用图像按钮

import tkinter


def show():
    label.config(text="Python!!!", bg="lightyellow", fg="red")


root = tkinter.Tk()
img = tkinter.PhotoImage(file="1.gif")
label = tkinter.Label(root)
button = tkinter.Button(root, image=img, command=show)
label.pack()
button.pack()
root.mainloop()

运行结果:
在这里插入图片描述

图像与文字并存的功能按钮

在设计功能按钮时,若是想要让图像和文字并存于功能按钮内,需要在Button()内增加参数"compound=参数",参数如下

参数含义
LEFT
TOP
RIGHT
BOTTOM
CENTER中央

例子:将图像放在文字上方

import tkinter


def show():
    label.config(text="Python!!!", bg="lightyellow", fg="red")


root = tkinter.Tk()
img = tkinter.PhotoImage(file="1.gif")
label = tkinter.Label(root)
button = tkinter.Button(root, image=img, text="点我", compound=tkinter.TOP, command=show)
label.pack()
button.pack()
root.mainloop()

运行结果:
在这里插入图片描述
例子:将图像与文字重叠

button = tkinter.Button(root, image=img, text="点我", fg="red", compound=tkinter.CENTER, command=show)

运行结果:
在这里插入图片描述

简易计算器按钮布局的应用
import tkinter

root = tkinter.Tk()
label = tkinter.Label(root, text="", bg="lightyellow", width=28)
btn7 = tkinter.Button(root, text="7", width=3)
btn8 = tkinter.Button(root, text="8", width=3)
btn9 = tkinter.Button(root, text="9", width=3)
btnRemaining = tkinter.Button(root, text="%", width=3)
btnSquare = tkinter.Button(root, text="√", width=3)
btn4 = tkinter.Button(root, text="4", width=3)
btn5 = tkinter.Button(root, text="5", width=3)
btn6 = tkinter.Button(root, text="6", width=3)
btnx = tkinter.Button(root, text="*", width=3)
btnDivision = tkinter.Button(root, text="÷", width=3)
btn1 = tkinter.Button(root, text="1", width=3)
btn2 = tkinter.Button(root, text="2", width=3)
btn3 = tkinter.Button(root, text="3", width=3)
btnAdd = tkinter.Button(root, text="+", width=3)
btnReduction = tkinter.Button(root, text="-", width=3)
btnZero = tkinter.Button(root, text="0", width=3)
btnDoubleZero = tkinter.Button(root, text="-", width=3)
btnD = tkinter.Button(root, text=".", width=3)
btnEqual = tkinter.Button(root, text="=", width=9)

label.grid(row=0, column=0, columnspan=5)
btn7.grid(row=1, column=0, padx=5)
btn8.grid(row=1, column=1, padx=5)
btn9.grid(row=1, column=2, padx=5)
btnRemaining.grid(row=1, column=3, padx=5)
btnSquare.grid(row=1, column=4, padx=5)
btn4.grid(row=2, column=0, padx=5)
btn5.grid(row=2, column=1, padx=5)
btn6.grid(row=2, column=2, padx=5)
btnx.grid(row=2, column=3, padx=5)
btnDivision.grid(row=2, column=4, padx=5)
btn1.grid(row=3, column=0, padx=5)
btn2.grid(row=3, column=1, padx=5)
btn3.grid(row=3, column=2, padx=5)
btnAdd.grid(row=3, column=3, padx=5)
btnReduction.grid(row=3, column=4, padx=5)
btnZero.grid(row=4, column=0, padx=5)
btnDoubleZero.grid(row=4, column=1, padx=5)
btnD.grid(row=4, column=2, padx=5)
btnEqual.grid(row=4, column=3, padx=5, columnspan=2)

root.mainloop()

运行结果:

在这里插入图片描述

设计鼠标光标在功能按钮上的形状
import tkinter

root = tkinter.Tk()
label = tkinter.Label(root, text="Python!!!")
button = tkinter.Button(root, text="Button", cursor="star")
label.pack()
button.pack()
root.mainloop()

运行结果:
在这里插入图片描述

谢谢观看,笔者会持续更新,如有错误或者建议,请私信我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值