2

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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Thalrod_
  • 53
  • 3
  • 2
    For some reason, when you retrieve the `foreground` option of a ttk.Entry, you get a `color` object, rather than the string that was originally set. This object doesn't compare as equal to the string, so I don't see how your code could possibly work at all. Try doing the comparison as `if str(self['foreground']) == self.placeholder_color:`, which looks like it would fix the problem on the Tkinter version I'm using. – jasonharper Jun 05 '20 at 21:12
  • Hi, thank you very much, it's working :D ! I didn't think of that at all. Thank you very much, have a nice day :). – Thalrod_ Jun 05 '20 at 21:15

0 Answers0