I’m creating a simple login GUI. I’m trying to make it so that level 3 users can access all requests, level 2 users can only access database and sales related requests, and level 1 users can only access the sale requests.
However, I’m stuck on trying to retrieve the level info and using it in the if- else
statement.
JavaScript
x
10
10
1
def database_request():
2
level=level.get()
3
4
if level.get() >= "2":
5
login_success_screen.destroy()
6
login_success_screen.update()
7
else:
8
Label(login_success_screen, text="Access Denied", fg="gray", bg="white", font=("times new roman", 12)).pack()
9
os.remove(username_info)
10
Full code:
JavaScript
1
187
187
1
import tkinter as tk
2
from tkinter import messagebox
3
import os
4
from PIL import Image, ImageTk
5
6
class Window(Frame):
7
def __init__(self,root):
8
Frame.__init__(self,root)
9
self.root=root
10
self.pack(fill=BOTH, expand=1)
11
self.root.wm_title("Login")
12
self.root.geometry("1202x632")
13
self.root.resizable(False,False)
14
15
global username_verify
16
global passcode_verify
17
global level_verify
18
19
username_verify=StringVar()
20
passcode_verify=StringVar()
21
level_verify=StringVar()
22
23
global username_entry
24
global passcode_entry
25
global level_entry
26
27
load=Image.open("books.png")
28
render=ImageTk.PhotoImage(load)
29
img=Label(self, image=render)
30
img.image=render
31
img.place(x=0, y=0)
32
33
Frame_login=Frame(self.root,bg="white",highlightbackground="#c4652b",highlightthickness=5)
34
Frame_login.place(x=150,y=150,height=340,width=500)
35
36
title=Label(Frame_login,text="Login Here", font=("Impact",35,"bold"),fg="#d77337",bg="white").place(x=72,y=20)
37
desc=Label(Frame_login,text="Staff Login Gate", font=("Goudy old style",15,"bold"),fg="#d25d17",bg="white").place(x=72,y=80)
38
39
lbl_user=Label(Frame_login,text="Username", font=("goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=110)
40
self.txt_user=Entry(Frame_login,textvariable=username_verify,font=("times new roman",15),bg="lightgray")
41
self.txt_user.place(x=72,y=140,width=350,height=35)
42
43
lbl_passcode=Label(Frame_login,text="Passcode", font=("Goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=180)
44
self.txt_passcode=Entry(Frame_login,textvariable=passcode_verify,show='*',font=("times new roman",15),bg="lightgray")
45
self.txt_passcode.place(x=72,y=208,width=350,height=35)
46
47
def login_success():
48
global login_success_screen
49
login_success_screen=Toplevel(Frame_login)
50
login_success_screen.title("User Action Gate")
51
login_success_screen.geometry("500x340")
52
login_success_screen.resizable(False,False)
53
login_success_screen.configure(bg="white")
54
login_success_screen.configure(highlightbackground="#c4652b")
55
login_success_screen.configure(highlightthickness=5)
56
result.config(text="")
57
58
title=Label(login_success_screen,text="Login Successful", font=("Impact",35,"bold"),fg="#d77337",bg="white").place(x=72,y=20)
59
desc=Label(login_success_screen,text="User Action Gate", font=("Goudy old style",15,"bold"),fg="#d25d17",bg="white").place(x=72,y=80)
60
61
def sales_request():
62
login_success_screen.destroy()
63
login_success_screen.update()
64
65
def database_request():
66
level=level.get()
67
68
if level.get() >= "2":
69
login_success_screen.destroy()
70
login_success_screen.update()
71
else:
72
Label(login_success_screen, text="Access Denied", fg="gray", bg="white", font=("times new roman", 12)).pack()
73
74
Sales_button=Button(login_success_screen,command=sales_request,text="Request Sales (1)",fg="white",bg="#d77337",bd=0,font=("times new roman",20,"bold")).place(x=72,y=120,width=345)
75
76
Database_button=Button(login_success_screen,command=database_request,text="Request Database (2)",fg="white",bg="#d77337",bd=0,font=("times new roman",20,"bold")).place(x=72,y=185,width=345)
77
78
Staff_button=Button(login_success_screen,text="Request Staff (3)",fg="white",bg="#d77337",bd=0,font=("times new roman",20,"bold")).place(x=72,y=250,width=345)
79
80
def passcode_not_recognised():
81
result.config(text="Login Unsuccessful: Passcode Not Recognised", fg="red")
82
83
def user_not_found():
84
result.config(text="Login Unsuccessful: User Not Found", fg="red")
85
86
result = Label(Frame_login, bg="white", font=("times new roman", 12))
87
result.pack()
88
89
def delete_login_success():
90
login_success_screen.destroy()
91
92
def delete_passcode_not_recognised():
93
passcode_not_recog_screen.destroy()
94
95
def delete_user_not_found_screen():
96
user_not_found_screen.destroy()
97
98
def login_verify():
99
username1=username_verify.get()
100
passcode1=passcode_verify.get()
101
level1=level_verify.get()
102
103
list_of_files=os.listdir()
104
if username1 in list_of_files:
105
file1=open(username1, "r")
106
verify=file1.read().splitlines()
107
if passcode1 in verify:
108
login_success()
109
110
else:passcode_not_recognised()
111
112
else:user_not_found()
113
114
115
def create_registration():
116
top = tk.Toplevel(root)
117
top.title("Registration")
118
top.geometry("500x450")
119
top.resizable(False,False)
120
top.configure(bg="white")
121
top.configure(highlightbackground="#c4652b")
122
top.configure(highlightthickness=5)
123
124
global username
125
global passcode
126
global level
127
global username_entry
128
global passcode_entry
129
global level_entry
130
username=StringVar()
131
passcode=StringVar()
132
133
title=Label(top,text="Register Here", font=("Impact",35,"bold"),fg="#d77337",bg="white").place(x=72,y=20)
134
desc=Label(top,text="User Registration Area", font=("Goudy old style",15,"bold"),fg="#d25d17",bg="white").place(x=72,y=80)
135
136
lbl_user=Label(top,text="Username", font=("goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=110)
137
self.txt_user=Entry(top,textvariable=username,font=("times new roman",15),bg="lightgray")
138
self.txt_user.place(x=72,y=140,width=350,height=35)
139
140
lbl_passcode=Label(top,text="Passcode", font=("Goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=180)
141
self.txt_passcode=Entry(top,textvariable=passcode,show='*',font=("times new roman",15),bg="lightgray")
142
self.txt_passcode.place(x=72,y=208,width=350,height=35)
143
144
lbl_level=Label(top,text="Level", font=("Goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=250)
145
level = StringVar(top)
146
level.set("")
147
148
w = OptionMenu(top, level, "1", "2", "3")
149
w.pack()
150
w.place(x=72,y=280)
151
w.configure(bd=0)
152
153
def register_user():
154
155
username_info = username.get()
156
passcode_info = passcode.get()
157
level_info = level.get()
158
159
if level_info and username_info and passcode_info:
160
if os.path.exists(username_info):
161
result.config(text="Registration Unsuccessful: User Already Exists", fg="red")
162
else:
163
with open(username_info, "w") as f:
164
f.write("n".join([username_info, passcode_info, level_info]))
165
result.config(text="Registration Successful", fg="green")
166
else:
167
result.config(text="Registration Unsuccessful: Please Input All Information", fg="red")
168
169
170
result = Label(top, bg="white", font=("times new roman", 12))
171
result.pack()
172
173
Submit_button=Button(top,command=register_user,text="Submit",fg="white",bg="#d77337",bd=0,font=("times new roman",20,"bold")).place(x=72,y=320,width=180)
174
175
def quit():
176
top.destroy()
177
top.update()
178
179
Back_button=Button(top,command=quit,text="Back to Home",bg="white",fg="#d77337",bd=0,font=("times new roman",12)).place(x=72,y=380)
180
181
Register_button=Button(Frame_login,command=create_registration,text="Register New Account",bg="white",fg="#d77337",bd=0,font=("times new roman",12)).place(x=72,y=248)
182
Login_button=Button(self.root,command=login_verify,text="Login",fg="white",bg="#d77337",bd=0,font=("times new roman",20,"bold")).place(x=310,y=460,width=180)
183
184
root = Tk()
185
app = Window(root)
186
root.mainloop()
187
If possible, please provide some examples.
Advertisement
Answer
You need to save the user level from the user credential file before executing login_success()
inside login_verify()
function:
JavaScript
1
14
14
1
def login_verify():
2
username1 = username_verify.get()
3
passcode1 = passcode_verify.get()
4
if os.path.exists(username1):
5
with open(username1) as file1:
6
credentials = file1.read().splitlines()
7
if passcode1 in credentials:
8
self.user_level = credentials[-1] # save user level
9
login_success()
10
else:
11
passcode_not_recognised()
12
else:
13
user_not_found()
14
Then using this user level in database_request()
function:
JavaScript
1
7
1
def database_request():
2
if self.user_level >= "2":
3
login_success_screen.destroy()
4
login_success_screen.update()
5
else:
6
access.config(text="Access Denied")
7