문제

I have an instance of ttk.Entry. The user clicks it. I have the event bound. Depending on some condition, I either want the input cursor to appear and allow typing or I essentially want to ignore the click and not have the input cursor appear in the ttk.Entry. I don't want to have to use the readonly or disabled states.

Manipulating focus has not effect.

도움이 되었습니까?

해결책 2

After trawling the ttk documentation, this does the trick:

    ttk.Style().map("TEntry",
                    foreground=[('disabled', 'black')],
                    fieldbackground=[('disabled','white')]
                    )
    widget['state'] = 'disabled'

다른 팁

Here is a class that does what you ask.

class MyEntry(Entry):

    def disable(self):
        self.__old_insertontime = self.cget('insertontime')
        self.config(insertontime=0)
        self.bind('<Key>', lambda e: 'break')

    def enable(self):
        self.unbind('<Key>')
        if self.cget('insertontime') == 0:
            self.config(insertontime=self.__old_insertontime)

However, since your real concern is that you don't want a disabled Entry to look disabled, just set the colors of disabledbackground and disabledforground to match the colors of background and forground. If you need this rolled into a class, do it like this:

class MyEntry(Entry):
    def __init__(self, *args, **kwds):
        Entry.__init__(self, *args, **kwds)
        self.config(disabledbackground=self.cget('background'))
        self.config(disabledforeground=self.cget('foreground'))

And use it like this:

e = MyEntry(root)
e.config(state=DISABLED) # or state=NORMAL

Note. Be careful when reinventing gui conventions. Having something that looks enabled act disabled can be confusing for users. So don't change this unless you have good reason.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top