Home > Blockchain >  hint entry widget tkinter
hint entry widget tkinter

Time:11-29

I am trying to code a program which will show a login screen. I'm really interested in showing some kind of hint in the entry widget. For example, in the password field it will show “enter password” and once you click into the widget, it'll get clear. I've already implemented the function which will clear the widget, but I can not connect it to my entry widgets. Can somebody please help?

"""

def login():
    db = sqlite3.connect('login.sqlite3')
    db.execute('CREATE TABLE IF NOT EXISTS login (email TEXT, passwort TEXT)')
    db.execute("INSERT INTO login(email, passwort) VALUES('admin','admin')")
    db.execute("INSERT INTO login(email, passwort) VALUES('[email protected]','DafürGibtEsEine1 ')")
    cur = db.cursor()
    cur.execute("SELECT * FROM login WHERE email=? AND passwort=?", (email_var.get(), password_var.get()))
    row = cur.fetchone()
    if row:
        messagebox.showinfo('Information', 'Login succesful')
    else:
        messagebox.showerror('Error','Login error')    

    cur.connection.commit()
    db.close()

login_screen    = tk.Tk()
login_screen.title("Login")

login_screen.geometry("1280x720 50 50")
login_screen.resizable(False, False)

email_var       = tk.StringVar()
password_var    = tk.StringVar()
 
def registration():
    
    # make docstring pretty
    """registration function:
        this function saves the password and email the user enters 
        then it prints them on the screen
    """
    email       = email_var.get()
    password    = password_var.get()
     
    print("Email : "   email)
    print("Password : "   password)
     
    email_var.set("")
    password_var.set("")

frame = LabelFrame(login_screen, text = "Login: ", pady = 10, padx = 10)
frame.grid(row = 0, column = 0, padx = 30, pady = 30)

name_label         = tk.Label(frame, text = 'Email', font=('arial',10, 'bold'))
name_label.grid(row=1,column=1, sticky = tk.W, padx = 5, pady = 5)


password_label     = tk.Label(frame, text = 'Password', font = ('arial',10,'bold'))
password_label.grid(row=2,column=1, sticky = tk.W, padx = 5, pady = 5)

entry_name         = tk.Entry(frame,textvariable = email_var, font=('arial',10,'normal'))
entry_name.grid(row=1,column=2, sticky = tk.W, padx = 5, pady = 5)

entry_password     = tk.Entry(frame, textvariable = password_var, font = ('arial',10,'normal'), show = '*')
entry_password.grid(row=2,column=2, sticky = tk.W, padx = 5, pady = 5)

submit_button      = tk.Button(frame,text = 'Submit', font = ('arial', 10, 'normal'), command = login) #davor ,command = registration
submit_button.grid(row=3,column=2)

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

login_screen.mainloop()

"""

CodePudding user response:

The simple answer: You never created a instance of the EntryWithPlaceholder class.

entry_password     = tk.Entry(frame, textvariable = password_var, font = ('arial',10,'normal'), show = '*')

This should be:

entry_password     = EntryWithPlaceholder(frame, textvariable = password_var, font = ('arial',10,'normal'), show = '*')

And I made some changes to the class to see it actually work.

class EntryWithPlaceholder(tk.Entry):
def __init__(self, master=None, font = None, placeholder="PLACEHOLDER", color='grey', textvariable = None):
    super().__init__()

    self.placeholder = placeholder
    self.placeholder_color = color
    self.default_fg_color = self['fg']
    self['textvariable'] = textvariable
    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['show'] = '*'
        self.delete('0', 'end')
        self['fg'] = self.default_fg_color

def foc_out(self, *args):
    if not self.get():
        self.put_placeholder()
        self['show'] = ''
  • Related