Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x python中的Tkinter gui登录系统使用.get()获取未定义的错误_Python 3.x - Fatal编程技术网

Python 3.x python中的Tkinter gui登录系统使用.get()获取未定义的错误

Python 3.x python中的Tkinter gui登录系统使用.get()获取未定义的错误,python-3.x,Python 3.x,这是我在检查用户名和密码时尝试构建的python登录系统,我收到以下错误: 我想知道这是否是因为它在另一个函数中,但我把它也放在main中,这只会给我带来错误 import tkinter import time def main(): global window window = tkinter.Tk() window.title("Login") window.minsize(300,150) window.configure(background=

这是我在检查用户名和密码时尝试构建的python登录系统,我收到以下错误:

我想知道这是否是因为它在另一个函数中,但我把它也放在main中,这只会给我带来错误

import tkinter
import time

def main():
    global window
    window = tkinter.Tk()
    window.title("Login")
    window.minsize(300,150)
    window.configure(background="#7AC5CD")
    userlbl = tkinter.Label(window, text="Username")
    userlbl.pack()
    userinp = tkinter.Entry(window)
    userinp.pack()
    pwlbl = tkinter.Label(window, text="Password")
    pwlbl.pack()
    userpw = tkinter.Entry(window)
    userpw.pack()
    submitbtn = tkinter.Button(text="Submit username and password here", command=check)
    submitbtn.pack()

def check():
    username = userinp.get()
    password = userpw.get()    
    if username == 1234:
        GD_USER = tkinter.Label(window, text="Correct user name")
        GD_USER.pack()
    else:
        BD_USER = tkinter.Label(window, text="Bad username")
        BD_USER.pack()

    if password == 'test':
        GD_PASS = tkinter.Label(window, text="Correct password")
        GD_PASS.pack()
        entry_YES()
        return
    else:
        BD_PASS = tkinter.Label(window, text="wrong password")

    window.mainloop()


def entry_NO():
    print("access denied")
    time.sleep(5)
    close_window
    return



def entry_YES():
    print("Access granted please wait")


def close_window():
    window.destry()
    enter code here

这是因为您在
main
函数的作用域中定义了
userinp
,因此它没有定义为
check
函数。您可以将
userinp
userpw
设置为全局,也可以将您的应用程序设置为这样的类,这使得变量通过self传递更加容易

调用类时会调用
\uuuuu init\uuuu
函数,因此可以将其用作“main”。 我已经在
self
中添加了几乎所有内容,这不是必需的,但是如果您想在新函数中进一步更改任何已创建的小部件,这将非常有用。 睡觉前需要使用
update()
函数打包标签

import tkinter as tk
import time

class App():

    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Login")
        self.window.minsize(300,150)
        self.window.configure(background="#7AC5CD")
        self.userlbl = tk.Label(self.window, text="Username")
        self.userlbl.pack()
        self.userinp = tk.Entry(self.window)
        self.userinp.pack()
        self.pwlbl = tk.Label(self.window, text="Password")
        self.pwlbl.pack()
        self.userpw = tk.Entry(self.window)
        self.userpw.pack()
        self.submitbtn = tk.Button(text="Submit username and password here", command=self.check)
        self.submitbtn.pack()
        self.window.mainloop()

    def check(self):
        self.username = self.userinp.get()
        self.password = self.userpw.get()    
        if self.username == '1234':
            self.GD_USER = tk.Label(self.window, text="Correct user name")
            self.GD_USER.pack()
        else:
            self.BD_USER = tk.Label(self.window, text="Bad username")
            self.BD_USER.pack()

        if self.password == 'test':
            self.GD_PASS = tk.Label(self.window, text="Correct password")
            self.GD_PASS.pack()
            self.entry_YES()
        else:
            self.BD_PASS = tk.Label(self.window, text="Wrong password")
            self.BD_PASS.pack()
            self.window.update()
            self.entry_NO()

    def entry_NO(self):
        print("Access denied, wait 5 seconds to try again")
        time.sleep(5)

    def entry_YES(self):
        print("Access granted please wait")

    def close_window(self):
        window.destroy()

App()

有关将应用程序变成课堂的更多信息,请阅读并提问。

非常感谢您的回复,证明非常有用