Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add placeholder to an Entry in tkinter?

I have created a login window in tkinter which has two Entry field, first one is Username and second one is Password.
code

from tkinter import *  ui = Tk()  e1 = Entry(ui) #i need a placeholder "Username" in the above entry field e1.pack()  ui.mainloop() 

I want a placeholder called "Username" in the Entry, but if you click inside the entry box, the text should disappear.

like image 274
Pierce Avatar asked Jan 07 '15 13:01

Pierce


People also ask

How do you add a placeholder in Python?

In Python, you can use {} as placeholders for strings and use format() to fill in the placeholder with string literals. For example: print("You have {} apples.". format(5)) # this will print: # You have 5 apples.

What is __ init __ In tkinter?

Basically, the __init__ method of a class is called as soon as an instance of that class is created. An example of the instance being created in your code is : app = Application(master=root) this means that your class must be called 'Application' though you haven't included that part.

Can you use place and pack tkinter?

pack is the easiest layout manager to use with Tkinter. Instead of declaring the precise location of a widget, pack() declares the positioning of widgets in relation to each other. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning.


2 Answers

You can create a class that inherits from Entry like below:

import tkinter as tk  class EntryWithPlaceholder(tk.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['fg']          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['fg'] = self.placeholder_color      def foc_in(self, *args):         if self['fg'] == self.placeholder_color:             self.delete('0', 'end')             self['fg'] = self.default_fg_color      def foc_out(self, *args):         if not self.get():             self.put_placeholder()  if __name__ == "__main__":      root = tk.Tk()      username = EntryWithPlaceholder(root, "username")     password = EntryWithPlaceholder(root, "password", 'blue')     username.pack()     password.pack()       root.mainloop() 
like image 70
Nae Avatar answered Sep 28 '22 00:09

Nae


You need to set a default value for this entry. Like this:

from tkinter import *  ui = Tk()  e1 = Entry(ui) e1.insert(0, 'username') e1.pack()  ui.mainloop() 

Then if you want to delete the content when you click the entry, then you have to bind a mouse click event with an event handler method to update content of this entry. Here is a link for you.

like image 25
Stephen Lin Avatar answered Sep 28 '22 01:09

Stephen Lin