Tkinter (02) 按钮部件 Button

按钮部件的创建及其选项

import tkinter as tk
w = tk.Button(parent, option=value, ...)
activebackground 按钮被按下时, 显示的背景颜色
activeforeground 按钮被按下时, 显示的前景颜色
anchor 文字定位
bd or borderwidth 边框宽度
bg or background 正常背景色
bitmap 用来替代文本的标准点阵图名
command 点击时调用的函数
compound 图与文本共用时,图相对于文本的位置. LEFT/RIGHT/TOP/BOTTOM/CENTER
cursor 鼠标悬停在按钮上时显示的光标
default tk.ACTIVE 或 tk.DISABLED, 按钮外框是否显示 sunken
disabledforeground 禁用时的前景颜色
fg or foreground 正常前景色
font 文字字体
height 按钮的高度, 文本行数或图像像素数
higtlightbackground 非焦点时高亮颜色, Win10 下可能有问题, 需要另外处理
highlightcolor 焦点时高亮颜色, Win10 下可能有问题, 需要另外处理
highlightthickness 聚焦高亮厚度
image 按钮上的图像
justify 多行对齐, tk.LEFT, tk.RIGHT 及 tk.CENTER
overrelief 鼠标悬停按钮时的浮雕样式, 默认为 tk.RAISED
padx 在文本左侧和右侧填充间隔
pady 在文本上方和下方填充间隔
relief 浮雕样式, 默认为 tk.RAISED
repeatdelay 直到经过指定时间(毫秒), 开始重复按下按钮
repeatinterval 重复间隔, 以毫秒为单位重复按下按钮
state tk.NORMAL, tk.ACTIVE (鼠标悬停在其上) 以及 tk.DISABLED
takefocus 键盘聚焦, 0 或 1
text 按钮文字
textvariable StringVar(), 更改变量将更新按钮上的文本
underline 下划线的索引处,无则为 -1
width 按钮宽度, 文本字数或图像像素数
wraplength 将文本以其适合的长度换行

按钮部件的方法

flash() 按钮在鼠标悬停时的颜色和常规颜色之间闪烁切换
invoke() 单击按钮,返回回调的返回值

一般的使用方式

  1. 导入 tkinter
  2. 定义 Button 的回调函数
  3. 建立主要视窗, 以作为其他部件的容器
  4. 建立部件, 如 Button
  5. 加入部件到布局中
  6. 开始主要视窗的 mainloop()

范例视窗及代码

在这里你可以看到所有选项的效果

Tkinter (02) 按钮部件 Button

注: image 因为另外需要图片文件, 其选项类似bitmap, 在此略过; 另外highlightbackground 选项因为在WIN10 下有点问题, 所以看不出其效果; 还有一些与鼠标动作有关, 所以图片上看不出, 要执行代码, 使用鼠标才能看到结果.

import tkinter as tk

def say_hello():
    global count
    print(f'#{count} Hello !')
    count += 1

def string(option):

    return f'{list(option.keys())[0]}\n= {list(option.values())[0]}'

def cell(option):

    global row, col

    frame   = tk.Frame(root, bd=5, relief='ridge', padx=5, pady=5, width=175, height=100)
    frame.grid_propagate(0)

    button  = tk.Button(frame, **default)
    button.configure(**option)
    button.grid(row=0, sticky='w')

    label   = tk.Label(frame, text=string(option), font=font1, justify='left')
    label.grid(row=1, sticky='w')

    frame.grid(row=row, column=col)

    col = (col+1) % 8
    if col == 0:
        row += 1

def layout(option_1, option_2):

    cell(option_1)
    cell(option_2)

row = 0
col = 0
count = 0

font1   = ('Courier', 8)
font2   = ('Courier', 6)
default = {'text':'Button', 'font':font1, 'width':10, 'height':2}

root = tk.Tk()

text_variable = tk.StringVar()
text_variable.set("nottuB")

settings = [
    [{'activebackground':'blue'},       {'activebackground':'green'}],
    [{'activeforeground':'blue'},       {'activeforeground':'green'}],
    [{'anchor':'center'},               {'anchor':'se'}],
    [{'background':'green'},            {'background':'gray'}],
    [{'bitmap':''},                     {'bitmap':'hourglass', 'compound':'top',
                                         'width':100, 'height':40}],
    [{'borderwidth':'5', 'height':'1'}, {'borderwidth':'8', 'height':1}],
    [{'command':''},                    {'command':say_hello}],
    [{'compound':'top',    'bitmap':'hourglass', 'width':100, 'height':40},
     {'compound':'bottom', 'bitmap':'hourglass', 'width':100, 'height':40}],
    [{'cursor':''},                     {'cursor':'pirate'}],
    [{'default':'disabled'},            {'default':'active'}],
    [{'disabledforeground':'gray',   'state':'disabled'},
     {'disabledforeground':'yellow', 'state':'disabled'}],
    [{'font':font2},                    {'font':font1}],
    [{'foreground':'black'},            {'foreground':'blue'}],
    [{'height':1},                      {'height':2}],
    [{'highlightbackground':'green', 'highlightthickness':'5', 'default':'disabled', 'state':'normal'},
     {'highlightbackground':'blue',  'highlightthickness':'5', 'default':'disabled', 'state':'normal'}],
    [{'highlightcolor':'green', 'highlightthickness':'5', 'default':'active'},
     {'highlightcolor':'blue', 'highlightthickness':'5', 'default':'active'}],
    [{'highlightthickness':'1'},        {'highlightthickness':'5'}],
    [{'image':''},                      {'image':''}],
    [{'justify':'center', 'text':'Long line\nline'},
     {'justify':'right', 'text':'Long line\nline'}],
    [{'overrelief':'groove', 'bd':5},   {'overrelief':'ridge', 'bd':5}],
    [{'padx':'0'},                      {'padx':'10'}],
    [{'pady':'0'},                      {'pady':'4'}],
    [{'relief':'raised'},               {'relief':'ridge'}],
    [{'repeatdelay':200,    'repeatinterval':200, 'command':say_hello},
     {'repeatdelay':1000,   'repeatinterval':200, 'command':say_hello}],
    [{'repeatinterval':200, 'repeatdelay':200,    'command':say_hello},
     {'repeatinterval':1000,'repeatdelay':200,     'command':say_hello}],
    [{'state':'normal'},                {'state':'disabled'}],
    [{'takefocus':False},               {'takefocus':True}],
    [{'text':'Button 1'},               {'text':'1 Button'}],
    [{'textvariable':''},               {'textvariable':text_variable}],
    [{'underline':-1},                  {'underline':5}],
    [{'width':0},                       {'width':15}],
    [{'wraplength':'0'},                {'wraplength':'30'}],
]

for option1, option2 in settings:
    layout(option1, option2)

root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!