Python --- (two) Tkinter widgets: Button

Previous: Python - (a) Tkinter widgets: Label


                                                The Tkinter Button Widget

## Introduction

Button (Button) components used to implement a variety of buttons. Button assembly may contain text or images, you can be a Python function or method associated therewith, when the button is pressed, the corresponding function or method is performed automatically.

Button component can only display a single font text, but the text can span multiple lines. Additionally, you can add one of the individual characters underscore (for example, used to represent the keyboard shortcut). By default, tab key is used to switch between the button.

When to use ## Button component?

In short, Button component is used to allow the user said: "dry!", Via text or icons on the buttons let users know what to use press down. Button component often used toolbar, the application window, and accept or reject the dialog . Checkbutton Radiobutton assembly and more suitable for the use of data input buttons

## use
common buttons are very easy to use. All you need to do is specify the content of the Button (text, bitmaps or images), and the correlation function or method when the button is pressed should be called:

import tkinter as tk
 
master = tk.Tk()
 
def callback():
    print("我被调用了!")
 
b = tk.Button(master, text="执行", command=callback)
b.pack()
 
master.mainloop()

Here Insert Picture Description
Here Insert Picture Description

If a button is not associated with a function or method, then it is useless. You may use this button to program development process, in this case, a better approach is to disable the buttons, so as to avoid causing your test users puzzled.

b = tk.Button(master, text="不执行", state="disabled")

Here Insert Picture Description
If you do not specify the size of the Label, the Label's size just to accommodate its contents only. You can use padx and pady option to add extra space between the Button's content and borders.

Of course, you can explicitly set the size Button by height and width options: If you display a text, then the text of these two options is defined as a unit cell size of Button; if you display a bitmap or image, then they Button size in pixels is defined as a unit (or other screen unit).

For the text content of a Button component, you can use the pixels of specified size Buttton, but this requires some skill. Here provides a method for your reference (there are other ways to achieve):

def callback():
    print("我被调用了!")
f = tk.Frame(master, height=64, width=64)
f.pack_propagate(0)
f.pack()
 
b = tk.Button(f, text="确定", command=callback)
b.pack(fill="both", expand=1)

Here Insert Picture Description

Button can display multiple lines of text, you can use line breaks or direct use wraplength options to achieve. When the text wraps, you can use the anchor and justify and padx option to make the text as you want them to appear:

def callback():
    print("我被调用了!")
longtext = """
我明明只是一个按钮,
作为按钮并不需要太多
的文字用于告诉用户当
我被按下的时候会发生
什么事情,但我为什么
这么长?
"""
b = tk.Button(master, text=longtext, anchor="w", justify="left", padx=2, command=callback)
b.pack()

Here Insert Picture Description

In order to make a normal button remain "pressed" state, for example, you want to implement a toolbox in some way (like Photoshop on the left side of the toolbar button, when pressed him to keep concave down, that you are using this tool), you can simply defaults relief option "raised" changed to "sunken":

b.config(relief="sunken")

Here Insert Picture Description
You may also want to change the background color. However, a better approach is to use Checkbutton and Radiobutton components and put them in indicatoron option is set to False:

b = tk.Checkbutton(master, image=bold, variable=var, indicatoron=False)

In earlier versions of Tkinter in, image option overrides the text option. This means that if you specify two options, only the image options are displayed. But in the new Tkinter, you can use compound options set the blending mode of the two. Below is provided by, for example, compound = "center" so that the text is located above the image (overlap display):

photo = tk.PhotoImage(file = 'botton.gif')
b = tk.Button(master, text="点我", font = 20, image = photo, compound = "center")
 
b.pack()

Here Insert Picture Description
By "left", "right", "top" and "bottom" may be provided next to the text displayed in the image:

b = tk.Button(master, text="点它 ->", font = 20, image = photo, compound = "right")

Here Insert Picture Description

##参数
Button(master=None, ** options) (class)

master - parent component

** options - component options, the table below details the specific meaning and usage of each option:

Options meaning
activebackground 1. 设置当 Button 处于活动状态(通过 state 选项设置状态)的背景色
2. 默认值由系统指定
activeforeground 1. 设置当 Button 处于活动状态(通过 state 选项设置状态)的前景色
2. 默认值由系统指定
anchor

1. 控制文本(或图像)在 Button 中显示的位置

2. "n", "ne", "e", "se", "s", "sw", "w", "nw", 或者 "center" 来定位(ewsn 代表东西南北,上北下南左西右东)
3. 默认值是 "center"

background 1. 设置背景颜色
2. 默认值由系统指定
bg 跟 background 一样
bitmap 1. 指定显示到 Button 上的位图
2. 如果指定了 image 选项,则该选项被忽略
borderwidth 1. 指定 Button 的边框宽度
2. 默认值由系统指定,通常是 1 或 2 像素
bd 跟 borderwidth 一样
compound

1. 控制 Button 中文本和图像的混合模式
2. 默认情况下,如果有指定位图或图片,则不显示文本

3. 如果该选项设置为 "center",文本显示在图像上(文本重叠图像)
4. 如果该选项设置为 "bottom","left","right" 或 "top",那么图像显示在文本的旁边(如 "bottom",则图像在文本的下方)
5. 默认值是 NONE

cursor 1. 指定当鼠标在 Button 上飘过的时候的鼠标样式
2. 默认值由系统指定
default 1. 如果设置该选项("normal"),该按钮会被绘制成默认按钮
2. Tkinter 会根据平台的具体指标来绘制(通常就是绘制一个额外的边框)
2. 默认值是 "disable"
disabledforeground 1. 指定当 Button 不可用的时候前景色的颜色
2. 默认值由系统指定
font 1. 指定 Button 中文本的字体
2. 一个 Button 只能设置一种字体
3. 默认值由系统指定
foreground 1. 设置 Button 的文本和位图的颜色
2. 默认值由系统指定
fg 跟 foreground 一样
height 1. 设置 Button 的高度
2. 如果 Button 显示的是文本,那么单位是文本单元
3. 如果 Button 显示的是图像,那么单位是像素(或屏幕单元)
4. 如果设置为 0 或者干脆不设置,那么会自动根据 Button 的内容计算出高度
highlightbackground 1. 指定当 Button 没有获得焦点的时候高亮边框的颜色
2. 默认值由系统指定
highlightcolor 1. 指定当 Button 获得焦点的时候高亮边框的颜色
2. 默认值由系统指定
highlightthickness 1. 指定高亮边框的宽度
2. 默认值是 0(不带高亮边框)
image 1. 指定 Button 显示的图片
2. 该值应该是 PhotoImage,BitmapImage,或者能兼容的对象
3. 该选项优先于 text 和 bitmap 选项
justify

1. 定义如何对齐多行文本

2. 使用 "left","right" 或 "center"
3. 注意,文本的位置取决于 anchor 选项
4. 默认值是 "center"

overrelief 1. 定义当鼠标飘过时 Button 的样式
2. 如果不设置,那么总是使用 relief 选项指定的样式
padx 指定 Button 水平方向上的额外间距(内容和边框间)
pady 指定 Button 垂直方向上的额外间距(内容和边框间)
relief 1. 指定边框样式
2. 通常当按钮被按下时是 "sunken",其他时候是 "raised"
3. 另外你还可以设置 "groove"、"ridge" 或 "flat"
4. 默认值是 "raised"
repeatdelay 见下方 repeatinterval 选项的描述
repeatinterval 1. 通常当用户鼠标按下按钮并释放的时候系统认为是一次点击动作。如果你希望当用户持续按下按钮的时候(没有松开),根据一定的间隔多次触发按钮,那么你可以设置这个选项。
2. 当用户持续按下按钮的时候,经过 repeatdelay 时间后,每 repeatinterval 间隔就触发一次按钮事件。
3. 例如设置 repeatdelay=1000,repeatinterval=300,则当用户持续按下按钮,在 1 秒的延迟后开始每 300 毫秒触发一次按钮事件,直到用户释放鼠标。
state 1. 指定 Button 的状态
2. 默认值是 "normal"
3. 另外你还可以设置 "active" 或 "disabled"
takefocus 1. 指定使用 Tab 键可以将焦点移到该 Button 组件上(这样按下空格键也相当于触发按钮事件)
2. 默认是开启的,可以将该选项设置为 False 避免焦点在此 Button 上
text 1. 指定 Button 显示的文本
2. 文本可以包含换行符
3. 如果设置了 bitmap 或 image 选项,该选项则被忽略
textvariable 1. Button 显示 Tkinter 变量(通常是一个 StringVar 变量)的内容
2. 如果变量被修改,Button 的文本会自动更新
underline 1. 跟 text 选项一起使用,用于指定哪一个字符画下划线(例如用于表示键盘快捷键)
2. 默认值是 -1
3. 例如设置为 1,则说明在 Button 的第 2 个字符处画下划线
width 1. 设置 Button 的宽度
2. 如果 Button 显示的是文本,那么单位是文本单元
3. 如果 Button 显示的是图像,那么单位是像素(或屏幕单元)
4. 如果设置为 0 或者干脆不设置,那么会自动根据 Button 的内容计算出宽度
wraplength 1. 决定 Button 的文本应该被分成多少行
2. 该选项指定每行的长度,单位是屏幕单元
3. 默认值是 0

##方法

— 刷新 Button 组件,该方法将重绘 Button 组件若干次(在 “active” 和 “normal” 状态间切换)。
invoke()

— 调用 Button 中 command 选项指定的函数或方法,并返回函数的返回值。
— 如果 Button 的state(状态)是 “disabled”(不可用)或没有指定 command 选项,则该方法无效。


Next: Python - (three) Tkinter widgets: Checkbutton

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104148047