Python|为 tkinter 按钮添加样式
Tkinter 是一个Python标准库,用于创建 GUI(图形用户界面)应用程序。它是最常用的Python包之一。 Tkinter 在 Tk 主题小部件的帮助下支持传统和现代图形支持。 Tkinter 在tkinter.ttk中也有所有可用的小部件。
在 tkinter.ttk 按钮中添加样式有点令人毛骨悚然,因为它不支持直接实现。要在 ttk.Button 中添加样式,我们必须首先创建一个在 tkinter.ttk 中可用的样式类对象。
我们可以使用以下步骤创建 ttk.Button:
btn = ttk.Button(master, option = value, ...)
ttk.Button 选项 –
command: A function to be called when button is pressed.
text: Text which appears on the Button.
image: Image to be appeared on the Button.
style: Style to be used in rendering this button.
要在 ttk.Button 上添加样式,我们不能直接在选项中传递值。首先,我们必须创建一个 Style 对象,其创建方式如下:
style = ttk.Style()
下面的代码将只为选定的按钮添加样式,即,只有那些按钮会被更改,我们将在其中传递样式选项。
代码#1:
Python3
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Object
root = Tk()
# Set geometry (widthxheight)
root.geometry('100x100')
# This will create style object
style = Style()
# This will be adding style, and
# naming that style variable as
# W.Tbutton (TButton is used for ttk.Button).
style.configure('W.TButton', font =
('calibri', 10, 'bold', 'underline'),
foreground = 'red')
# Style will be reflected only on
# this button because we are providing
# style only on this Button.
''' Button 1'''
btn1 = Button(root, text = 'Quit !',
style = 'W.TButton',
command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
''' Button 2'''
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
Python3
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Root Object
root = Tk()
# Set Geometry(widthxheight)
root.geometry('100x100')
# Create style Object
style = Style()
# Will add style to every available button
# even though we are not passing style
# to every button widget.
style.configure('TButton', font =
('calibri', 10, 'bold', 'underline'),
foreground = 'red')
# button 1
btn1 = Button(root, text = 'Quit !',
style = 'TButton',
command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
# button 2
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
Python3
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Root Object
root = Tk()
# Set Geometry(widthxheight)
root.geometry('500x500')
# Create style Object
style = Style()
style.configure('TButton', font =
('calibri', 20, 'bold'),
borderwidth = '4')
# Changes will be reflected
# by the movement of mouse.
style.map('TButton', foreground = [('active', '!disabled', 'green')],
background = [('active', 'black')])
# button 1
btn1 = Button(root, text = 'Quit !', command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
# button 2
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
输出:
只有一个按钮会被设置样式,因为在上面的代码中,我们只在一个按钮中提供样式。代码 #2在所有可用按钮上应用样式
Python3
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Root Object
root = Tk()
# Set Geometry(widthxheight)
root.geometry('100x100')
# Create style Object
style = Style()
# Will add style to every available button
# even though we are not passing style
# to every button widget.
style.configure('TButton', font =
('calibri', 10, 'bold', 'underline'),
foreground = 'red')
# button 1
btn1 = Button(root, text = 'Quit !',
style = 'TButton',
command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
# button 2
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
输出:
现在,如果你想通过鼠标的移动来改变按钮的外观,即,现在当我们将鼠标悬停在按钮上时,它会改变它的颜色,当我们按下它时它会改变颜色,依此类推。代码 #3在鼠标悬停时更改颜色
Python3
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Root Object
root = Tk()
# Set Geometry(widthxheight)
root.geometry('500x500')
# Create style Object
style = Style()
style.configure('TButton', font =
('calibri', 20, 'bold'),
borderwidth = '4')
# Changes will be reflected
# by the movement of mouse.
style.map('TButton', foreground = [('active', '!disabled', 'green')],
background = [('active', 'black')])
# button 1
btn1 = Button(root, text = 'Quit !', command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100)
# button 2
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
输出: