Home > OS >  Tkinter Custom widget: Is there any way to change the style of a ttk.Entry when inserting a textvari
Tkinter Custom widget: Is there any way to change the style of a ttk.Entry when inserting a textvari

Time:01-03

I have a custom ttk.Entry widget. I need to do an action when modifying the 'textvariable' via the .configure method, but I'm not getting it.

An example:

self.user = StringVar()
self.user.set('test')
self.my_ent = custom_Entry(self.frame)
self.my_ent.configure(textvariable=self.user)

When there is 'textvariable' in .configure, I would like to change the widget style

I tried to recreate the .configure method inside my custom widget, but I can't call .configure inside the .configure

def configure(self, cnf=None, **kw):
    for k, v in kw.items():
        if k == 'textvariable':
            self.delete('0', END)
            self.configure(style='custom.TEntry')
            self['show'] = ''
    return self._configure('configure', cnf, kw)

I can't do this inside .configure:

self.configure(style='custom.TEntry')
self['show'] = ''

complement

enter image description here

sample code https://www.mediafire.com/file/ehopuk4ljyj1mpi/frm_main.py.zip/file note: I updated the file

CodePudding user response:

You can call the base configure method via super()

This is the first paragraph in the documentation for super():

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.

For example:

def configure(self, **kwargs):
    super().configure(**kwargs)
    if "textvariable" in kwargs:
        self.delete(0, "end")
        super().configure(style='custom.TEntry', show='')
  • Related