Question

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.

Was it helpful?

Solution 2

After trawling the ttk documentation, this does the trick:

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top