python - ttk.Combobox 状态为只读且未聚焦时出现故障

标签 python python-3.x combobox tkinter ttk

当 ttk.Combobox 是只读的并且没有焦点时,它的文本背景变成白色,这与灰色字段背景不同,并且使组合框看起来很难看:

Example

所需的样式将是第二个。如何使组合框像那样工作?

最佳答案

解决办法是把ttk的样式改成这样:

s = ttk.Style()
s.map("TCombobox",
    selectbackground=[
        ('!readonly', '!focus', 'SystemWindow'),
        ('readonly', '!focus', 'SystemButtonFace'),
        ],
    )

这会全局更改组合框的行为。在下面的演示中(从中制作了问题的屏幕截图)我为工作良好的组合框定义了一个自定义样式作为“Alt.TCombobox”并使用了它:

# cboxdemo.py by Adam Szieberth (2013)
# Python 3.3.0

"""Read-only Ttk.Combobox style demo module.

The style of the second combobox has been slightly modified to
make text background match with combobox background when out of
focus.

In read-only state (which is default) you can notice that text
background gets white in the first (original styled) combobox
when focus moves towards. Second combobox looks nice then.

With the button you can test that the two works exactly the same
in writeable state.
"""

from random import randint
from tkinter import Button, Frame, StringVar, Tk
from tkinter.ttk import Combobox, Style

class App(Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.state = None
        self.style = Style()
        self.style.map("Alt.TCombobox",
            selectbackground=[
                ('!readonly', '!focus', 'SystemWindow'),
                ('readonly', '!focus', 'SystemButtonFace'),
                ],
            )
        self.button = Button(self, text="Change state!",
            command=self.switch)
        self.cbox1var, self.cbox2var = StringVar(), StringVar()
        self.cbox1 = Combobox(self,
            exportselection=0,
            values=["sex", "sleep", "eat", "drink", "dream",],
            textvariable=self.cbox1var,
            )
        self.cbox1.bind('<<ComboboxSelected>>', self.bfocus)
        self.cbox1.current(1)
        self.cbox2 = Combobox(self,
            exportselection=0,
            values=["fear", "clarity", "power", "old age",],
            style="Alt.TCombobox",
            textvariable=self.cbox2var,
            )
        self.cbox2.bind('<<ComboboxSelected>>', self.bfocus)
        self.cbox2.current(3)
        self.cbox1.pack()
        self.cbox2.pack()
        self.button.pack()
        self.switch()

    def bfocus(self, *args):
        if randint(0,1):
            self.button.focus()
            print('Focus moved!')
        else:
            print('Focus stayed.')

    def switch(self):
        if self.state == ['readonly']:
            self.state = ['!readonly']
            print('State is writeable!')
        else:
            self.state = ['readonly']
            print('State is read-only!')
        self.cbox1.state(self.state)
        self.cbox2.state(self.state)

if __name__ == "__main__":
    root = Tk()
    root.title('ttk.Combobox styling')
    App(root).pack()
    root.mainloop()

关于python - ttk.Combobox 状态为只读且未聚焦时出现故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18610519/

相关文章:

python - 在 numpy 数组中找到一些缺失点

python - 1 个代理的 asyncio 中的信号量/多个池锁 - aiohttp

python - 如何从 QTableWidget 列获取行数据?

python-3.x - Pytest 捕获不起作用 - caplog 和 capsys 为空

wpf - 绑定(bind)到相同数据源的互斥组合框 - MVVM 实现

python - 检查矩阵是否在 python 中对角占优势

python - 如何加快 Pandas 多级数据帧总和?

python - 在 Vim 中更改 Python for 循环的颜色

C# Combobox 项目文本更改?

extjs - 在 ExtJs 3.3.1 中,如何在不单击 EditorGrid 的情况下显示 ComboBox 下拉列表?