我想更改ttk.Checkbutton小部件的边框颜色,默认情况下为浅蓝色。我尝试使用ttk.Style()的configure函数,并设法在整个按钮或其他按钮处于活动状态时更改了其颜色,但未更改Checkbox的边框。另一个问题是,在哪里可以找到此类信息,例如可以以ttk样式更改的所有选项的某种文档?
这里是我尝试过的示例:
from tkinter import ttk
root = Tk()
s = ttk.Style()
s.configure('MyOwn.TCheckbutton',
background='white',)
s.map('MyOwn.TCheckbutton',
foreground=[('active', 'red')])
cb = ttk.Checkbutton(root,text='Text',style='MyOwn.TCheckbutton')
cb.pack()
root.mainloop()
当我将鼠标悬停在复选框上时,我可以更改文本的颜色,但是我找不到如何更改复选框的边框颜色。
我相信您遇到的问题是Ttk Checkbutton实际上不是绘制的小部件,而实际上是图像。
[如果查看github项目ttkthemes,然后查看themes folder,然后转到radiance folder(这是一个类似UBUNTU的主题),然后转到to the folder inside that labelled "radiance"。
一个人会看到这样的东西:我们看到一个装有“ GIF”的文件夹,如果向下滚动并找到这些文件,则它们是图像文件:
check-dc.gif (Disabled Deselected)
check-du.gif (Disabled Deselected)
check-nc.gif (Normal Selected)
check-nu.gif (Normal Deselected)
我们可以查看是否浏览这些文件,这些都是复选按钮的所有可能状态。
在Windows下,这些检查按钮看起来像这样(生成with this code ::
关于这方面的一篇很棒的文章here,他们在其中通过在画布上添加绿色和红色正方形来创建自定义复选按钮,但是如果您想做出与您想要的相似的东西该怎么办。更改复选框按钮颜色的位置。
首先,我们将所有Checkbutton图像转换为gif格式,我已经为您完成了这些操作,可以从my Google Drive下载它们。
您需要从我的Google云端硬盘中获取:
check-nc.gif (Normal Selected)
check-nu.gif (Normal Deselected)
因为您不必担心停用的值,因为它们永远不会集中。
现在让我们看看它们如何在上述文章中加载图像:
on_image = tk.PhotoImage(width=48, height=24)
off_image = tk.PhotoImage(width=48, height=24)
on_image.put(("green",), to=(0, 0, 23,23))
off_image.put(("red",), to=(24, 0, 47, 23))
[他们在这里创建了两个空白图像,分别为48px X 24px,其中两个在左侧添加了一个绿色正方形,另一个在右侧添加了一个绿色正方形。
我们的处理方法类似,但略有不同,相反,我们将加载下载的两个gif文件,然后编写一个脚本,该脚本将制作4个矩形,这些矩形将覆盖复选按钮的当前灰色轮廓。像这样:
import tkinter as tk
root = tk.Tk()
def edit_check(colour, image):
image.put((colour,), to=(0, 0, 1, 13)) # LEFT
image.put((colour,), to=(0, 0, 13, 1)) # TOP
image.put((colour,), to=(12, 0, 13, 13)) # RIGHT
image.put((colour,), to=(0, 12, 13, 13)) # BOTTOM
def focus_in(event):
print(event)
image = off_image if var1.get() == 0 else on_image
edit_check("red", image)
cb1.configure(image=image)
cb1.image = image
def focus_out(event):
print(event)
image = off_image if var1.get() == 0 else on_image
edit_check("black", image)
cb1.configure(image=image)
cb1.image = image
on_image = tk.PhotoImage(file="check-nc.gif") # Instead of creating a new image we open an existing one
off_image = tk.PhotoImage(file="check-nu.gif") # Instead of creating a new image we open an existing one
var1 = tk.IntVar(value=0)
cb1 = tk.Checkbutton(root, image=off_image, selectimage=on_image, indicatoron=False,
onvalue=1, offvalue=0, variable=var1, offrelief='sunken')
cb1.pack(padx=20, pady=10)
# THESE IF YOU WANT TRUE FOCUS
#cb1.bind("<FocusIn>", focus_in)
#cb1.bind("<FocusOut>", focus_out)
# THESE IF YOU WANT MOUSE OVER
cb1.bind("<Enter>", focus_in)
cb1.bind("<Leave>", focus_out)
root.mainloop()
这是针对您的用例的更好的系统(请注意,由于文件另存为base64字符串,因此不需要任何图像文件)
import tkinter as tk
check_nu = b'iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TRZEWB4uIOGSoTi2IijpKFYtgobQVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi4uqk6CIl/i8ptIjx4Lgf7+497t4BQqPCVLNrHFA1y0jFY2I2tyr2vCKAIAYRwYzETD2RXszAc3zdw8fXuyjP8j735wgqeZMBPpF4jumGRbxBP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s4fQAy1NXyDXBwCIwVKXvd4929nb39e6bV3w/0UXLbKEvbjQAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB+MMDRctDrVlNE0AAAAjdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVAgd2l0aCBsb3ZlyGW0XgAAAJdJREFUKM+d0rENAyEMBdDvKA1zeART07MIGzAFE7EDK1B5CMqf7pIUlyP3JZdP+rItZkb8mScAjDFkF8QY+cCNbCF3R86ZvXduIXdHKYWqipSSXKJP0FqTEMK7Xu+dOWe6+yU4UEpJVBWlFLr7TwAAYmYcY8haC7VWzjkBAGfga+UhBLTWRFVPwREzI0nsjpndO67c+b0XBDxvkWRMW24AAAAASUVORK5CYII='
class example_window:
def __init__(self, master):
self.master = master
chckbox = custom_checkbox(master, "red")
chckbox.pack(padx=20, pady=10)
class custom_checkbox(tk.Checkbutton):
def __init__(self, parent, colour, *args, **kwargs):
default_kwargs = {'image': None, 'selectimage': None, 'indicatoron': False,
'onvalue': 1, 'offvalue': 0, 'variable': None, 'offrelief': 'sunken'}
for key, value in default_kwargs.items():
if key not in kwargs:
kwargs[key] = value
self.colour = colour
self.curr_colour = "black"
self.hover = False
if kwargs['variable'] is None:
self.variable = kwargs['variable'] = tk.IntVar(value=1)
print(kwargs, kwargs['variable'].get())
if kwargs['variable'].get() == 0:
if kwargs['image'] is None:
self.off_image = kwargs['image'] = tk.PhotoImage(data=check_nu)
else:
self.off_image = kwargs['image']
if kwargs['selectimage'] is None:
self.on_image = tk.PhotoImage(data=check_nc)
else:
self.on_image = kwargs['selectimage']
else:
if kwargs['image'] is None:
self.on_image = kwargs['image'] = tk.PhotoImage(data=check_nc)
else:
self.on_image = kwargs['image']
if kwargs['selectimage'] is None:
self.off_image = tk.PhotoImage(data=check_nu)
else:
self.off_image = kwargs['selectimage']
tk.Checkbutton.__init__(self, parent, *args, **kwargs)
self.bind("<Enter>", self.focus_in)
self.bind("<Leave>", self.focus_out)
self.variable.trace("w", self.focus_update)
def edit_check(self, colour, image):
image.put((colour,), to=(0, 0, 1, 13)) # LEFT
image.put((colour,), to=(0, 0, 13, 1)) # TOP
image.put((colour,), to=(12, 0, 13, 13)) # RIGHT
image.put((colour,), to=(0, 12, 13, 13)) # BOTTOM
def focus_in(self, event=None):
image = self.on_image if self.variable.get() == self['onvalue'] else self.off_image
self.edit_check(self.colour, image)
self.curr_colour = self.colour
self.configure(image=image)
self.image = image
self.hover = True
def focus_out(self, event=None):
image = self.on_image if self.variable.get() == self['onvalue'] else self.off_image
self.edit_check("black", image)
self.curr_colour = "black"
self.configure(image=image)
self.image = image
self.hover = False
def focus_update(self, *args):
if self.variable.get() == self['onvalue']:
image = self.on_image
else:
image = self.off_image
self.configure(image=image)
self.image = image
if self.hover:
self.focus_in()
def main():
root = tk.Tk()
example_window_gui = example_window(root)
root.mainloop()
if __name__ == '__main__':
main()
如果您希望能够向此小部件添加文本,请签出this code
您必须使用带有ttk复选按钮的样式图,这与可以使用config的常规tk不同。参见2个链接的参考文献
地图https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-map.html
ttk复选按钮https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Checkbutton.html
样式信息链接
https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-style-layer.html
https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-layouts.html