Error clicking the Tkinter Python button

Asked

Viewed 34 times

0

Hello I am developing a python application using Tkinter but I don’t know why it is giving me error. I was inspired by an example. My code.

from tkinter import *
win = Tk()

win.geometry("400x150")

class Application:
def __init__(self, master=None):

    self.title = Label(win,text="Please enter the username and password",font=('Helvetica',13))
    self.title.pack()

    self.suc = Label(win,font=('Helvetica',10))
    self.suc.pack()
    
    self.username = Entry(win,width=20)
    self.username.pack()

    self.password = Entry(win,show="*",width=20)
    self.password.pack()

    self.btnConfirm = Button(win, text="Enter",font=('Helvetica bold',10),command=self.enter)
    self.btnConfirm.pack()

    self.btnQuit = Button(win, text="Quit",font=('Helvetica bold',10),command=self.exit)
    self.btnQuit.pack()

def exit(self):
   win.destroy()

def enter(self):
    if username=="admin" and password=="123":
        self.suc["text"] = "Sucess"
    else:
        self.suc["text"] = "Error"

Application(win)
win.mainloop()

The error that happens when clicking the Enter button:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\ESFA\Desktop\Project Python\index1.py", line 29, in enter
    if username=="admin" and password=="123":
NameError: name 'username' is not defined

   
  • forgot self in front of username and password

  • I’ll try that, thanks

  • in its code also lacked to use Stringvar, this is thing of Tkinter, of a searched

  • ok thank you for informing

3 answers

0


EDIT

You need to also add self._<atributo> = StringVar() in its builder to username and password, Elton Nunes' answer has the full code.

In function def_enter(self) you call the objects username and password, but they were not previously declared in the function.

There are two options in a situation like this:

  • rewrite the function and pass the parameters def_enter(self, username, password)

  • call constructor method attributes(__init__) of your class: if self._username.get()=="admin" and self._password.get()=="123".

When using the second option Tkinter will read the inputs and make the logical comparison correctly.

  • Thank you, I’ll try this way

  • I edited the answer, Elton Nunes showed what probably leads to the problem.

0

I tried to follow the directions but it generates another error.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: enter() missing 2 required positional arguments: 'username' and 'password'

0

from tkinter import *
win = Tk()

win.geometry("400x150")

class Application:
    def __init__(self, master=None):

        self.title = Label(master,text="Please enter the username and password",font=('Helvetica',13))
        self.title.pack()

        self.suc = Label(master,font=('Helvetica',10))
        self.suc.pack()
        
        self.username_var = StringVar()
        self.username = Entry(master,width=20, textvariable=self.username_var)
        self.username.pack()

        self.password_var = StringVar()
        self.password = Entry(master,show="*",width=20, textvariable=self.password_var)
        self.password.pack()

        self.btnConfirm = Button(master, text="Enter",font=('Helvetica bold',10),command=self.enter)
        self.btnConfirm.pack()

        self.btnQuit = Button(master, text="Quit",font=('Helvetica bold',10),command=self.exit)
        self.btnQuit.pack()

    def exit(self):
       win.destroy()

    def enter(self):
        if self.username_var.get() =="admin" and self.password_var.get() =="123":
            self.suc["text"] = "Sucess"
        else:
            self.suc["text"] = "Error"

Application(win)
win.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.