简体   繁体   English

如何自定义 tkinter/ttk 小部件和框架的边框颜色?

[英]How to customise the border color of tkinter/ttk widgets and frames?

Is there any way to customize the border color of a Tkinter/ttk widgets or frames?有没有办法自定义Tkinter/ttk小部件或框架的边框颜色?

Yes, you can create your own style and apply it to the widget or configure the attributes of an existing style. 是的,您可以创建自己的样式并将其应用于窗口小部件或配置现有样式的属性。

Some documentation and examples are in the official docs, specifically the section about ttk.Style . 一些文档和示例在官方文档中,特别是关于ttk.Style的部分。

Additional information on using styles (for several languages) can be found at tkdocs.com . 有关使用样式(多种语言)的更多信息,请访问tkdocs.com

Regarding a border on ttk.Frames: in lieu of styles, the best I can think of is to insert a ttk.Frame within a ttk.Frame.关于 ttk.Frames 的边框:代替 styles,我能想到的最好的方法是在 ttk.Frame 中插入一个 ttk.Frame。 The outer frame would use a standard "background" property with the border color of choice, and a padding equal to the desired border thickness.外框将使用标准的“背景”属性和选择的边框颜色,以及等于所需边框厚度的填充。 The inner frame then obscures the middle, so you have a line only around the edges.然后内部框架遮住了中间,所以你只有在边缘周围有一条线。 Works, but seems a little bit hacky (and a bit less wieldy if you're creating many widgets/borders).有效,但似乎有点笨拙(如果您要创建许多小部件/边框,则使用起来会少一些)。

I've searched quite a bit and haven't found or figured out a way to use a style to change a ttk.Frame's border color.我已经搜索了很多,但没有找到或想出一种使用样式来更改 ttk.Frame 边框颜色的方法。 Using something like style.configure( 'SeletectedItem.TFrame', highlightcolor='#78F', highlightthickness=2 ) or style.configure( 'SeletectedItem.TFrame', highlightbackground='#78F', highlightcolor='#78F', highlightthickness=2, borderwidth=2 ) (config options that work on a regular Tk.Frame) doesn't seem to do anything.使用类似style.configure( 'SeletectedItem.TFrame', highlightcolor='#78F', highlightthickness=2 )style.configure( 'SeletectedItem.TFrame', highlightbackground='#78F', highlightcolor='#78F', highlightthickness=2, borderwidth=2 ) (在常规 Tk.Frame 上工作的配置选项)似乎没有做任何事情。 With style.layout() I find just [('Frame.border', {'sticky': 'nswe'})] , and from style.element_options('TFrame.border' ) there is just ('-relief',) .使用style.layout()我发现只有[('Frame.border', {'sticky': 'nswe'})] ,而从style.element_options('TFrame.border' ) 那里只有('-relief',) . I don't know if I'm using the wrong option names or syntax (or what options are available for .configure() for different widget classes), or if this just isn't possible with styles.我不知道我是否使用了错误的选项名称或语法(或者对于不同的小部件类有哪些选项可用于.configure() ),或者 styles 是否无法做到这一点。 I'm interested if anyone has extra info or knows if this can be done.如果有人有额外的信息或知道是否可以这样做,我很感兴趣。

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

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