tkinter-button详解

本文介绍tkinter的button控件。通常按钮的作用就是用来点击,然后完成相应的动作。那么怎样表示按钮的作用以及对应的动作呢?本文将详细介绍。

按钮(button)

在tkinter中,button的定义如下:

Button(master=None, cnf={}, **kw)

按钮的属性

text

指定按钮上的文字

from tkinter import Tk,Button

main_win = Tk()
main_win.title('渔道的按钮')
width = 450 
height = 450 
main_win.geometry(f'{width}x{height}')

text = "点我"
Button(main_win, text=text).pack()

main_win.mainloop()

button_text

font

指定按钮上文字的字体

text = "点我"
font=("Courier", 20, "bold")
Button(main_win, text=text, font=font).pack()

button_font

width

指定按钮的宽度。如果按钮上显示的是文字,则以字符个数为宽度单位;如果按钮上显示的是图片,则以像素为宽度单位

height

指定按钮的高度。如果按钮上显示的是文字,则以字符个数为高度单位;如果按钮上显示的是图片,则以像素为高度单位

text = "点我"
Button(main_win, text=text, width=20, height=10).pack()

button_width_height

anchor

锚点,用来指定文本或图像在label显示区的显示位置。默认值是"center",可设置的值为’n’, ‘ne’, ‘e’, ‘se’, ‘s’, ‘sw’, ‘w’, ‘nw’; ‘e’、‘w’、‘s’、'n’分别表示东西南北。

# 这里我们将按钮的宽和高设置为40和10,anchor为'se'
# 最终的显示效果是 '点我'在按钮的东南方,也就是右下角;
Button(main_win, anchor='se', width=40, height=10, bg='white', text='点我').pack()

button_anchor

image

指定在按钮上显示的图片

png = PhotoImage(file='qq.png')
Button(main_win, image=png).pack()

注意,生成的是一个带图片的按钮。

button_image

如果,text和image属性都指定了,将会显示什么呢?按钮将会仅显示图片

png = PhotoImage(file='qq.png')
text = 'hello'
Button(main_win, image=png, text=text, fg='red').pack()

compound

使按钮中 文字和图片都可以显示. text和image的位置是相对于image的.例如,compound为bottom,就是text在上,image在下.

main_win.geometry(f'{800}x{800}')
png = PhotoImage(file='button.png')
text = '渔道的按钮'
fg='blue'
Button(main_win, compound='bottom', image=png, text=text, fg=fg).pack()
Button(main_win, compound='top', image=png, text=text, fg=fg).pack()
Button(main_win, compound='center', image=png, text=text, fg=fg).pack()
Button(main_win, compound='left', image=png, text=text, fg=fg).pack()
Button(main_win, compound='right', image=png, text=text, fg=fg).pack()

button_text_image

bitmap

bitmap属性也是用来显示图片。但是有点儿特殊,它是用来显示位图。它有两种显示来源。一种是tkinter内置的位图;一种是用户指定图片。

# 显示tkinter内置的位图
bitmap_list = ['error', 'hourglass', 'questhead', 'info',
                'question', 'warning', 'gray12', 'gray50', 
                'gray75']
for bitmap in bitmap_list:
    Button(main_win, bitmap=bitmap).pack()

button_bitmap_internel

bitmap属性的值是字符串,字符串值只有两种,一种就是前面使用的内置位图的字符串,另一种是使用 @+xbm后缀的文件名。

重点:仅支持xbm格式且xbm的路径要使用@开头

from PIL import Image
xbm = 'qq.xbm'
# 将qq.png转换位单通道的qq.xbm图片
Image.open('qq.png').convert("1").save(xbm)
Button(main_win, bitmap='@'+xbm).pack()

button_bitmap_externel

backgroud\bg

指定按钮背景颜色

foreground\fg

指定按钮前景颜色,即文字的颜色

text = "点我"
Button(main_win, fg='white', bg='black', text=text).pack()

button_fg_bg

cursor

指定鼠标在按钮上时的显示样式,样式的值是’arrow’, ‘watch’, ‘cross’

Button(main_win, cursor="cross", width=40, height=10, text='点我', anchor='ne').pack()

由于不能将鼠标截图,这里就不展示了,可自行运行程序观察。

borderwidth\bd

指定按钮边框宽度

padx

指定水平方向上按钮内容和边框的间距

pady

指定垂直方向上按钮内容和边框的间距

main_win.geometry(f'{800}x{800}')
fg = 'white'
bg = 'black'
text = '点我'
w = 20
h = 10
Button(main_win, text=text, fg=fg, background=bg).pack()
Button(main_win, text=text, fg=fg, background=bg,
                width=w, height=h).pack()
# borderwidth
Button(main_win, text=text, fg=fg, background=bg,
                width=w, height=h, borderwidth=10).pack()
# padx pady
Button(main_win, text=text, fg=fg, background=bg,
                width=w, height=h, borderwidth=10,
                padx = 10, pady = 10).pack()

main_win.mainloop()

button_padx_pady_bd

上面例子的显示结果可以看出,如果设置bd、padx、pady,按钮会相应变大.

relief

指定边框样式,默认样式为’flat’,样式的种类有:‘flat’, ‘groove’, ‘raised’, ‘ridge’, ‘solid’, ‘sunken’

如果bd的宽度变大,relief显示的边框也会跟着变大。

main_win.geometry(f'{600}x{800}')
relief_list = ['flat', 'groove', 'raised', 'ridge', 'solid', 'sunken']

for relief in relief_list:
    Button(main_win, text="点我", fg='red', background="green",
                width = 10, height=5, borderwidth=10, 
                padx=10, pady=10, relief=relief).pack()

button_relief

overrelief

当鼠标飘过按钮时,按钮的边框的显示样式

text='点我'
Button(main_win, text=text, bg='red', bd=5, width=10, height=10, overrelief='groove').pack()

justify

定义按钮中文字的对齐方式,"left"左对齐,"center"居中,"right"右对齐

poem = '''
            将进酒
             李白
君不见黄河之水天上来,奔流到海不复回。
君不见高堂明镜悲白发,朝如青丝暮成雪。
人生得意须尽欢,莫使金樽空对月。
天生我材必有用,千金散尽还复来。
烹羊宰牛且为乐,会须一饮三百杯。
岑夫子,丹丘生,将进酒,杯莫停。
与君歌一曲,请君为我倾耳听。
钟鼓馔玉不足贵,但愿长醉不愿醒。
古来圣贤皆寂寞,惟有饮者留其名。
陈王昔时宴平乐,斗酒十千恣欢谑。
主人何为言少钱,径须沽取对君酌。
五花马、千金裘,呼儿将出换美酒,与尔同销万古愁。
'''
main_win.geometry(f'{800}x{800}')
Button(main_win, text=poem, fg='white', justify="center", background="blue",
            borderwidth=10, 
            padx=10, pady=10, relief='ridge').pack()

Button(main_win, text=poem, fg='white', justify="left", background="blue",
            borderwidth=10, 
            padx=10, pady=10, relief='ridge').pack()

button_justify

state

指定button的状态,normal(默认)/active/disable

activebackground

指定button为active状态时的背景颜色

activeforeground

指定button为active状态时的前景颜色

Button(main_win, text='点我', bg='white', state='active', 
            activeforeground='red', activebackground='green').pack()

Button(main_win, text='点我', bg='white', state='normal', 
            activeforeground='red', activebackground='green').pack()

button_acfg_acbg

disabledforeground

当button的状态为DISABLE时,文字的显示颜色

Button(main_win, text='点我', bg='white', state='disable', 
            disabledforeground='red', activebackground='green').pack()

Button(main_win, text='点我', bg='white', state='normal', 
            activeforeground='red', activebackground='green').pack()

button_disabledfg

command

指定按钮被点击时调用的函数。按钮非常重要的属性,提升人机交互体验。

def click_cb():
    messagebox.showinfo("title", "我是渔道")
Button(main_win, command=click_cb, text='点我', bg='white').pack()

鼠标点击 "点我"按钮后,会弹出一个对话框,如下图所示:

button_command

textvariable

显示 StringVar变量的内容,作用是可以动态的更新label显示的内容。

def click_cb(text_var):
    text_var.set("好坏哦")

text_var = StringVar() 
text_var.set("点我")

Button(main_win, command=lambda : click_cb(text_var), bg='white', textvariable=text_var).pack()

button_textvariable

当点击"点我"按钮后,按钮上的文字变成了"好坏哦"。可运行程序感受一下简单的动态交互效果.

button_textvariable2

repeatdelay

重复延迟

表示点击按钮后,延迟repeatdelay(ms),做出相应动作.

def click_cb():
    print("xxx")

text_var = StringVar() 
text_var.set("点我")

# 点击按钮,即刻执行click_cb()
# Button(main_win, command=click_cb, bg='white', textvariable=text_var).pack()
Button(main_win, repeatdelay=1000, command=click_cb, bg='white', textvariable=text_var).pack()
# 点击按钮,延迟1秒 执行click_cb()

repeatinterval

重复间隔

repeatinterval需要和repeatdelay一起使用才能凑效.作用就是长按按钮时,间隔repeatinterval(ms),执行相应动作.

def click_cb():
    print("xxx")

text_var = StringVar() 
text_var.set("点我")

# 长按按钮,间隔300ms,执行一次click_cb()
Button(main_win, repeatdelay=1000, repeatinterval=300, command=click_cb, bg='white', textvariable=text_var).pack()

button的属性就先介绍到这里,如果以后有新的发现,继续追加!

  • 15
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PythonTkinter是一个GUI编程库,它提供了一组工具和组件,可以用于创建图形用户界面。TkinterPython标准库的一部分,因此它可以在大多数Python安装中使用。 Tkinter提供了许多组件,例如按钮、标签、文本框、滚动条等等,这些组件可以用于创建各种类型的GUI应用程序。Tkinter还提供了布局管理器,可以帮助您轻松地组织和排列组件。 使用Tkinter创建GUI应用程序的基本步骤如下: 1. 导入Tkinter模块 2. 创建主窗口 3. 添加组件 4. 设置组件属性 5. 组织和排列组件 6. 启动主循环 在Tkinter中,每个组件都有一个唯一的标识符,称为“名称”。您可以使用名称来引用组件并设置其属性。例如,您可以使用以下代码创建一个标签: label = tkinter.Label(root, text="Hello, World!") 在这里,“root”是主窗口的名称,“text”是标签的属性之一。您可以使用以下代码设置标签的文本颜色: label.config(foreground="red") Tkinter还提供了许多事件,例如单击按钮、按下键盘等等。您可以使用这些事件来响应用户的操作。例如,您可以使用以下代码创建一个按钮,并在单击时显示一条消息框: button = tkinter.Button(root, text="Click me!") button.bind("<Button-1>", lambda event: tkinter.messagebox.showinfo("Message", "Hello, World!")) 在这里,“bind”方法将按钮与事件绑定在一起,“<Button-1>”表示单击左键,lambda函数用于创建一个匿名函数,该函数在单击按钮时显示消息框。 总之,Tkinter是一个功能强大的GUI编程库,可以帮助您轻松地创建各种类型的GUI应用程序。如果您想深入了解Tkinter的使用,请查看官方文档或参考其他资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sif_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值