简体   繁体   English

如何更改 ttk.Separator 颜色

[英]How to change ttk.Separator color

I have a ttk.Separator widget and I would like it to be all black.我有一个 ttk.Separator 小部件,我希望它全黑。 I used ttk.Style() as follows我使用 ttk.Style() 如下

import Tkinter as TK
import ttk

self.line_style = ttk.Style()
self.line_style.configure("Line.TSeparator", background="#000000")
self.line = ttk.Separator(self.tk, orient=TK.VERTICAL, style="Line.TSeparator")
self.line.place(x = 1250,y = 0, height = self.tk.winfo_screenheight(), width = 8)

And the separator is black, 8 pixels wide, but it has a 1 pixel white line on the left side.并且分隔符是黑色的,8 像素宽,但左侧有 1 像素的白线。 Plase, do you know how could I get rid of it? Plase,你知道我怎么能摆脱它吗?

截图

Unfortunately you can't get rid of the 1 pixel white line, the only available option to configure for a ttk Separator is the background option.不幸的是,您无法摆脱 1 像素白线,为 ttk 分隔符配置的唯一可用选项是background选项。 You can see this by finding the layout of the Separator and then listing all of its configuration options as follows您可以通过找到 Separator 的布局然后列出其所有配置选项来看到这一点,如下所示

s = ttk.Style()
print(s.layout('TSeparator')) # [('Separator.separator', {'sticky': 'nswe'})]
print(s.element_options('Separator.separator')) # ('-orient', '-background')

If you set the background color to something other than black you'd see that this 1 pixel border is not always white but somehow related to chosen background color, either a lighter or darker shade.如果您将背景颜色设置为黑色以外的其他颜色,您会看到这个 1 像素的边框并不总是白色,而是与所选的背景颜色有关,无论是较浅还是较深的阴影。

The most viable workaround is to simply insert a styled Frame (ttk or regular tkinter) of the desired width instead of using a separator最可行的解决方法是简单地插入所需宽度的样式Frame (ttk 或常规 tkinter),而不是使用分隔符

I know that this is an old post but I had the same issue and figured out a work around.我知道这是一篇旧帖子,但我遇到了同样的问题并找到了解决方法。 When I create a 1 pixel high tkinter.Frame and stretch it out along the x-axis, I get something that looks like a ttk.Separator .当我创建一个 1 像素高的tkinter.Frame并沿 x 轴拉伸它时,我得到了一个看起来像ttk.Separator Example:示例:

import tkinter as tk

root = tk.Tk()

separator = tk.Frame(root, bg="blue", height=1, bd=0)
separator.pack(fill="x")

root.mainloop()

The colour of the separator is controlled by its bg keyword.分隔符的颜色由其bg关键字控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM