Monday, November 18, 2024
Google search engine
HomeLanguagesPlaceholder in Tkinter

Placeholder in Tkinter

Prerequisite: 

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python.

What is a placeholder and how can it be implemented using tkinter:

In programming, a placeholder may be a character, word, or string of characters that briefly takes the place of the final data. For instance, an applied scientist might recognize that she desires a precise range of values or variables, however does not however recognize what to input.

The Placeholder attribute is used to give hint that tells what input is expected by input field, or we can say it will give the short hint that is displayed within the input field before the user enters a value.

In Tkinter there is no in-built method or property for placeholder but it can still be executed using the modules’ inbuilt functions.

Approach:

  • Import module
  • Create Normal Tkinter Window
  • Add Entry box

Syntax:

Entry(Object Name, **attr)

  • Add placeholder, here it is done using insert() and then the same is bonded to the box

Syntax:

insert(index) 

  • Execute code

Program:

Python3




# Import Module
from tkinter import *
  
# Create Tkinter Object
root = Tk()
  
# Set geometry
root.geometry('400x400')
  
# call function when we click on entry box
def click(*args):
    playlist_url.delete(0, 'end')
  
# call function when we leave entry box
def leave(*args):
    playlist_url.delete(0, 'end')
    playlist_url.insert(0, 'Enter Text:- ')
    root.focus()
  
  
# Add Entry Box
playlist_url = Entry(root, width=60)
  
# Add text in Entry box
playlist_url.insert(0, 'Enter Text:- ')
playlist_url.pack(pady=10)
  
# Use bind method
playlist_url.bind("<Button-1>", click)
playlist_url.bind("<Leave>", leave)
  
# Execute Tkinter
root.mainloop()


Output:

RELATED ARTICLES

Most Popular

Recent Comments