首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError:未定义名称“username_entry”

NameError:未定义名称“username_entry”
EN

Stack Overflow用户
提问于 2022-12-03 11:23:35
回答 1查看 32关注 0票数 0

因此,我尝试使用定制工具进行登录gui,我希望首先有一个带有2个按钮的窗口:登录和退出,然后按login打开另一个带有登录标签的py脚本,如果我执行第二个脚本,它还行,但是如果我尝试从第一个脚本开始,我会得到这个错误。

代码语言:javascript
复制
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

这是第一个代码:

`

代码语言:javascript
复制
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()

`

这是登录代码:

`

代码语言:javascript
复制
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()

`

尝试执行登录框并获得此错误。如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2022-12-03 11:31:49

显然,登录函数主体中没有定义username_entry。请将其添加到函数参数中,然后正确使用它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74666259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档