so I know my question isn't exactly easy to understand. I would like to create a place holder with tkinter and with tk.entry it works but with ttk.entry it doesn't work I have to print(self['foreground']) for the variable to be "loaded" and it works
Here's some code that I found and used: With this one when I focus the entry the placeholder doesn't leave
class EntryWithPlaceholder(ttk.Entry):
def __init__(self, master=None, placeholder="PLACEHOLDER", color='grey'):
super().__init__(master)
self.placeholder = placeholder
self.placeholder_color = color
self.default_fg_color = self['foreground']
self.bind("<FocusIn>", self.foc_in)
self.bind("<FocusOut>", self.foc_out)
self.put_placeholder()
def put_placeholder(self):
self.insert(0, self.placeholder)
self['foreground'] = self.placeholder_color
def foc_in(self, *args):
if self['foreground'] == self.placeholder_color:
self.delete('0', 'end')
self['foreground'] = self.default_fg_color
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
However when I do: print("self['foreground']") like this, it works:
class EntryWithPlaceholder(ttk.Entry):
def __init__(self, master=None, placeholder="PLACEHOLDER", color='grey'):
super().__init__(master)
self.placeholder = placeholder
self.placeholder_color = color
self.default_fg_color = self['foreground']
self.bind("<FocusIn>", self.foc_in)
self.bind("<FocusOut>", self.foc_out)
self.put_placeholder()
def put_placeholder(self):
self.insert(0, self.placeholder)
self['foreground'] = self.placeholder_color
def foc_in(self, *args):
print(self['foreground']) #<==== here
if self['foreground'] == self.placeholder_color:
self.delete('0', 'end')
self['foreground'] = self.default_fg_color
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
Moreover when I use tk.entry instead of ttk.entry everything works fine without the print(). However I need to use ttk.entry to get the windows theme.
The code comes from Nae, here's the link: How to add placeholder to an Entry in tkinter?