1

Hey I'm trying to get a variable from a class but for some reason its not coming through. its probably really simple but I really cant think of anything

Here is the code its not done yet I've only made a login screen:

from tkinter import *
import tkinter.messagebox as tm
correct = False
#-------------Functions-----------------------------------------------------

class LoginMenu(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_2 = Label(self, text="Welcome to the rota system")
        self.label_3 = Label(self, text="Please enter the password to continue:")
        self.label_1 = Label(self, text="Password")

        self.entry_1 = Entry(self)

        self.label_1.grid(row=3, sticky=W)
        self.label_2.grid(row=1, sticky=W)
        self.label_3.grid(row=2, sticky=W)
        self.entry_1.grid(row=3, sticky=W)


        self.logbtn = Button(self, text="Login", command = self.login_btn_clicked)
        self.logbtn.grid(columnspan=2)

        self.pack()

    def login_btn_clicked(self):

        password = self.entry_1.get()

        if password == "1234":
            correct = True

        else:
            tm.showerror("Login error", "Incorrect password")
        return correct



#-----------------Main-Program----------------------------------------------

window = Tk()
LoginMenu(window)
if correct == True:
    print("Yay")
    LoginMenu.self.destroy()
window.mainloop()
1
  • correct variable is never declared when the password is incorrect, so it can not be return. Even if it is declared it will only exist in the scope of the class method. It is not a global variable and you should not use one inside a class. Commented Dec 14, 2015 at 12:09

2 Answers 2

3

Your variable within the class only has a local scope.

A good way would be to define the variable correct as a class member:

class LoginMenu(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.correct = False

and then set it in your function:

def login_btn_clicked(self):

        password = self.entry_1.get()

        if password == "1234":
            self.correct = True

Your could access it from the global scope via (no need for == True, btw)

loginmenu = LoginMenu(window)
if loginmenu.correct:

The problem is though that this won't work in your case. You enter your main loop after your if construct. Please have a look at the Tkinter docs how to properly structure a Tkinter app.

Sign up to request clarification or add additional context in comments.

Comments

1

To reference a global variable inside a local scope you have to define that variable inside the class like this:

global correct

(inside the function login_btn_clicked)

1 Comment

Although this is technically correct, global variables are not very pythonic :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.