Reputation: 1
So i'm trying to do a login gui using customtkinter I want to have an window with 2 buttons first : Login and Exit Then when I press Login to open another py script with the login label If i execute the second script its all right but if I try from the first one I get this error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\denis\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "D:\test\venv\Lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 527, in _clicked
self._command()
File "<string>", line 21, in login
NameError: name 'username_entry' is not defined
This is the first code:
`
import tkinter
import customtkinter
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("dark-blue")
app = customtkinter.CTk() # create CTk window like you do with the Tk window
app.title("Menu")
app.geometry("240x240")
app.config(bg="#242320")
def button_function():
exec(open('D:\test\login.py').read())
def Close():
app.destroy()
font1=('Arial', 15, 'bold')
button = customtkinter.CTkButton(master=app, text="Login", font=font1, command=button_function)
button.place(relx=0.5, rely=0.4, anchor=tkinter.CENTER)
button = customtkinter.CTkButton(master=app, text="Exit", font=font1, command=Close)
button.place(relx=0.5, rely=0.6, anchor=tkinter.CENTER)
app.mainloop()
`
and this is the login code:
`
import customtkinter
from tkinter import *
from tkinter import messagebox
app = customtkinter.CTk()
app.title("Login")
app.geometry("350x200")
app.config(bg="#242320")
font1=('Arial', 15, 'bold')
username="hello"
password="123"
trials=0
def login():
global username
global password
global trials
written_username = username_entry.get()
written_password = password_entry.get()
if(written_username == '' or written_password==''):
messagebox.showwarning(title="Error", message="Enter your username and password.")
elif(written_username==username and written_password==password):
new_window=Toplevel(app)
new_window.geometry("350x200")
new_window.config(bg="#242320")
welcome_label=customtkinter.CTkLabel(new_window, text="Welcome...", font=font1, text_color="#FFFFFF")
welcome_label.place(x=100, y=100)
elif((written_username != username or written_password != password) and trials<3):
messagebox.showerror(title="Error", message="Your username or password are not correct.")
trials=trials + 1
if (trials != 3):
trials_label = customtkinter.CTkLabel(app, text=f"You have {3-trials} trials", font=font1, text_color="#FFFFFF")
trials_label.place(x=100, y=160)
if(trials==3):
login_button.destroy()
locked_label = customtkinter.CTkLabel(app, text="Your account is locked.", font=font1, text_color="#FFFFFF")
locked_label.place(x=100, y=160)
username_label=customtkinter.CTkLabel(app, text="Username: ",font=font1, text_color="#FFFFFF")
username_label.place(x=10, y=25)
password_label=customtkinter.CTkLabel(app, text="Password: ",font=font1, text_color="#FFFFFF")
password_label.place(x=10, y=75)
username_entry=customtkinter.CTkEntry(app,fg_color="#FFFFFF", font=font1, text_color="#000000", border_color="#FFFFFF", width= 200, height= 1)
username_entry.place(x=100, y=25)
password_entry=customtkinter.CTkEntry(app,fg_color="#FFFFFF", font=font1, text_color="#000000", border_color="#FFFFFF", show="*", width= 200, height= 1)
password_entry.place(x=100, y=75)
login_button=customtkinter.CTkButton(app, command=login, text="Login", font=font1, text_color="#FFFFFF", fg_color="#1f538d", hover_color="#14375e", width=50)
login_button.place(x=165, y=120)
app.mainloop()
`
Tried to do a login box and got this error. Idk how to resolve it
Upvotes: 0
Views: 562
Reputation: 98
Obviously, the username_entry
is not defined in the login function body. please add it to the function arguments and then use it properly.
Upvotes: 1