0

I need help trying to call a variable that is in two different functions(I think). I am new to python but I'm trying to learn, just started today and I am stuck on this, I've searched through the web and can't find anything on it.

def newAccount(): #New account window
    create = Tk()
    create.title("New Account")
    create.minsize(width=200, height=150)
    create.maxsize(width=400, height=300)

    L1 = Label(create, text= "New User:").grid(column= 1, row= 1)
    E1 = Entry(create, bd= 3)
    E1.grid(column= 1, row= 2)

    L2 = Label(create, text="Password:").grid(column=1, row=3)
    E2 = Entry(create, bd=3, show="*")
    E2.grid(column=1, row=5)

    L3 = Label(create, text="Comfirmation").grid(column=1, row=6)
    E3 = Entry(create, bd=3, show="*")
    E3.grid(column=1, row=7)

def createNew():
    User = E1.get()
    Pass = E2.get()

    if(Pass == E3.get()):
        Pass == Pass
        create.destroy()
    else:
        O1 = Message(create, text= "Passwords don't match.", fg= "red", width= 250).grid(column= 1, row= 10)

        E2.delete(0, END)
        E3.delete(0, END)

    return User, Pass

def quit():
    create.destroy()

B1 = Button(create, text="Create", command= createNew).grid(column= 1, row= 8)
create.close = Button(create, text="Quit", command= quit).grid(column=1, row=9)

def callback():
    User = [Username]
    Pass = [Password]

    if(User == User):
        if(Pass == Pass):
            print(".")

I am trying to use the user and password i got from 'def createNew()' and use it in 'def callback()'

DYZ
  • 55,249
  • 10
  • 64
  • 93
Kregg
  • 3
  • 2
  • 2
    Just curious, what is `Pass == Pass` supposed to mean? As for your question, either use global variables (bad) or create a class and store the user name and password in the instance variables. – DYZ Feb 12 '18 at 07:25
  • inside of createNew? I honestly don't know :| lol, but thank you let me try – Kregg Feb 12 '18 at 07:26
  • Yes, that one.... – DYZ Feb 12 '18 at 07:27
  • You have multiple instances of things like `User == User` and `Pass == Pass`. You realize these are just comparisons that evaluate to `True` right? – Tom Karzes Feb 12 '18 at 07:27
  • Yes I understand that but the one in createNew, i honestly don't know why that one was there, but then again I'm new to python so there is probably a 10x easier way of trying to make what I'm making – Kregg Feb 12 '18 at 07:31
  • this may help you understand global variable https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – louay_075 Feb 12 '18 at 07:56

2 Answers2

0

The easiest and most efficient solution to 'sharing variables' across functions would be to program using an Object Orientated approach (OOP).

  1. For a more in depth account of OOP check out: https://www.youtube.com/playlist?list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc
  2. For a good guide on creating Tkinter applications using an OOP approach check: https://www.youtube.com/playlist?list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk

But in brief OOP uses classes to create objects. Each class has attributes (variables, features associated with that object) and methods (functions used to 'do things' using those attributes).

The main feature that OOP will provide in your scenario is INHERITANCE. Inheritance allows functions withing a class to share variables but it also allows classes to inherit attributes from other classes.

Looking at your code i assume you are trying to create a login feature. Using your example to get the value of E1 use the code below

CODE:

Make your Account function into a class

class Account(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

    'enter your code here e.g your labels and entry widgets'

# when declaring E1 in your class you have to add self.

self.E1 = Entry(self)
self.E1.grid(blah blah)

when getting entry value in another function pass argument self and call ##using self.

def createNew(self):
        User = self.E1.get()

Complete small interface example:

from tkinter import *        

class Create:
    def __init__(self, master):

        frame = Frame(master)
        frame.pack()


        userInput = Label(frame, text="Enter text to pass to another funtion:")
        userInput.grid(row=1, column=0, sticky="w")

        self.userEntry=Entry(frame)
        self.userEntry.grid(row=1, column=1, sticky="w")

        submit = Button(frame, text="submit", command=self.getVariable)
        submit.grid(row=3,columnspan=2)



    def getVariable(self):
        text = self.userEntry.get()
        print(text)

root = Tk()
app = Create(root)
root.mainloop()   

to pass variables you have to declare the variable as self.variableName and then pass self as an argument in the function you want to call that variable to and then just reference it.

I urge you to watch the Sentdex videos i linked at the top he explains this in detail and shows you how to implement it.

Good Luck and Have Fun coding!

Moller Rodrigues
  • 780
  • 5
  • 16
0

You need to declare the variable outside of any functions.

variable_name = "" # Whatever

To use it in a function just pass it as a parameter.

function_name(variable_name)

to change the variable from within a function you need to declare it as "global"

def change_variable():

    global variable_name
    variable_name = "Hi"

Like this

def change_variable():
    global var
    var = "Hello"

def process_variable(x):
    print(var)

var = ""               # declare the variable or list or whatever
change_variable()      # assign new values or amend existing ones
process_variable(var)  # use the variables values in another function
Neil
  • 706
  • 8
  • 23