Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- entry_design = {
- 'font': ("Helvetica", 11),
- 'insertbackground': '#ccc',
- 'highlightbackground': '#ccc',
- 'highlightcolor': '#ccc',
- 'relief': 'flat',
- 'bg': 'white',
- 'fg': '#333',
- 'bd': 3,
- 'width': 30,
- }
- label_design = {
- 'font': ("Helvetica", 12),
- 'bg': '#3d3d3d',
- 'fg': '#eee'
- }
- login_button_design = {
- 'highlightbackground': '#c0392b',
- 'activebackground': '#c0392b',
- 'activeforeground': 'white',
- 'relief': 'flat',
- 'bg': '#e74c3c',
- 'fg': 'white',
- 'bd': 0,
- }
- class LoginFrame(Frame):
- def __init__(self, master=None, *args, **kwargs):
- Frame.__init__(self, master=master, *args, **kwargs)
- self.configure(bg='#3d3d3d')
- self.configure(border=40)
- user_var = StringVar()
- pass_var = StringVar()
- user_var.set('Username')
- pass_var.set('Password')
- image = PhotoImage(file='login_2.png')
- app_image = Label(self, **label_design, image=image)
- app_image.grid(row=0, column=0, ipady=30)
- app_image.image = image
- self.user_entry = Entry(self, name='user_entry', textvariable=user_var, **entry_design)
- self.user_entry.bind('<FocusOut>', self.event_handler)
- self.user_entry.bind('<FocusIn>', self.event_handler)
- self.user_entry.grid(row=1, column=0, ipady=10)
- self.pass_entry = Entry(self, name='pass_entry', textvariable=pass_var, **entry_design)
- self.pass_entry.bind('<FocusOut>', self.event_handler)
- self.pass_entry.bind('<FocusIn>', self.event_handler)
- self.pass_entry.grid(row=2, column=0, ipady=10)
- self.info_var = StringVar()
- info_label = Label(self, textvariable=self.info_var, fg='orange')
- info_label.grid(row=3, column=0, sticky=EW, pady=5)
- info_label.configure(bg='#3d3d3d')
- login_button = Button(self, text='Login')
- login_button.grid(row=4, column=0, pady=4, sticky=E, ipadx=30, ipady=6)
- login_button.configure(command=self.check_credentials)
- login_button.configure(**login_button_design)
- def check_credentials(self):
- username = self.user_entry.get()
- password = self.user_entry.get()
- if username and password is not None or '':
- if username == 'admin' and password == 'admin':
- self.hide_login()
- return True
- self.info_var.set("Wrong username or password")
- self.after(3000, self.info_var.set, '')
- return False
- @staticmethod
- def event_handler(event):
- if str(event.type) is 'FocusIn':
- event.widget.delete(0, 'end')
- if str(event.widget).endswith('pass_entry'):
- event.widget.configure(show='*')
- return None
- return None
- if str(event.type) is 'FocusOut':
- if event.widget.get() is '':
- if str(event.widget).endswith('user_entry'):
- event.widget.insert('end', 'Username')
- return None
- if str(event.widget).endswith('pass_entry'):
- event.widget.configure(show='')
- event.widget.insert('end', 'Password')
- return None
- return None
- return None
- def show_login(self):
- self.master.logout_button.grid_forget()
- self.grid(row=0, column=0, sticky=NS)
- def hide_login(self):
- self.master.logout_button.grid(row=0, column=0, sticky=N)
- self.grid_forget()
- class MainWindow(Frame):
- def __init__(self, master=None, *args, **kwargs):
- Frame.__init__(self, master=master, *args, **kwargs)
- self.configure(bg='#3d3d3d', bd=20)
- self.loginFrame = LoginFrame(master=self)
- self.loginFrame.grid(row=0, column=0, sticky=NS)
- self.columnconfigure(0, weight=1)
- self.rowconfigure(0, weight=1)
- self.logout_button = Button(self, text='Logout')
- self.logout_button.configure(command=self.loginFrame.show_login)
- self.pack(fill='both', expand=True)
- if __name__ == '__main__':
- mainWindow = MainWindow()
- mainWindow.master.title('User Manager')
- mainWindow.master.geometry('1040x580')
- mainWindow.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement