0

我正在使用 tkinter 创建一个程序,该程序带有存储在文本文件中的默认名称和密码。登录后,您需要打开Toplevel窗口并输入您想在后续登录中使用的名称和密码。我已经定义了我的变量,但是如果我想覆盖文本文件,我会收到以下内容:

错误“NameError:名称'e1'未定义”

我知道我已经定义了。

import sys
from tkinter import messagebox
from tkinter import *


now = open("pass.txt","w+")
now.write("user\n")
now.write("python3")
now.close()


def login_in():
    with open("pass.txt") as f:
        new = f.readlines()
        name = new[0].rstrip()
        password = new[1].rstrip()
    if entry1.get() == name and entry2.get() == password:
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("error","login Failed")


def change_login():
    ch = Toplevel(root)
    ch.geometry('300x300')
    e1 = Entry(ch, width=20).pack()
    e2 = Entry(ch, width=20).pack()
    sb = Button(ch, text="save", command=save_changes).pack()

def save_changes():  # function to change data in the txt file
    data = e1.get() + "\n " + e2.get()
    with open("pass.txt", "w") as f:
        f.writelines(data)


root= Tk()
log = Toplevel()


root.geometry("350x350")
log.geometry("200x200")

entry1 = Entry(log)
entry2 = Entry(log)
button1 = Button(log, text="Login", command=login_in) #Login button

entry1.pack()
entry2.pack()
button1.pack()

label = Label(root, text="welcome").pack()
butt = Button(root, text="change data in file", command=change_login).pack()

root.withdraw()
root.mainloop()      
4

1 回答 1

0

所以你在这里有几个选择,但总的来说你有两个主要问题。

第一个问题是使用.pack()创建后的 youre1e2entry 字段。这是该方法的一个问题,因为如果您以这种方式打包get(),几何管理器将返回。None正确的方法是首先创建小部件e1 = Entry(ch, width=20),然后将其打包到下一行,e1.pack()这将允许get()e1.

第二个问题是局部变量与全局变量的问题。您已经在函数内部创建了e1and并且没有告诉 python并且是全局变量,它会自动假定您希望它们作为局部变量。这意味着变量只能从创建它们的函数中访问。e2change_login()e1e2

你有几个选择,我会为你分解它们。

1)快速而肮脏的选择是分配e1e2作为全局变量。这可以通过使用global var_name, var2_name, and_so_onso 在这种情况下将此行添加到您的change_login():save_changes()函数的顶部来完成:

global e1, e2

这很好地告诉 python 添加e1e2到全局命名空间,它将允许save_changes()使用全局命名空间中的变量。

2)另一种方法是将2个变量传递给save_changes()使用按钮命令。我们将需要使用lambda它,我们可以通过添加:

command = lambda e1=e1, e2=e2: save_changes(e1, e2)

到创建的按钮change_login()并添加2个参数,save_changes()如下所示:

save_changes(e1, e2)

这与第一个选项一样有效,并且还避免了使用全局变量。

3) 第三种选择是将整个程序创建为一个类,并使用类属性来允许变量与类中的所有方法一起使用。

以下是使用第一个选项的代码示例:

import sys
from tkinter import messagebox
from tkinter import *

now = open("pass.txt","w+")
now.write("user\n")
now.write("python3")
now.close()

def login_in():
    with open("pass.txt") as f:
        new = f.readlines()
        name = new[0].rstrip()
        password = new[1].rstrip()
    if entry1.get() == name and entry2.get() == password:
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("error","login Failed")

def change_login():
    global e1, e2
    ch = Toplevel(root)
    ch.geometry('300x300')
    e1 = Entry(ch, width=20)
    e1.pack()
    e2 = Entry(ch, width=20)
    e2.pack()
    sb = Button(ch, text="save", command=save_changes).pack()

def save_changes():  # function to change data in the txt file
    global e1, e2
    data = e1.get() + "\n" + e2.get() # removed space after \n
    with open("pass.txt", "w") as f:
        f.writelines(data)

root= Tk()
log = Toplevel()
root.geometry("350x350")
log.geometry("200x200")

entry1 = Entry(log)
entry2 = Entry(log)
button1 = Button(log, text="Login", command=login_in) #Login button

entry1.pack()
entry2.pack()
button1.pack()

label = Label(root, text="welcome").pack()
butt = Button(root, text="change data in file", command=change_login).pack()

root.withdraw()
root.mainloop()

下面是使用第二个选项的代码示例:

import sys
from tkinter import messagebox
from tkinter import *

now = open("pass.txt","w+")
now.write("user\n")
now.write("python3")
now.close()

def login_in():
    with open("pass.txt") as f:
        new = f.readlines()
        name = new[0].rstrip()
        password = new[1].rstrip()
    if entry1.get() == name and entry2.get() == password:
        root.deiconify()
        log.destroy()
    else:
        messagebox.showerror("error","login Failed")

def change_login():
    ch = Toplevel(root)
    ch.geometry('300x300')
    e1 = Entry(ch, width=20)
    e1.pack()
    e2 = Entry(ch, width=20)
    e2.pack()
    Button(ch, text="save", command=lambda e1=e1, e2=e2: save_changes(e1, e2)).pack()

def save_changes(e1, e2):  # function to change data in the txt file
    data = e1.get() + "\n" + e2.get() # removed space after \n
    with open("pass.txt", "w") as f:
        f.writelines(data)

root= Tk()
log = Toplevel()
root.geometry("350x350")
log.geometry("200x200")

entry1 = Entry(log)
entry2 = Entry(log)
button1 = Button(log, text="Login", command=login_in) #Login button

entry1.pack()
entry2.pack()
button1.pack()

label = Label(root, text="welcome").pack()
butt = Button(root, text="change data in file", command=change_login).pack()

root.withdraw()
root.mainloop()

下面是您的代码转换为类并使用类属性来避免使用全局变量的示例。

import sys
from tkinter import messagebox
from tkinter import *


class MyApp(Frame):

    def __init__(self, master, *args, **kwargs):
        Frame.__init__(self, master, *args, **kwargs)

        self.master = master
        self.master.withdraw()

        self.log = Toplevel()
        self.master.geometry("350x350")
        self.log.geometry("200x200")

        self.entry1 = Entry(self.log)
        self.entry2 = Entry(self.log)
        self.button1 = Button(self.log, text="Login", command=self.login_in)

        self.entry1.pack()
        self.entry2.pack()
        self.button1.pack()

        Label(root, text="welcome").pack()
        Button(root, text="change data in file", command=self.change_login).pack()

    def login_in(self):
        with open("pass.txt") as f:
            new = f.readlines()
            name = new[0].rstrip()
            password = new[1].rstrip()
        if self.entry1.get() == name and self.entry2.get() == password:
            self.master.deiconify()
            self.log.destroy()
        else:
            messagebox.showerror("error","login Failed")

    def change_login(self):
        ch = Toplevel(self.master)
        ch.geometry('300x300')
        self.e1 = Entry(ch, width=20)
        self.e1.pack()
        self.e2 = Entry(ch, width=20)
        self.e2.pack()
        Button(ch, text="save", command=self.save_changes).pack()

    def save_changes(self):
        data = "{}\n{}".format(self.e1.get(), self.e2.get())
        with open("pass.txt", "w") as f:
            f.writelines(data)


if __name__ == "__main__":
    now = open("pass.txt","w+")
    now.write("user\n")
    now.write("python3")
    now.close()

    root = Tk()
    app = MyApp(root)
    root.mainloop()
于 2017-09-14T15:16:40.320 回答