Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- import ast
- import mysql.connector
- from mysql.connector import Error
- from PIL import Image, ImageTk
- #@@@@@@@@@@@@@@
- #username - 1
- #password - 1
- #@@@@@@@@@@@@@@
- root = Tk()
- root.title('Login')
- root.geometry('925x500+300+200')
- root.configure(bg="#fff")
- root.resizable(False,False)
- name = Label(root, text='LEXEME.IO', fg="black", bg='white', font=('Microsoft Yahei UI Light', 16, 'bold'))
- name.place(x=410, y=40)
- trial_no = 0
- def connect_to_db(): #database details
- try:
- connection = mysql.connector.connect(
- host='localhost',
- user='root',
- password='password',
- database='userregistration'
- )
- return connection
- except Error as e:
- messagebox.showerror("Database Error", f"Error connecting to MySQL: {e}")
- return None
- def trial(): #max number of trials
- global trial_no
- trial_no += 1
- print("Trial # is", trial_no)
- if trial_no==3:
- messagebox.showwarning("Warning","You have tried too many times") #if user fails sign in 3 times
- root.destroy() #closes the program
- def signin(): #sign in
- user_input=user.get()
- password_input=code.get()
- if user_input == "" or user_input == "UserID" or password_input == "" or password_input == "Password":
- messagebox.showerror("Entry error", "Retry username or password!") #if no entry then return error
- return
- try:
- mydb= connect_to_db()
- if mydb is None:
- return
- mycursor=mydb.cursor()
- command = "SELECT * FROM users WHERE username=%s AND password=%s" #opens the users table
- mycursor.execute(command, (user_input, password_input))
- myresult = mycursor.fetchone()
- print(myresult)
- if myresult is None: #if it cannot match the username and password in the table
- messagebox.showinfo("Invalid", "Invalid Username or Password!")
- trial()
- else:
- messagebox.showinfo("Login", "Successfully Logged in")
- #opens landing page
- def homepg():
- homepagewindow = Toplevel()
- homepagewindow.title("Homepage")
- homepagewindow.geometry('925x500+300+200')
- homepagewindow.configure(bg='#fff')
- homepagewindow.resizable(False, False)
- homepagewindow.update_idletasks()
- w= Label(homepagewindow, text="Hello World",fg="black", bg='white', font=('Microsoft Yahei UI Light', 23, 'bold'))
- w.place(x=410, y=40)
- min_w = 50
- max_w = 200
- cur_width = min_w #increasing width of the frame
- expanded = False
- frame_height = homepagewindow.winfo_height()
- frame = Frame(homepagewindow, bg='black', width=50, height=homepagewindow.winfo.height())
- frame.grid(row=0,column=0)
- def expand():
- nonlocal cur_width, expanded
- cur_width += 10 #increase width by 10
- rep = homepagewindow.after(5, expand) #repeats expansion every 5ms
- frame.config(width=cur_width)
- if cur_width >= max_w:
- expanded = True #fully expanded
- homepagewindow.after_cancel(rep) #stops the expansion
- fill()
- def contract():
- nonlocal cur_width, expanded
- cur_width -= 10
- rep = homepagewindow.after(5, contract)
- frame.config(width=cur_width)
- if cur_width <= min_w:
- expanded = False
- homepagewindow.after_cancel(rep)
- fill()
- def fill():
- if expanded:
- #Show a text and remove the icon
- home_b.config(text='Home', image='', font=(0,21))
- profile_b.config(text='Profile', image='', font=(0,21))
- savedfiles_b.config(text='Saved Files', image='', font=(0,21))
- questions_b.config(text='Saved Question', image='', font=(0,21))
- else:
- # Bring the icon back
- home_b.config(image=home, font=(0,21))
- profile_b.config(image=profile, font=(0,21))
- savedfiles_b.config(image=savedfiles, font=(0,21))
- questions_b.config(image=questions, font=(0,21))
- try:
- home = ImageTk.PhotoImage(Image.open('Images/home.png').resize((40,40,),Image.ANTIALIAS))
- profile = ImageTk.PhotoImage(Image.open('Images/profile.png').resize((40,40,),Image.ANTIALIAS))
- savedfiles = ImageTk.PhotoImage(Image.open('Images/savedfiles.png').resize((40,40,),Image.ANTIALIAS))
- questions = ImageTk.PhotoImage(Image.open('Images/questions.png').resize((40,40,),Image.ANTIALIAS))
- except Exception as e:
- print(f"Error loading images: {e}")
- return
- #Make the buttons with the icons to be shown
- home_b = Button(frame, image=home, bg='white', relief='flat')
- profile_b = Button(frame, image=profile, bg='white', relief='flat')
- savedfiles_b = Button(frame, image=savedfiles, bg='white', relief='flat')
- questions_b = Button(frame, image=questions, bg='white', relief='flat')
- #place them on the frame
- home_b.grid(row=0,column=0,pady=10)
- profile_b.grid(row=1,column=0,pady=50)
- savedfiles_b.grid(row=2,column=0, pady=70)
- questions_b.grid(row=3, column=0, pady=90)
- #Bind them to the frame,for when expanding or contracting
- frame.bind('<Enter>', lambda e: expand())
- frame.bind('<Leave>', lambda e: contract())
- #So that it does not depend on the widgets inside the frame
- frame.grid_propagate(False)
- homepg()
- except Error as e:
- messagebox.showerror("Error", f"Error: {e}")
- finally:
- if mydb.is_connected():
- mycursor.close()
- mydb.close()
- ##################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ start of sign up window
- def signup_command(): #sign up window
- signupwindow=Toplevel(root)
- signupwindow.title("Sign Up")
- signupwindow.geometry('925x500+300+200')
- signupwindow.configure(bg='#fff')
- signupwindow.resizable(False,False)
- frame=Frame(signupwindow,width=350, height=390, bg='#fff')
- frame.place(x=285,y=50)
- #title
- heading=Label(frame,text='Sign up', fg="black", bg='white', font=('Microsoft Yahei UI Light', 23, 'bold'))
- heading.place(x=100, y=5)
- def signup():
- user_input=user.get()
- password_input=code.get()
- confirm_password=confirm_code.get()
- if password_input == confirm_password:
- try:
- connection = connect_to_db()
- if connection is not None:
- cursor = connection.cursor()
- # Check if username already exists
- cursor.execute("SELECT username FROM users WHERE username=%s", (user_input,))
- result = cursor.fetchone()
- if result:
- messagebox.showerror('Error', 'Username already exists')
- else:
- # Insert new user
- cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (user_input, password_input))
- connection.commit()
- messagebox.showinfo('Signup', 'Successfully signed up')
- cursor.close()
- connection.close()
- except Error as e:
- messagebox.showerror('Error', f"Error: {e}")
- else:
- messagebox.showerror('Invalid', 'Both passwords should match')
- def sign(): #when you click on 'Already on Lexeme? Sign in' link
- signupwindow.destroy()
- ####-------------------------------------------------------------
- def on_enter(e): # username in sign up
- user.delete(0, 'end')
- def on_leave(e):
- if user.get()=='':
- user.insert(0, 'Username')
- user = Entry(frame,width=25, fg='black', border=0, bg='white', font=('Microsoft Yahei UI Light',11))
- user.place(x=30, y=80)
- user.insert(0, 'Username')
- user.bind("<FocusIn>", on_enter)
- user.bind("<FocusOut>", on_leave)
- Frame(frame,width=295, height=2, bg='black').place(x=25, y=107)
- ###-------------------------------------------------------------
- def on_enter(e): # passoword in sign up
- code.delete(0, 'end')
- def on_leave(e):
- if code.get()=='':
- code.insert(0, 'Password')
- code = Entry(frame,width=25, fg='black', border=0, bg='white', font=('Microsoft Yahei UI Light',11))
- code.place(x=30, y=150)
- code.insert(0, 'Password')
- code.bind("<FocusIn>", on_enter)
- code.bind("<FocusOut>", on_leave)
- Frame(frame,width=295, height=2, bg='black').place(x=25, y=177)
- ###-------------------------------------------------------------
- def on_enter(e): #confirm password in sign up
- confirm_code.delete(0, 'end')
- def on_leave(e):
- if confirm_code.get()=='':
- confirm_code.insert(0, 'Confirm Password')
- confirm_code = Entry(frame,width=25, fg='black', border=0, bg='white', font=('Microsoft Yahei UI Light',11))
- confirm_code.place(x=30, y=220)
- confirm_code.insert(0, 'Confirm Password')
- confirm_code.bind("<FocusIn>", on_enter)
- confirm_code.bind("<FocusOut>", on_leave)
- Frame(frame,width=295, height=2, bg='black').place(x=25, y=247)
- ###-------------------------------------------------------------
- Button(frame, width=39, pady=7, text='Sign Up', bg='black', fg='white', border=0, command=signup).place(x=32, y=280)
- label=Label(frame,text='Already on Lexeme?', fg='black', bg='white', font=('Microsoft Yahei UI Light', 9))
- label.place(x=90, y=330)
- signin=Button(frame,width=6, text='Sign In', border=0, bg='white', cursor='hand2', fg='black', command=sign)
- signin.place(x=205,y=331)
- signupwindow.mainloop()
- ##################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ end of sign up window
- frame = Frame(root, width=350, height=350, bg="White")
- frame.place(x=300,y=70)
- heading=Label(frame, text='Sign in', fg='#010101', bg='white', font=('Microsoft YaHei UI Light', 14, 'bold'))
- heading.place(x=25, y=30)
- #########----------------------------------------------------------------------
- def on_enter(e):
- user.delete(0, 'end')
- def on_leave(e):
- name=user.get()
- if name=='':
- user.insert(0,'UserID')
- user = Entry(frame, width=25,fg='black',border=0,bg="white",font=('Microsoft YaHei UI Light', 11))
- user.place(x=30,y=80)
- user.insert(0, 'Username')
- user.bind('<FocusIn>', on_enter)
- user.bind('<FocusOut>', on_leave)
- Frame(frame,width=295, height=2, bg='black').place(x=25,y=107)
- #########----------------------------------------------------------------------
- def on_enter(e):
- code.delete(0, 'end')
- def on_leave(e):
- name=code.get()
- if name=='':
- code.insert(0,'Password')
- code = Entry(frame, width=25,fg='black',border=0,bg="white",font=('Microsoft YaHei UI Light', 11))
- code.place(x=30,y=150)
- code.insert(0, 'Password')
- code.bind('<FocusIn>', on_enter)
- code.bind('<FocusOut>', on_leave)
- Frame(frame,width=295, height=2, bg='black').place(x=25,y=177)
- button_mode=True
- def hide():
- global button_mode
- if button_mode:
- eyeButton.config(image=closeye, activebackground="white")
- code.config(show="*")
- button_mode=False
- else:
- eyeButton.config(image=openeye, activebackground="white")
- code.config(show="")
- button_mode=True
- openeye=PhotoImage(file="Program_vr1/Images/openeye.png")
- closeye=PhotoImage(file="Program_vr1/Images/closeye.png")
- eyeButton=Button(frame,image=openeye, bg="#375174", bd="0", command=hide)
- eyeButton.place(x=580, y=410)
- ##############################################################
- Button(frame, width=39, pady=7, text='Sign in', bg='black', fg='white', border=1, command=signin).place(x=35,y=204)
- label=Label(frame,text="Don't have an account?",fg='black', bg='white', font=('Microsoft YaHei UI Light',9))
- label.place(x=75,y=270)
- sign_up= Button(frame, width=6, text='Sign up', border=0, bg='white', cursor='hand2', fg='black', command=signup_command)
- sign_up.place(x=210,y=271)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement