0

This is my python login system I have attempted to built when I run the check on the user name and password I receive this error : http://pastebin.com/0DPAWx18

I was wondering if it because it is in a another function but I then put it in main as well and that just gave me errors

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
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
Nick6565
  • 3
  • 2

1 Answers1

0

That's is because you define userinp in the main function's scope, so it isn't defined the check function. You could make userinp and userpw global, or you could make your app into a class like this, which makes variable passing a lot easier through self.

The __init__ function is called when the class is called, so that can be used as 'main'. I've put pretty much everything in self, which isn't necessary, but can be useful if you'd want to change any of the created widgets further on in a new function. The update() function is needed to pack the labels before sleeping.

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

For more info on making your app into a class read this and this question.

Community
  • 1
  • 1
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62