CONRADINE POWELL
CONRADINE POWELL

Reputation: 3

How to make the text disappear in this text box?

I am trying to make the default text disappear when I click in text box to enter data. Also, if I should click on an Add Sales Data button, how would I make a new window pop up for them to enter the data?

from tkinter import *

root=Tk()
root.title("CPS")

menu = Label(root, text="MAIN MENU")
menu1 = Label(root, text="Choose an option below ")
menu2 = Button( root, text="Calculate sales data",
padx=110)
menu3 = Button(root, text=" Exit the Program", padx=70)

salesid =Entry(root, width=30)
salesid.insert(0,"Enter Your Sales ID: ")

salesamount =Entry(root, width=30)
salesamount.insert(0,"Enter Your Sales Amount: ")

class_type =Entry(root, width=30)
class_type.insert(0,"Enter Your Class type: ")

menu.pack()
menu1.pack()
menu2.pack()
menu3.pack()

salesid.pack()
salesamount.pack()
class_type.pack()

root.mainloop()

Upvotes: 0

Views: 920

Answers (1)

Billy
Billy

Reputation: 1207

You are trying to make a placeholder/hint for your Entry. Tkinter Entrys, by default, doesn't have an option for placeholder text.

Solution

For making the default text disappear when you click in text box, you can add a binding for the mouse button to the Entry, to delete the existing text.

import tkinter as tk
...

salesid.bind("<Button-1>", lambda event: salesid.delete(0, tk.END))

Do notice that this does not bring back the placeholder text even after the focus is lost. In order to do that, you can check if the text in entry is the placeholder or not, and do deletions and insertions based on that. Example:

def focusIn(entry, placeholder):
    if entry.get() == placeholder:
        entry.delete(0, tk.END)

def focusOut(entry, placeholder):
    if entry.get() == "":
        entry.insert(0, placeholder)

placeholder = "Enter Your Sales ID: "
salesid = tk.Entry(root, width=30)
salesid.insert(0, placeholder)

salesid.bind("<FocusIn>", lambda e: focusIn(salesid, placeholder))
salesid.bind("<FocusOut>", lambda e: focusOut(salesid, "Enter Your Sales ID: "))

Now you can make this a custom Entry widget which can take a placeholder option:

class PlaceholderEntry(tk.Entry):
    def __init__(self, master, placeholder=None, *args, **kwargs):
        super().__init__(master, *args, **kwargs)
        self.placeholder = placeholder
        self.insert(0, self.placeholder)

        self.bind("<FocusIn>", self.focusIn)
        self.bind("<FocusOut>", self.focusOut)

    def focusIn(self, _):
        if self.get() == self.placeholder:
            self.delete(0, tk.END)

    def focusOut(self, _):
        if self.get() == "":
            self.insert(0, self.placeholder)

salesid = PlaceholderEntry(root, width=30, placeholder="Enter Your Sales ID: ")

Reference

Suggestions

A better way to approach your problem here would be using labels to show what the Entrys are for. You can create a Frame for all the labels and Entrys, then use grid geometry manager to add them like a table. Here's an example:

# try not to import globally
import tkinter as tk


root = tk.Tk()
root.title("CPS")

menu = tk.Label(root, text="MAIN MENU")
menu1 = tk.Label(root, text="Choose an option below ")
menu2 = tk.Button( root, text="Calculate sales data", padx=110)
menu3 = tk.Button(root, text=" Exit the Program", padx=70)

menu.pack()
menu1.pack()
menu2.pack()
menu3.pack()

# Frame to hold Labels and Entrys
details = tk.Frame()
details.pack(fill=tk.BOTH, expand=True)

lbl_salesid = tk.Label(details, text="Enter Your Sales ID: ")
salesid = tk.Entry(details, width=30)
lbl_sales_amount = tk.Label(details, text="Enter Your Sales Amount: ")
salesamount = tk.Entry(details, width=30)
lbl_class_type = tk.Label(details, text="Enter Your Class Type: ")
class_type = tk.Entry(details, width=30)

lbl_salesid.grid(row=0, column=0, sticky=tk.W)
salesid.grid(row=0, column=1)
lbl_sales_amount.grid(row=1, column=0, sticky=tk.W)
salesamount.grid(row=1, column=1)
lbl_class_type.grid(row=2, column=0, sticky=tk.W)
class_type.grid(row=2, column=1)

root.mainloop()

enter image description here

Upvotes: 1

Related Questions