Ramazan
Ramazan

Reputation: 1

Tkinter trying write input from file to .txt

im trying get an input using entry and then write it to a .txt file but i get this error - Traceback (most recent call last): File "C:/Users/User/Desktop/Register.py", line 52, in fout.write(U + '\n') NameError: name 'U' is not defined

from Tkinter import *
class Register:
    def __init__(self, parent):
        top = self.top = Toplevel(parent)

        VarEntUser = StringVar()
        VarEntPass = StringVar()
        VarEntRetype = StringVar()

        self.Label1 = Label(top, text = "What is your username: ")
        self.Label2 = Label(top, text = "Enter a password: ")
        self.Label3 = Label(top, text = "Retype Password: ")
        self.EntUser = Entry(top, textvariable = VarEntUser )
        self.EntPass = Entry(top, textvariable = VarEntPass)
        self.EntRetype = Entry(top, textvariable = VarEntRetype)


        self.Label1.grid(row = 0, sticky = W)
        self.Label2.grid(row = 1, sticky = W)
        self.Label3.grid(row = 2, sticky = W)
        self.EntUser.grid(row = 0, column = 1)
        self.EntPass.grid(row = 1, column = 1)
        self.EntRetype.grid(row = 2, column = 1)

        U = raw_input(self.VarEntUser.get())
        P = raw_input(self.VarEntPass.get())
        R = raw_input(self.VarEntRetype.get())

        self.MySubmitButton = Button(top, text = 'Submit', command = self.send)
        self.MySubmitButton.grid(row = 3, sticky = E)





    # Checks the password and checks if all fields have been entered
    def send(self):
        if len(P) <= 0 and len(U) <= 0:
                    print "Please fill out all fields."

        else:
            pass
        if P == R:
            pass

        else:
             print "Passwords do not match"

        with open('username.txt', 'a') as fout:
                 fout.write(U + '\n')
        with open('password.txt', 'a') as fout:
                 fout.write(P + '\n')




    # opens a new the registration window       
    def Register():
        inputDialog = Register(root)
        root.wait_window(inputDialog.top)

root = Tk()

Lable = Label(root, text = 'Choose an option')

LoginB = Button(root, text = 'Log In', comman = LogIn) LoginB.pack()

RegisterB = Button(root, text = 'Register', command = Register) RegisterB.pack()

root.mainloop()

Upvotes: 0

Views: 1334

Answers (2)

Blue
Blue

Reputation: 557

Like @Anmol_uppal said, using class variables is the solution.

Right now, U, P, and R are local variables specific to the __init__ function. In the send function, you also are going to want to change each U, P, and R to self.U, self.P, and self.R correspondingly.

Here is the fixed code:

from Tkinter import *
# User Registers an account with a 

username password and passwor retype.
class Register:
    def __init__(self, parent):
        top = self.top = Toplevel(parent)

        VarEntUser = StringVar()
        VarEntPass = StringVar()
        VarEntRetype = StringVar()

        self.Label1 = Label(top, text = "What is your username: ")
        self.Label2 = Label(top, text = "Enter a password: ")
        self.Label3 = Label(top, text = "Retype Password: ")
        self.EntUser = Entry(top, textvariable = VarEntUser )
        self.EntPass = Entry(top, textvariable = VarEntPass)
        self.EntRetype = Entry(top, textvariable = VarEntRetype)


        self.Label1.grid(row = 0, sticky = W)
        self.Label2.grid(row = 1, sticky = W)
        self.Label3.grid(row = 2, sticky = W)
        self.EntUser.grid(row = 0, column = 1)
        self.EntPass.grid(row = 1, column = 1)
        self.EntRetype.grid(row = 2, column = 1)

        self.U = raw_input(self.VarEntUser.get())
        self.P = raw_input(self.VarEntPass.get())
        self.R = raw_input(self.VarEntRetype.get())

        self.MySubmitButton = Button(top, text = 'Submit', command = self.send)
        self.MySubmitButton.grid(row = 3, sticky = E)





# Checks the password and checks if all fields have been entered
def send(self):
    if len(self.P) <= 0 and len(self.U) <= 0:
            print "Please fill out all fields."

    else:
        pass
    if self.P == self.R:
        pass

    else:
        print "Passwords do not match"

    with open('username.txt', 'a') as fout:
         fout.write(self.U + '\n')
    with open('password.txt', 'a') as fout:
         fout.write(self.P + '\n')




# opens a new the registration window       
def Register():
    inputDialog = Register(root)
    root.wait_window(inputDialog.top)

root = Tk()

Lable = Label(root, text = 'Choose an option')

LoginB = Button(root, text = 'Log In', comman = LogIn) LoginB.pack()

RegisterB = Button(root, text = 'Register', command = Register) RegisterB.pack()

root.mainloop()

Upvotes: 0

Walle Cyril
Walle Cyril

Reputation: 3247

try this, note self. for variables that travels with the instance, you don t need raw input in tkinter, and you should define your own print2 methods which should print messages on the gui

from Tkinter import *

# User Registers an account with a username password and passwor retype.
class Register:
    def __init__(self, parent):
        top = self.top = Toplevel(parent)

        self.VarEntUser = StringVar()
        self.VarEntPass = StringVar()
        self.VarEntRetype = StringVar()

        self.Label1 = Label(top, text = "What is your username: ")
        self.Label2 = Label(top, text = "Enter a password: ")
        self.Label3 = Label(top, text = "Retype Password: ")
        self.EntUser = Entry(top, textvariable = self.VarEntUser )
        self.EntPass = Entry(top, textvariable = self.VarEntPass)
        self.EntRetype = Entry(top, textvariable = self.VarEntRetype)

        self.Label1.grid(row = 0, sticky = W)
        self.Label2.grid(row = 1, sticky = W)
        self.Label3.grid(row = 2, sticky = W)
        self.EntUser.grid(row = 0, column = 1)
        self.EntPass.grid(row = 1, column = 1)
        self.EntRetype.grid(row = 2, column = 1)

        self.MySubmitButton = Button(top, text = 'Submit', command = self.send)
        self.MySubmitButton.grid(row = 3, sticky = E)


    def send(self):
    """Checks the password and checks if all fields have been entered."""
        U = self.VarEntUser.get()
        P = self.VarEntPass.get()
        R = self.VarEntRetype.get()
        if len(P) <= 0 or len(U) <= 0:
            print "Please fill out all fields."
        else:
            pass

        if P == R:
            pass
        else:
             print "Passwords do not match"

        with open('username.txt', 'a') as fout:
                 fout.write(U + '\n')
        with open('password.txt', 'a') as fout:
                 fout.write(P + '\n')

# opens a new the registration window       
def launch_register(root):
    inputDialog = Register(root)
    root.mainloop()

root = Tk()
launch_register(root)

#Lable = Label(root, text = 'Choose an option')
#LoginB = Button(root, text = 'Log In', comman = LogIn) LoginB.pack()
#RegisterB = Button(root, text = 'Register', command = Register) RegisterB.pack()

Upvotes: 1

Related Questions