Welcome, guest | Sign In | My Account | Store | Cart
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

def add_placeholder_to(entry, placeholder, color="grey", font=None):
    normal_color = entry.cget("fg")
    normal_font = entry.cget("font")
    
    if font is None:
        font = normal_font

    placeholder_data = [placeholder, True]

    def on_focusin(event, entry=entry, normal_color=normal_color, placeholder_data=placeholder_data):
        if placeholder_data[1]:
            entry.delete(0, "end")
            entry.config(fg = normal_color, font=normal_font)
        
            placeholder_data[1] = False

    def on_focusout(event, entry=entry, color=color, placeholder_data=placeholder_data):
        if entry.get() == '':
            entry.insert(0, placeholder_data[0])
            entry.config(fg = color, font=font)
            
            placeholder_data[1] = True

    entry.insert(0, placeholder)
    entry.config(fg = color, font=font)

    entry.bind('<FocusIn>', on_focusin, add="+")
    entry.bind('<FocusOut>', on_focusout, add="+")
    
    return placeholder_data

if __name__ == "__main__":
    root = tk.Tk()

    login_frame = tk.LabelFrame(root, text="Login", padx=5, pady=5)
    login_frame.pack(padx=10, pady=10)
    
    label = tk.Label(login_frame, text="User: ")
    label.grid(row=0, column=0, sticky=tk.E)

    # I add a border of 1px width and color #bebebe to the entry using these parameters:
    #  - highlightthickness=1
    #  - highlightbackground="#bebebe"
    #
    entry = tk.Entry(login_frame, bd=1, bg="white", highlightbackground="#bebebe", highlightthickness=1)
    
    # I make the entry a little bit more height using ipady option
    entry.grid(row=0, column=1, ipady=1)

    add_placeholder_to(entry, 'Enter your username...')

    label = tk.Label(login_frame, text="Password: ")
    label.grid(row=1, column=0, sticky=tk.E)

    entry = tk.Entry(login_frame, bd=1, bg="white", highlightbackground="#bebebe", highlightthickness=1)
    entry.grid(row=1, column=1, ipady=1)

    add_placeholder_to(entry, 'Password...')
    
    # Every row has a minimum size
    login_frame.grid_rowconfigure(0, minsize=28)
    login_frame.grid_rowconfigure(1, minsize=28)
    
    root.mainloop()

Diff to Previous Revision

--- revision 4 2017-03-31 21:45:07
+++ revision 5 2017-04-01 19:27:15
@@ -3,31 +3,36 @@
 except ImportError:
     import tkinter as tk
 
-def add_placeholder_to(entry, placeholder, color="grey"):
+def add_placeholder_to(entry, placeholder, color="grey", font=None):
     normal_color = entry.cget("fg")
+    normal_font = entry.cget("font")
+    
+    if font is None:
+        font = normal_font
 
     placeholder_data = [placeholder, True]
 
     def on_focusin(event, entry=entry, normal_color=normal_color, placeholder_data=placeholder_data):
         if placeholder_data[1]:
-            entry.delete(0, "end") # delete all the text in the entry
-            entry.insert(0, '') #Insert blank for user input
-            entry.config(fg = normal_color)
+            entry.delete(0, "end")
+            entry.config(fg = normal_color, font=normal_font)
         
             placeholder_data[1] = False
 
     def on_focusout(event, entry=entry, color=color, placeholder_data=placeholder_data):
         if entry.get() == '':
             entry.insert(0, placeholder_data[0])
-            entry.config(fg = color)
+            entry.config(fg = color, font=font)
             
             placeholder_data[1] = True
 
     entry.insert(0, placeholder)
-    entry.config(fg = color)
+    entry.config(fg = color, font=font)
 
-    entry.bind('<FocusIn>', on_focusin)
-    entry.bind('<FocusOut>', on_focusout)
+    entry.bind('<FocusIn>', on_focusin, add="+")
+    entry.bind('<FocusOut>', on_focusout, add="+")
+    
+    return placeholder_data
 
 if __name__ == "__main__":
     root = tk.Tk()

History