ttk 入口背景色 [英] ttk Entry background colour

查看:55
本文介绍了ttk 入口背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我究竟如何从 ttk 更改 Entry 小部件的背景颜色?到目前为止,我所拥有的是:

How exactly do I change the background colour of an Entry widget from ttk? What I have so far is:

        self.estyle = ttk.Style()
        self.estyle.configure("EntryStyle.TEntry", background='black')
        self.estyle.map("EntryStyle.TEntry",
                        foreground=[('disabled', 'yellow'),
                                    ('active', 'blue')],
                        background=[('disabled', 'magenta'),
                                    ('active', 'green')],
                        highlightcolor=[('focus', 'green'),
                                        ('!focus', 'red')])
        self.urlentry_v = StringVar()
        self.urlentry = ttk.Entry(self.input_frame, style="EntryStyle.TEntry",
                                  textvariable=self.urlentry_v)

基本上,我已经改变了我能想到的一切,但文本条目仍然顽固地保持白色.

Basically, I've changed everything I can think of, but the text entry remains stubbornly white.

另外,有没有办法改变边框颜色?

Additionally, is there a way of changing the border colour?

推荐答案

经过很多的挖掘,我已经弄清楚了.尽管我必须努力寻找才能弄清楚这一点,但我想其他人会从中受益:

I've figured it out, after a lot of digging. As hard as I had to search to figure this out, I suppose others would benefit from this:

应用于 ttk.Entry 的标准样式根本不采用 fieldbackground 选项,这将改变文本输入字段的颜色.解决方案是创建一个确实响应该选项的新元素.

The standard style applied to ttk.Entry simply doesn't take a fieldbackground option, which would be what changes the colour of the text entry field. The solution is this to create a new element that does respond to the option.

from tkinter import *
from tkinter import ttk

root_window = Tk()

estyle = ttk.Style()
estyle.element_create("plain.field", "from", "clam")
estyle.layout("EntryStyle.TEntry",
                   [('Entry.plain.field', {'children': [(
                       'Entry.background', {'children': [(
                           'Entry.padding', {'children': [(
                               'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})],
                      'border':'2', 'sticky': 'nswe'})])
estyle.configure("EntryStyle.TEntry",
                 background="green", 
                 foreground="grey",
                 fieldbackground="black")
entry_v = StringVar()
entry = ttk.Entry(root_window, style="EntryStyle.TEntry", textvariable=entry_v)
entry.pack(padx=10, pady=10)

不幸的是,似乎更改边框颜色的唯一方法是将其设置为零边框宽度并将其嵌套在 充当 作为边框的框架中,或者定义一个新的布局项使用图像作为边框.

Unfortunately, it appears that the only way to change the border colour is to either give it zero border width and nest it in a frame that acts as its border, or to define a new layout item that uses an image as a border.

另外,请注意背景控制的唯一东西是非常小的角落空间;如果你仔细眯眼,你会看到每个角落都有一个绿色像素.

Additionally, note that the only thing the background controls is the very tiny corner space; if you squint closely, you can see a single pixel of green in each corner.

要将图像用作边框,您可以这样做:

To use an image as a border, you can do this:

img2 = PhotoImage("entryBorder", data="""
        R0lGODlhHQAdAOMNAAAAAAQGCAgLERkfLR0mODBFZTFFZTNIajtTezxTez1XgD5XgU
        Fch////////////ywAAAAAHQAdAAAEbHCQg5i9OGt0iFRaKGLKxBgCoK5s6woGc4Cp
        a9+AwFQM7ruYn1AVHP6KRhwyaVsyW87nKioFUKXXZ5a5TXaN32FYOD5eqsAzmlX2tZ
        XqNZGxYATkgAD9wCjUqgIFMgR1I4YZCx4TCYeGCR0DEQA7""")

oestyle = ttk.Style()
oestyle.element_create("blueborder", "image", "entryBorder",
                                   border=3, sticky="nsew")
oestyle.layout("OEntryStyle.TEntry",
               [('Entry.blueborder', {'children': [(
                   'Entry.padding', {'children': [(
                     'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})])
oestyle.configure("OEntryStyle.TEntry",
                 background="black",
                  foreground="grey")
oentry_v = StringVar()
oentry = ttk.Entry(root_window, style="OEntryStyle.TEntry", textvariable=oentry_v)
oentry.pack(padx=10, pady=10)

字符串是通过将我想要的边框图像作为 gif 提供给来生成的

The string of characters is generated by feeding an image of the borders I want as a gif to

import base64

with open('otherframeBorder.gif', 'rb') as f:
    encoded = base64.encodestring(f.read())
    print(encoded.decode('latin1'))

这篇关于ttk 入口背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆