I made a placeholder for entry widgets from tkinter, so I was wondering how good it is. I just started learning Python a bit before, so please do correct me if I'm wrong or if OI missed something, or some easy steps or where it loses the efficiency.
Code:
import tkinter as tk from tkinter import ttk class PlaceholderEntry(tkttk.Entry): ''' Custom modern Placeholder Entry box, takes positional argument master and placeholder\n Use acquire() for getting output from entry widget\n Use shove() for inserting into entry widget\n Use remove() for deleting from entry widget\n Use length() for getting the length of text in the widget\n BUG 1: Possible bugs with binding to this class\n BUG 2: AnomalousPotential behaviourbugs with config or configure method ''' def __init__(self, master, placeholder, **kwargs): # style for ttk widget self.s = ttk.Style() self.s.configure('my.TEntry', foreground='black', font=(0, 0, 'normal')) self.s.configure('placeholder.TEntry', foreground='grey', font=(0, 0, 'bold')) # init entry box ttk.Entry.__init__(self, master, style='my.TEntry', **kwargs) self.text = placeholder self.__has_placeholder = False # placeholder flag # add placeholder if box empty self._add() # bindings of the widget self.bind('<FocusIn>', self._clear) self.bind('<FocusOut>', self._add) self.bind_allbind('<Key>''<KeyRelease>', self._normal) self.bind_all('<Button-1>', self._cursor) def _clear(self, *args): # method to remove the placeholder if self.get() == self.text and self.__has_placeholder: # remove placeholder when focus gain self.delete(0, tk.END) self.s.configure('mystyle='my.TEntry', foreground='black', font=(0, 0, 'normal')) self.__has_placeholder = False #set # set flag to false def _add(self, *args): # method to add placeholder if self.get() == '' and not self.__has_placeholder: # if no text add placeholder self.s.configure('mystyle='placeholder.TEntry', foreground='grey', font=(0, 0, 'bold')) self.insert(0, self.text) # insert placeholder self.icursor(0) # move insertion cursor to start of entrybox self.__has_placeholder = True #set # set flag to true def _normal(self, *args): #method # method to set the text to normal properties self._add() # if empty add placeholder if self.get() == self.text and self.__has_placeholder: # clear the placeholder if starts typing self.bind('<Key>', self._clear) self.icursor(-1) # keep insertion cursor to the end else: self.s.configure('mystyle='my.TEntry', foreground='black', font=(0, 0, 'normal')) # set normal font def acquire(self): # """Customcustom method to get the text"""text if self.get() == self.text and self.__has_placeholder: return 'None' else: return self.get() def shove(self, index, string): # """Customcustom method to insert text into entry"""entry self._clear() self.insert(index, string) def remove(self, first, last): # """Customcustom method to remove text from entry"""entry if self.get() != self.text: self.delete(first, last) self._add() elif self.acquire() == self.text and not self.__has_placeholder: self.delete(first, last) self._add() def length(self): """Custom method to get the length of text in the entry widget""" if self.get() == self.text and self.__has_placeholder: return 0 else: return len(self.get()) def _cursor(self, *args): # method to not allow user to move cursor when placeholder exists if self.get() == self.text and self.__has_placeholder: self.icursor(0) #usage of class if __name__ == '__main__': root = tk.Tk() e = PlaceholderEntry(root,placeholder='Type something here...') e.pack(padx=10,pady=10) root.mainloop() Edit:
The older update used global changes, which lead to problems when using more than one entry widget, now that is fixed.