我正在嘗試撰寫一個將顯示登錄螢屏的程式。我真的很想在條目小部件中顯示某種提示。例如,在密碼欄位中,它會顯示“輸入密碼”,一旦您點擊進入小部件,它就會變得清晰。我已經實作了清除小部件的功能,但我無法將它連接到我的入口小部件。有人可以幫忙嗎?
"""
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()
"""
uj5u.com熱心網友回復:
簡單的答案:您從未創建過 EntryWithPlaceholder 類的實體。
entry_password = tk.Entry(frame, textvariable = password_var, font = ('arial',10,'normal'), show = '*')
這應該是:
entry_password = EntryWithPlaceholder(frame, textvariable = password_var, font = ('arial',10,'normal'), show = '*')
我對課程進行了一些更改以查看它是否真正起作用。
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'] = ''
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/370477.html
