Related
I've been experimenting with a sqlite3 for the first time and was developing a login system (with some help from articles and videos) and have noticed that all credentials inputted into the program are being accepted, I can't find a fault in the program so I could use some help.
Importing Important Libraries
import sqlite3
import bcrypt
class Database:
def __init__(self):
try:
self.conn = sqlite3.connect("usertable.db")
print("Successfully Opened Database")
self.curr = self.conn.cursor()
except:
print("Failed")
def createTable(self):
create_table = '''
CREATE TABLE IF NOT EXISTS cred(
id Integer PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL
);
'''
self.curr.execute(create_table)
self.conn.commit()
def insertData(self, data):
insert_data = """
INSERT INTO cred(username, password)
VALUES(?, ?);
"""
self.curr.execute(insert_data, data)
self.conn.commit()
def searchData(self, data):
search_data = '''
SELECT * FROM cred WHERE username = (?);
'''
self.curr.execute(search_data, data)
rows = self.curr.fetchall()
if rows == []:
return 1
return 0
def validateData(self, data, inputData):
validate_data = """
SELECT * FROM cred WHERE username = (?);
"""
self.curr.execute(validate_data, data)
row = self.curr.fetchall()
if row[0][1] == inputData[0]:
return row[0][2] == bcrypt.hashpw(inputData[1].encode(), row[0][2])
(The login and register classes)
from tkinter import *
from tkinter import messagebox
import bcrypt
from database import Database
db = Database()
db.createTable()
class Login:
def __init__(self):
self.loginWindow = Tk()
self.loginWindow.title("Login")
self.loginWindow.geometry("300x250")
self.label = Label(self.loginWindow, text="Login")
self.label.place(x=95, y=40)
self.usernames = StringVar()
self.passwords = StringVar()
self.usernameE = Entry(self.loginWindow, relief=FLAT, textvariable=self.usernames)
self.usernameE.place(x=70, y=80)
self.passwordE = Entry(self.loginWindow, show="*", relief=FLAT, textvariable = self.passwords)
self.passwordE.place(x=70, y=120)
self.username = self.usernames.get()
self.password = self.passwords.get()
self.submit = Button(self.loginWindow, text = "Submit", pady = 5, padx = 20, command = self.validate)
self.submit.place(x=100, y=150)
def validate(self):
data = (self.username,)
inputData = (self.username, self.password,)
try:
if (db.validateData(data, inputData)):
messagebox.showinfo("Successful", "Login Was Successful")
else:
messagebox.showerror("Error", "Wrong Credentials")
except IndexError:
messagebox.showerror("Error", "Wrong Credentials")
def run(self):
self.loginWindow.mainloop()
class Register:
def __init__(self):
self.registerWindow = Tk()
self.registerWindow.title("Register")
self.registerWindow.geometry("300x250")
self.label = Label(self.registerWindow, text="Register")
self.usernameS = StringVar()
self.passwordS = StringVar()
self.usernameE = Entry(self.registerWindow, relief = FLAT, textvariable = self.usernameS)
self.usernameE.place(x=70, y=80)
self.passwordE = Entry(self.registerWindow, show="*", relief = FLAT, textvariable = self.passwordS)
self.passwordE.place(x=70, y=120)
self.submit = Button(self.registerWindow, text="Submit", pady = 5, padx = 20, command = self.add)
self.submit.place(x=100, y=150)
self.username = self.usernameS.get()
self.password = self.passwordS.get()
self.salt = bcrypt.gensalt()
self.hashed = bcrypt.hashpw(self.password.encode(), self.salt)
def run(self):
self.registerWindow.mainloop()
def add(self):
data = (self.username,)
result = db.searchData(data)
print(result, result, result)
if result != 0:
data = (self.username, self.hashed)
db.insertData(data)
messagebox.showinfo("Successful", "Username Was Added")
else:
messagebox.showwarning("Warning", "Username already Exists")
(The main window class)
from tkinter import *
from login import Login, Register
class Window(object):
def __init__(self):
self.window = Tk()
self.window.title("Login Screen")
self.window.geometry("300x250")
self.label = Label(self.window, text = "Welcome to Login")
self.label.place(x=95, y=40)
self.login = Button(self.window, text = "Login", pady = 5, padx = 30, command = login)
self.login.place(x=100, y=100)
self.register = Button(self.window, text = "Register", pady = 5, padx = 20, command = register)
self.register.place(x=100, y=150)
def run(self):
self.window.mainloop()
def login():
loginTk = Login()
loginTk.run()
def register():
registerTk = Register()
registerTk.run()
window = Window()
window.run()
The program is stored in 3 different files (the login and register classes are stored within the same file.)
As you can see, I can store passwords here.
I have to add a feature that I can register a new user. How do I do it so that I can create new instances when creating a new user?
import datetime
import getpass
class User:
def __init__(self, username, mail, date_of_birth, gender, password):
self.username = username
self.mail = mail
self.date_of_birth = datetime.datetime.strptime(date_of_birth, "%d.%m.%Y").date()
self.gender = gender
self.password = password
def get_username(self):
return self.username
def get_mail(self):
return self.mail
def get_date_of_birth(self):
return self.date_of_birth
def get_gender(self):
return self.gender
def get_password(self):
self.password
def get_date(self):
return self.date_of_birth
log = ""
def tries_left(self):
max_tries = 3
trie = 0
while trie <= max_tries:
print("wrong password!")
print("tries left " + str(max_tries - trie + 1))
trie +=1
pwrd = input("input password")
if pwrd != self.password:
pass
else:
self.login_or_register()
def login2(self):
global log
log = input("input your username: ")
if log == self.username:
pwrd = input("whats your password?")
if pwrd == self.password:
print("Logged in!")
self.add_password_orCheck_password()
else:
self.tries_left()
else:
print("wrong username!")
self.login2()
def login_or_register(self):
user_input = input("Login or Register: ")
if user_input == "login":
self.login2()
elif user_input == "register":
pass
else:
print("Try again!")
self.login_or_register()
def add_password(self):
global sitepassword
global Site
Site = input("Enter site name: ")
sitepassword = input("Enter password")
self.add_password_orCheck_password()
global passwords
passwords = {Site: sitepassword}
def check_passwords(self):
global passwords
global sitepassword
global Site
passwords = {Site: sitepassword}
which_password = input("Which sites password do you want to see?")
for Site in passwords:
if which_password in passwords:
print(sitepassword)
self.add_password_orCheck_password()
def add_password_orCheck_password(self):
add_new = input("Do you want to add a password or check passwords?")
if "add a password" in add_new:
self.add_password()
elif "check passwords" in add_new:
self.check_passwords()
Matt = User("Matterson", "matt#gmail.com", "21.12.1999" ,"male", "Password987")
session1 = Matt.login_or_register()
i wasnt thinking here is a better option:
users = {}
while True:
name = input('yourtexthere')
email = input('yourtexthere')
year = input('yourtexthere')
gender = input('yourtexthere')
password = input('yourtexthere') # here you will use getpass
users[f'{name}'] = User(name, email, year, gender, password)
however you can also store them in list but then you will have to access them by index and you may want to use the email as a key:
users[f'{email}'] = User(name, email, year, gender, password)
here is the code (refer to my comment) its not the cleanest one but it works, here you can try implementing your User class and making it as you need however you need experience in tkinter (this is just an example) hope it helps (it also creates the json file (if it is not already created) in this codes directory) (and I hope this aint against some guidlines):
from tkinter import *
from tkinter import messagebox
import json
all_user_pass = dict()
try:
with open('stored_data.json') as file:
data = json.load(file)
except:
with open('stored_data.json', 'w') as file:
json.dump(all_user_pass, file, indent=2)
with open('stored_data.json') as file:
data = json.load(file)
class MainWindow(Toplevel):
def __init__(self, parent):
self.parent = parent
Toplevel.__init__(self, parent)
self.x_tk = 310
self.y_tk = 100
self.win_h = int(self.winfo_screenheight()/2 - self.y_tk/2)
self.win_w = int(self.winfo_screenwidth()/2 - self.x_tk/2)
self.geometry(f"{str(self.x_tk)}x{str(self.y_tk)}+{str(self.win_w)}+{str(self.win_h)}")
self.title('Login')
self.protocol('WM_DELETE_WINDOW', self.quit)
self.username = Entry(self, bg="grey", bd=5, font="none 12 normal")
word_username = Label(self, text='Username:')
word_username.grid(row=0, column=0, sticky='w')
self.username.grid(row=0, column=1)
self.password = Entry(self, bg="grey", bd=5, font="none 12 normal")
word_password = Label(self, text='Password:')
word_password.grid(row=1, column=0, sticky='w')
self.password.grid(row=1, column=1)
login_button = Button(self, text='Login', command=self.login)
login_button.grid(row=2, column=0)
self.bind('<Return>', self.login)
signup_button = Button(self, text='Sign Up', command=self.open_signup)
signup_button.grid(row=2, column=2)
self.incorrect_user_or_pass = Label(self, text='Incorrect username or password!')
self.empty_user_or_pass = Label(self, text='Cannot be blank!')
def open_signup(self):
signupwindow = SignUpWindow(self)
signupwindow.focus_force()
signupwindow.withdraw
def login(self, event=None):
global data
existing_user = self.username.get()
user_pass = self.password.get()
self.incorrect_user_or_pass.place_forget()
if existing_user != '' and user_pass != '':
if existing_user in data and data[existing_user] == user_pass:
messagebox.showinfo('Successful Login', 'You have logged in successfully!', parent=self)
self.username.delete(0, 'end')
self.password.delete(0, 'end')
self.empty_user_or_pass.place_forget()
self.incorrect_user_or_pass.place_forget()
else:
self.username.delete(0, 'end')
self.password.delete(0, 'end')
self.empty_user_or_pass.place_forget()
self.incorrect_user_or_pass.place(x=60, y=65)
else:
self.empty_user_or_pass.place(x=90, y=65)
class SignUpWindow(Toplevel):
def __init__(self, parent):
self.parent = parent
Toplevel.__init__(self, parent)
self.x_tk = 310
self.y_tk = 100
self.win_h = int(self.parent.winfo_rooty() - 31)
self.win_w = int(self.parent.winfo_rootx() - 8)
self.geometry(f"{str(self.x_tk + 16)}x{str(self.y_tk + 39)}+{str(self.win_w)}+{str(self.win_h)}")
self.title('Signup')
self.bind('<FocusOut>', self.f_out_main)
self.bind('<Escape>', self.close)
self.update_idletasks()
self.overrideredirect(True)
self.config(bg='green')
Label(self, text='Signup', font='none 20 normal', bg='green', bd=5) .grid(row=0, column=0, columnspan=4)
self.username = Entry(self, bg="grey", bd=5, font="none 12 normal")
word_username = Label(self, text='Username:', bg='green')
word_username.grid(row=1, column=0, sticky='w')
self.username.grid(row=1, column=1)
self.username.bind('<FocusOut>', self.f_out)
self.password = Entry(self, bg="grey", bd=5, font="none 12 normal")
word_password = Label(self, text='Password:', bg='green')
word_password.grid(row=2, column=0, sticky='w')
self.password.grid(row=2, column=1)
self.password.bind('<FocusOut>', self.f_out)
cancel_button = Button(self, text='Cancel', command=self.cancel)
cancel_button.grid(row=3, column=0)
cancel_button.bind('<FocusOut>', self.f_out)
cancel_button.bind('<Return>', self.cancel)
signup_button = Button(self, text='Sign Up', command=self.signup)
signup_button.grid(row=3, column=2)
signup_button.bind('<FocusOut>', self.f_out)
self.bind('<Return>', self.signup)
self.user_taken = Label(self, text='Username already taken!', bg='green')
self.empty_user_or_pass = Label(self, text='Cannot be blank!', bg='green')
self.focus_list = []
def f_out(self, event=None):
self.focus_list.append('focus_out_of_widget')
def f_out_main(self, event=None):
self.focus_list.append('focus_out_of_window')
if len(self.focus_list) >= 2:
if self.focus_list[-2] == 'focus_out_of_window' and self.focus_list[-1] == 'focus_out_of_window':
self.close()
elif self.focus_list[0] == 'focus_out_of_window':
self.close()
def cancel(self, event=None):
self.destroy()
def signup(self, event=None):
global all_user_pass
global data
new_username = self.username.get()
new_password = self.password.get()
if new_username != '' and new_password != '':
if new_username not in data:
if new_username not in all_user_pass:
all_user_pass[new_username] = new_password
data.update(all_user_pass)
with open('stored_data.json', 'w') as f:
json.dump(data, f, indent=2)
self.username.delete(0, 'end')
self.password.delete(0, 'end')
self.user_taken.place_forget()
self.empty_user_or_pass.place_forget()
self.destroy()
else:
self.username.delete(0, 'end')
self.password.delete(0, 'end')
self.empty_user_or_pass.place_forget()
self.user_taken.place(x=90, y=110)
else:
self.username.delete(0, 'end')
self.password.delete(0, 'end')
self.empty_user_or_pass.place_forget()
self.user_taken.place(x=90, y=110)
else:
self.empty_user_or_pass.place(x=90, y=110)
def close(self, event=None):
self.parent.focus_set()
self.destroy()
root = Tk()
root.withdraw()
app = MainWindow(root)
app.mainloop()
I want a placeholder entry that is done by the below code. but I need the input password which would be hidden from the user (which may be in the form of asterisks.)
from tkinter import *
root=Tk()
root.geometry("300x200+600+250")
root.config(background="#E0FFFF")
root.resizable(False,False)
def userText(event):
e1.delete(0,END)
usercheck=True
def passText(event):
e2.delete(0, END)
passcheck=True
a=StringVar()
b=StringVar()
usercheck=False
passcheck=False
Label(root,text="User name",bg="#E0FFFF").place(x=20,y=50)
e1= Entry(root,textvariable=a)
e1.place(x=100,y=50)
e1.insert(0,"Enter username")
e1.bind("<Button>",userText)
Label(root,text="Password",bg="#E0FFFF").place(x=20,y=95)
e2= Entry(root,textvariable=b)
e2.place(x=100,y=95)
e2.insert(0,"Enter password")
e2.bind("<Button>",passText)
root.mainloop()
Extended tkinter.Entry showing placeholder text and * on a password entry.
Live-Demo
import tkinter as tk
class Entry(tk.Entry):
def __init__(self, master, placeholder):
super().__init__(master)
self.placeholder = placeholder
self._is_password = True if placeholder == "password" else False
self.bind("<FocusIn>", self.on_focus_in)
self.bind("<FocusOut>", self.on_focus_out)
self._state = 'placeholder'
self.insert(0, self.placeholder)
def on_focus_in(self, event):
if self._is_password:
self.configure(show='*')
if self._state == 'placeholder':
self._state = ''
self.delete('0', 'end')
def on_focus_out(self, event):
if not self.get():
if self._is_password:
self.configure(show='')
self._state = 'placeholder'
self.insert(0, self.placeholder)
Usage:
if __name__ == "__main__":
root = tk.Tk()
username = Entry(root, "username")
username.pack()
password = Entry(root, "password")
password.pack()
root.mainloop()
to hide the password use:
e2.config(show='*')
I I have 2 functions and the second function should run after the button in the first one has been clicked. This works fine, however I need the number that has been entered to go into a variable and so far the .get() function is not working and im not sure what to do.
I have looked at a lot of different examples, including login and sign up gui's however none of them have been able to help.
from tkinter import *
import tkinter.messagebox as box
def enter_rle_1():
enter_rle = Tk() #do not remove
enter_rle.title('Enter RLE') #do not remove
frame = Frame(enter_rle) #do not remove
label_linecount = Label(enter_rle,text = 'Linecount:')
label_linecount.pack(padx=15,pady= 5)
linecount = Entry(enter_rle,bd =5)
linecount.pack(padx=15, pady=5)
ok_button = Button(enter_rle, text="Next", command = linecount_button_clicked)
ok_button.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
enter_rle.mainloop()
def linecount_button_clicked():
Linecount = linecount.get()
if int(Linecount) < 3:
tm.showinfo("Error", "Enter a number over 3")
elif int(Linecount) > 1000000000:
tm.showinfo("Error", "Enter a number under 1,000,000,000")
else:
print("fdsfd")
enter_rle_1()
I expect there to be a popup telling me the number is too big or too small, depending on wether the number is or not, and if it is a good number, i just have it set as a print as some code to test to see if it works before i move on.
Define a String variable for the Entry widget (Make sure it is global defined):
global linecount_STR
linecount_STR = StringVar()
linecount_STR.set('Enter value here')
linecount = Entry(enter_rle,bd =5,textvariable=linecount_STR)
linecount.pack(padx=15, pady=5)
The number entered there can then be read with int(linecount_STR.get()).
i suggest an OO approach, look this code
#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class Main(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.vcmd = (self.register(self.validate), '%d', '%P', '%S')
self.linecount = tk.IntVar()
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1)
f = ttk.Frame()
ttk.Label(f, text = "Linecount").pack()
self.txTest = ttk.Entry(f,
validate='key',
validatecommand=self.vcmd,
textvariable=self.linecount).pack()
w = ttk.Frame()
ttk.Button(w, text="Next", command=self.on_callback).pack()
ttk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)
def on_callback(self,):
#print ("self.text = {}".format(self.linecount.get()))
x = self.linecount.get()
if x < 3:
msg = "Enter a number over 3"
elif x > 1000000000:
msg = "Enter a number under 1,000,000,000"
else:
msg = "You ahve enter {0}".format(x)
messagebox.showinfo(self.parent.title(), msg, parent=self)
def on_close(self):
self.parent.on_exit()
def validate(self, action, value, text,):
# action=1 -> insert
if action == '1':
if text in '0123456789':
try:
int(value)
return True
except ValueError:
return False
else:
return False
else:
return True
class App(tk.Tk):
"""Start here"""
def __init__(self):
super().__init__()
self.protocol("WM_DELETE_WINDOW", self.on_exit)
self.set_title()
self.set_style()
frame = Main(self,)
frame.pack(fill=tk.BOTH, expand=1)
def set_style(self):
self.style = ttk.Style()
#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
self.style.theme_use("clam")
def set_title(self):
s = "{0}".format('Enter RLE')
self.title(s)
def on_exit(self):
"""Close all"""
if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
self.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
notice this line at the begin....
self.linecount = tk.IntVar()# declare an integer variable
self.vcmd = (self.register(self.validate), '%d', '%P', '%S')#register a function to allow only integer in your text widget.
so, i opened a question about it yesterday but it got closed saying its a dupe, let me explain my question more specificly then, hoping im not passinng any law here..
im trying to set a default message that will be set as gray and low opacity in the entry widget on tkinter, that after being focused in will dispear.
i managed to do that, but i got into a problem.
the default text is "enter username" for example, but if someone tries to do this user name it says that the username entry is empty which isnt what i wanna do..
i hope you understood my question.
edit: my question is how to do a placeholder text, while allowing the user to input the same text as the placeholder.
thanks in advance, this is my code:
from tkinter import *
from tkinter import messagebox
main = Tk()
main.title("ani gever")
checkbox_var = IntVar()
def delete_user_default(event):
if User_entry.get()!="enter username":
pass
else:
User_entry.delete(0, END)
def delete_password_default(event):
if password_entry.get() !="insert password":
pass
else:
password_entry.delete(0, END)
password_entry.config(show="*")
def return_user_default(event):
if User_entry.get()=="":
User_entry.insert(0, "enter username")
emptyuser=1
else:
pass
def return_password_default(event):
if password_entry.get()=="":
password_entry.insert(0,"insert password")
password_entry.config(show="")
else:
pass
def details(event):
if ((User_entry.get() == "enter username") or (password_entry.get() == "insert password")):
errorLabel.config(text="Username or password is empty!",fg="red")
elif ((" " in User_entry.get()) or " " in password_entry.get()):
errorLabel.config(text="dont use spaces in your password or username!", fg="red")
else:
print ("username:" + User_entry.get() + "\npassword:" + password_entry.get())
errorLabel.config(text="")
#============== define texts=======================
User_label= Label(main,text="Username:")
password_label= Label(main,text="Password:")
User_entry = Entry(main)
password_entry= Entry(main,show="*")
enterButton= Button(main,text="Log in")
errorLabel= Label(main,text="")
#=============default text on entry's=============
password_entry.config(show="")
User_entry.insert(0,"enter username")
password_entry.insert(0,"insert password")
User_entry.bind("<FocusIn>",delete_user_default)
password_entry.bind("<FocusIn>",delete_password_default)
User_entry.bind("<FocusOut>",return_user_default)
password_entry.bind("<FocusOut>",return_password_default)
#=============return user details ===========
User_entry.bind("<Return>",details)
password_entry.bind("<Return>",details)
enterButton.bind("<Button-1>",details)
#=============place everything on screen===========
User_label.grid(row=0,sticky= W)
User_entry.grid(row=0,column=1,sticky= W)
password_label.grid(row=1,sticky= W)
password_entry.grid(row=1,column=1,sticky= W)
enterButton.grid(row=2,sticky=W)
errorLabel.grid(row=3,columnspan=10,sticky= W)
main.mainloop()
Using this answer an is_empty function can be created. It prints True if validated entry is 'empty' and False if not.
import tkinter as tk
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()
def is_empty(widget, empty_color):
if widget.get():
if widget['fg'] == empty_color:
print (True)
else:
print (False)
else:
print (True)
if __name__ == "__main__":
root = tk.Tk()
username = EntryWithPlaceholder(root, "username")
password = EntryWithPlaceholder(root, "password", 'blue')
username.pack()
password.pack()
tk.Button(root, text="Validate",
command=lambda wid=username, clr=username.placeholder_color:
is_empty(wid, clr)).pack()
root.mainloop()