ttk.Combobox当状态为只读和失焦时的毛刺 [英] ttk.Combobox glitch when state is read-only and out of focus

查看:234
本文介绍了ttk.Combobox当状态为只读和失焦时的毛刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当ttk.Combobox是只读的,而不是焦点,它的文本背景变成白色,不同于灰色字段背景,使组合框看起来很丑陋:





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

解决方案

解决方案是更改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

#完整许可文本:
# -------------------------------------------------- -----------
#do你想要公共许可证
#版本2,2004年12月

#版权所有(C)2004 Sam Hocevar< sam@hocevar.net>

#每个人都可以复制和分发此许可证文件的逐字或
#修改的副本,只要更改名称,就更改它是
#。

#你想要公共许可的什么
#用于复制,分发和条件的条款和条件
#修改

#0。你只是做什么你想要的。
#--------------------------------------------- -----------------

只读Ttk.Combobox样式演示模块

第二个组合框已经被略微修改为
在出现
焦点时使文本背景与组合框背景匹配

在只读状态(这是默认值)中,您可以注意到文本
背景在第一个(原始样式)组合框中变为白色
当焦点移动时第二个组合框看起来不错

使用按钮你可以测试两个工程完全相同
在可写状态


从随机导入randint
从tkinter导入Button,Frame,StringVar,Tk
从tkinter.ttk导入组合框,样式

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 =改变状态!,
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 stay'。)

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)

如果__name__ ==__main__:
root = Tk )
root.title('ttk.Combobox style')
应用程序(root).pack()
root.mainloop()
pre>

When a ttk.Combobox is read-only and not in focus, its text background gets white which differs from the gray field background and makes the combobox look ugly:

The desired style would be the second one's. How to make a combobox work like that?

解决方案

The solution is to change the ttk style like this:

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

This changes the behavior of the comboboxes globally. In the following demo (from which the screenshot of the question was made) I defined a custom style for the nicely working combobox as "Alt.TCombobox" and used that one for it:

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

# Full license text:
# --------------------------------------------------------------
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
#
# Everyone is permitted to copy and distribute verbatim or
# modified copies of this license document, and changing it is
# allowed as long as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND
# MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
# --------------------------------------------------------------

"""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()

这篇关于ttk.Combobox当状态为只读和失焦时的毛刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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