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"):
    normal_color = entry.cget("fg")

    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)
        
            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)
            
            placeholder_data[1] = True

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

    entry.bind('<FocusIn>', on_focusin)
    entry.bind('<FocusOut>', on_focusout)

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...')
    
    login_frame.grid_rowconfigure(0, minsize=28)
    login_frame.grid_rowconfigure(1, minsize=28)
    
    root.mainloop()

Diff to Previous Revision

--- revision 2 2017-03-31 20:09:00
+++ revision 3 2017-03-31 21:44:13
@@ -1,5 +1,3 @@
-# Author: Miguel Martinez Lopez
-
 try:
     import Tkinter as tk
 except ImportError:
@@ -40,7 +38,13 @@
     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...')

History