Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from tkinter import *
import tkinter.messagebox as tm
login = Tk()
"""
Self is a object of this class.
def btn_click(self) is a function. what it does when you clicked the login button it will send a message to this function(def btn_click(self)). Then the function will get what user
put because my code has self.entry_user.get() and self.entry_pass.get(). The key word is get. When you are using get it will check what user put in the entry. Then it will automatically
check the users.txt file where we store user information such as username and password to check if user information are correct or wrong. Then there is import os ('python3 index.py') what it
does if user information are right it will send user to the index.py(to the Chatbot). If user put wrong information it will show an error message.
"""
class LoginFrame(Frame):
def __init__(self, master):
super().__init__(master)
#Title of the GUI
login.title("Login")
#Label created for the username and password
self.label_user = Label(self, text="Username")
self.label_pass = Label(self, text="Password")
#This is the enter where user can put anything they want
self.entry_user = Entry(self)
#When you are adding infromation on this entry it will show as "*"
self.entry_pass = Entry(self, show="*")
#These are grids.Grids are important because it shows layout of our functionalities
self.label_user.grid(row=0, column=0)
self.label_pass.grid(row=1, column=0)
self.entry_user.grid(row=0, column=2)
self.entry_pass.grid(row=1, column=2)
#This is the login button when user press this button it will send message to the btn_click function
self.login_btn = Button(self, text="Login", command = self.btn_click)
self.login_btn.grid(row=2, column=2)
self.pack()
#This is the function we are using to identify the user click
#Inside of this function there are 2 statements. I added this for get what user put inside the entry.
def btn_click(self):
username = self.entry_user.get()
password = self.entry_pass.get()
#---------------------------------------------------------------------------------------------------------------------------#
# Daniel's Code #
f = open('users.txt','r')
#This Code defies the value f where it will open the file 'users.txt' and the 'r' indicates that it will only read from the
#file.
for line in f:
if username == line.split('-')[0] and password == line.split('-')[1].rstrip():
#This code will read the 'users.txt' file line at a time. It will also read everything on the line in two ways, before the '-'
#and after. This helps us allow to match the username and password together.
import os
os.system('python3 index.py')
exit()
f.close()
#If the user information is correct, it will open the program and close the Login window.
tm.showerror("Login error", "Please check your username and password.")
#If user information are not correct it will show them an error message and they'll have to put in the information again.
#---------------------------------------------------------------------------------------------------------------------------#
lf = LoginFrame(login)
login.mainloop()