Tkinter尝试从文件写入输入到.txt

时间:2015-02-21 13:10:54

标签: python tkinter

我尝试使用条目获取输入然后将其写入.txt文件但我收到此错误 - Traceback(最近一次调用最后一次):   File" C:/Users/User/Desktop/Register.py" ;,第52行,在   fout.write(U +' \ n') NameError:name' U'未定义

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

2 个答案:

答案 0 :(得分:1)

试试这个,请注意自己。对于随实例传播的变量,你不需要在tkinter中输入原始输入,你应该定义自己的print2方法,它们应该在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()

答案 1 :(得分:0)

像@Anmol_uppal所说,使用类变量是解决方案。

目前,U,P和R是特定于__init__函数的局部变量。在发送功能中,您还希望将每个U,P和R相应地更改为self.U,self.P和self.R。

这是固定代码:

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