簡體   English   中英

NameError:未定義名稱“username_entry”

[英]NameError: name 'username_entry' is not defined

所以我正在嘗試使用 customtkinter 進行登錄 gui 我想要一個帶有 2 個按鈕的 window 首先:登錄和退出然后當我按登錄打開另一個 py 腳本時登錄 label 如果我執行第二個腳本它沒問題但是如果我從第一個開始嘗試,我會收到此錯誤

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

這是第一個代碼:

`

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()

`

這是登錄代碼:

`

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()

`

試圖做一個登錄框並得到這個錯誤。 不知道怎么解決

顯然,登錄function body中沒有定義username_entry 請將其添加到 function arguments 然后正確使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM