Python Forum
[Tkinter] Placing a Placeholder in the entry box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Placing a Placeholder in the entry box
#1
Hi,
My placeholder works now but the problem lies with calling it in the SQL statement.

Without the placeholder, my SQL is working fine because each of my entry boxes have (example: textvariable="user") but when I place it in the configuration of my entry box the placeholder will also disappear. How can I make my placeholder works and also the SQL statement?
Thank you

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()

        self.user_input = EntryWithPlaceholder(root, 'USERNAME OR EMAIL')
        self.user_input.place(relx=0.18, rely=0.455, width=300, height=24)
        self.user_input.configure(font=("Corbel", 15))
        self.user_input.configure(borderwidth=0)
        self.user_input.configure(relief="flat")

    def login(self, Event=None):
        find_user = "SELECT * FROM employee WHERE ('username' :  u OR email_address = %s) and password = %s"
        cur.execute(find_user, [user.get(), user.get(), passwd.get()])
        results = cur.fetchall()
        if results:
            if results[0][10] == "Admin":
                messagebox.showinfo("Login Page", "The login is successful.")
                page1.user_input.delete(0, END)
                page1.pass_input.delete(0, END)

                root.withdraw()
                global adm
                global page2
                adm = Toplevel()
                page2 = Admin_Page(adm)
                # page2.time()
                adm.protocol("WM_DELETE_WINDOW", exitt)
                adm.mainloop()

            else:
                messagebox.showerror("Oops!!", "You are not an admin.")

        else:
            messagebox.showerror("Error", "Incorrect username or password.")
            page1.pass_input.delete(0, END)
Reply
#2
The EntryWithPlaceholder code you have taken from https://stackoverflow.com/questions/2782...in-tkinter
You should not be placing your code that makes new instances of itself inside of its FocusOut event handler.
self.user_input = EntryWithPlaceholder(root, 'USERNAME OR EMAIL')
Use the copied code as it is, create an instance of it, create your code separately.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,483 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,512 Mar-18-2019, 05:36 PM
Last Post: woooee
  Canvas not placing inside Grid leonv 2 3,942 Nov-21-2017, 05:37 PM
Last Post: leonv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020