Python Forum

Full Version: Placing a Placeholder in the entry box
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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.