gpt4 book ai didi

Python:如何在 ttk.Checkbutton 处于事件状态时更改其边框颜色?

转载 作者:行者123 更新时间:2023-12-01 06:43:56 25 4
gpt4 key购买 nike

我想更改 ttk.Checkbutton 小部件的边框颜色,默认情况下为浅蓝色。我尝试使用 ttk.Style() 的配置功能,并设法在激活时更改整个按钮或其他内容的颜色,但不能更改复选框的边框。另一个问题是我在哪里可以找到此类信息,例如可以以 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" .

人们会看到这样的东西: gifs我们看到一个充满“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 ):

checkbuttons

如何包含自定义检查按钮?

可以找到关于此的精彩帖子 here ,他们通过在 Canvas 上添加绿色和红色方 block 来创建自定义检查按钮,但是如果有人想要制作类似于您想要的东西怎么办?您可以在其中更改复选按钮的颜色。

如何更改默认复选按钮的边框颜色?

首先,让我们将所有检查按钮图像转换为 gif 格式,我已经为您完成了此操作,并且可以从 my Google Drive 下载它们。或者我的Gofile .

您需要从我的 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))

在这里,他们创建了两个空白图像,尺寸为 48 像素 X 24 像素,其中两个在左侧添加了一个绿色方 block ,另一个在右侧添加了一个。

我们的方法类似,但略有不同,相反,我们将加载您下载的两个 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+R8AAABhGlDQ1BJQ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mDWclQiaeIw4qqUb6QdVnhvMVZrdRY6578hYG8tpLmOs0RxLGEBJIQIaOGMiqwEKVVI8VEivZjHv5hx58kl0yuMhg5FlCFCsnxg//B727NwuSEmxSIAd0vtv0xCvTsAs26bX8f23bzBPA/A1da219tALOfpNfbWvgI6N8GLq7bmrwHXO4AQ0+6ZEiO5KcpFArA+xl9Uw4YuAX61tzeWvs4fQAy1NXyDXBwCIwVKXvd4929nb39e6bV3w/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)
else:
self.variable = kwargs['variable']
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

关于Python:如何在 ttk.Checkbutton 处于事件状态时更改其边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com