简体   繁体   English

使用 ttk 主题 azure-dark 和更改 ttk 组合框小部件的背景颜色

[英]Using ttk theme azure-dark and Changing background color of ttk combobox widget

I am using the ttk azure theme dark Azure-ttk-theme .我正在使用 ttk azure 主题 dark Azure-ttk-theme It sets the background color to nice modern looking UI.它将背景颜色设置为漂亮的现代 UI。 However, as seen in the images the background color of the tk window, Text widget and combobox widget is set as same.但是,如图所示,tk 窗口、文本小部件和组合框小部件的背景颜色设置为相同。 This looks bad when we are using all these together with some text in area(as seen on the right side of screenshot below).当我们将所有这些与区域中的一些文本一起使用时,这看起来很糟糕(如下面屏幕截图的右侧所示)。

I tried to modify the background of my combobox as below(pardon the full imports- just for replication):我尝试如下修改组合框的背景(请原谅完整的导入 - 仅用于复制):

from tkinter import *
from tkinter.ttk import *

root = Tk()
style = ttk.Style(root)
root.tk.call("source", SYSTEM_DIR / "azure.tcl")
root.tk.call("set_theme", "dark")
style.configure("TCombobox", fieldbackground= "orange", background= "white")
g_combo = Combobox(root, style="TCombobox")

But, the above code does not have any effect on my interface.但是,上面的代码对我的界面没有任何影响。 If someone has any idea on how I can handle this.如果有人对我如何处理这个有任何想法。 I might have to modify the tcl file for this theme.我可能需要为这个主题修改 tcl 文件。 Since, I have limited knowledge on tcl I am looking for answers/suggestions here.因为,我对 tcl 的了解有限,所以我在这里寻找答案/建议。

在此处输入图像描述

Ahh, well, I am answering this question because I also tried using this theme yesterday.啊,好吧,我正在回答这个问题,因为我昨天也尝试使用这个主题。 My idea was too something similar to yours.我的想法和你的想法太相似了。 I wanted to change the default colors and stuff to get a better UI.我想更改默认颜色和内容以获得更好的 UI。 Before getting into the solution, I have to tell you that this theme overrides the default values and designs to a custom-created one as set by the author.在进入解决方案之前,我必须告诉你,这个主题会覆盖默认值和设计为作者设置的自定义创建的。 So here's what you can do to achieve what you want.所以这就是你可以做的事情来实现你想要的。

As far as I know, the only way to do this is to edit the TCL file, which manages the colors of the widgets.据我所知,唯一的方法是编辑 TCL 文件,该文件管理小部件的颜色。 It would help if you had some idea about TCL language before you get into it so deep.如果您在深入了解TCL语言之前对它有所了解,将会有所帮助。 So I will tell you step by step:所以我一步一步告诉你:

  1. Go to dark.tcl file, and then find the widget's code where you want to change the background color.转到dark.tcl文件,然后找到要更改背景颜色的小部件代码。 Say you want to change the combo box's background color.假设您要更改组合框的背景颜色。 So if you are on windows, you can search "Combobox" by pressing ctrl+f after opening that file (I suggest using Notepad to edit it, it's more accessible).因此,如果您在 Windows 上,则可以在打开该文件后按ctrl+f搜索"Combobox" (我建议使用记事本进行编辑,它更易于访问)。

例子


Ok, so here are the codes where you want to change.好的,这是您要更改的代码。 As you can see in the third part of the code ( ttk::style element create Combobox.field ), it is used to create the combo box widget.正如您在代码的第三部分( ttk::style element create Combobox.field )中看到的,它用于创建组合框小部件。 You can keep trying changing different statements and customize the whole Combobox and its effects (If you know how to).您可以继续尝试更改不同的语句并自定义整个 Combobox 及其效果(如果您知道如何)。 But now lets focus on background color但现在让我们专注于背景颜色


  1. So in there, you see that the default background is set to an image known as box-basic.所以在那里,您会看到默认背景设置为称为 box-basic 的图像。

示例 2

So now you have to go toAzure-ttk-theme/theme/dark folder and find out where that box-basic file is.所以现在你必须去Azure-ttk-theme/theme/dark文件夹并找出那个box-basic文件在哪里。 Once you find it, you have to edit the colors of that image with respecting the same size and then replace the old one and make sure you set the same name.找到它后,您必须以相同的大小编辑该图像的颜色,然后替换旧图像并确保设置相同的名称。 I suggest you to use figma to replicate another image.我建议您使用figma复制另一个图像。 Its easy.这很简单。 Change it to any color you wish.将其更改为您想要的任何颜色。

And you are done!你完成了!

With some other theme fieldbackground would work, but not with Azure (or with any of my themes).使用其他一些主题fieldbackground可以使用,但不适用于 Azure(或我的任何主题)。

Tk handles PNG images containing transparency very badly, especially on Microsoft Windows. Tk 处理包含透明度的 PNG 图像非常糟糕,尤其是在 Microsoft Windows 上。 Do to reduce lagging, I removed the transparent areas of the image elements, and therefore where you'd expect fieldbackground to be, there's actually the images' background, which looks like as if it were fieldbg.为了减少滞后,我删除了图像元素的透明区域,因此您期望fieldbackground的位置实际上是图像的背景,看起来就像是 fieldbg。

So the solution is to edit the images, as TechieGeeke suggested .所以解决方案是编辑图像,正如TechieGeeke建议的那样。

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

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