Python: 名称 'entry1 ' 未定义

Python: name 'entry1 ' is not defined

我写了一段代码,将 window 的背景颜色保存在文件中,关闭并再次 运行 程序后,它会记住 window 颜色(它保存在名为 'data.txt' 的文件中)。

# import modules

from tkinter import*  
from tkinter import messagebox as mb
import os.path

# create a window 
w=Tk()
w.title('My Application')
w.resizable(0,0)
w.geometry('300x300')


# Read files

if not(os.path.exists('data.txt')):       #check is the file exists
    print('file does not exists')
    file = open("data.txt",'w')
    file.close()

file = open("data.txt",'r')  #open a file


filelen=len(file.read())
print(len(file.read()))             #length of the file
file.close()


file = open("data.txt",'a')

print('file length is', filelen)
if filelen==0:               #if the file if empty, write default values
    file.write('0 \n0 \n0')            
    print('written to file')
file.close()

file = open("data.txt", 'r')


a=(file.readlines())


e1=float(a[0].replace(' \n',' '))
e2=float(a[1].replace(' \n',' '))    # remove '\n'
e3=float(a[2].replace(' \n',' '))



def _from_rgb(rgb):
    """translates an rgb tuple of int to a tkinter friendly color code
    """
    return "#%02x%02x%02x" % rgb   


file.close()

w.configure(background=_from_rgb((int(e1), int(e2), int(e3))))   #change the bg color to values from the file


def show_settings():

    settings=Tk()
    settings.geometry('400x200')

    entry1=Entry(settings)
    entry2=Entry(settings)
    entry3=Entry(settings)

    entry1.grid(row=1, column=1)
    entry2.grid(row=2, column=1)
    entry3.grid(row=3, column=1)

    changeInfo1=Label(settings,text='Red:',padx=20).grid(row=1, column=0)
    changeInfo2=Label(settings,text='Green:',padx=20).grid(row=2, column=0)
    changeInfo3=Label(settings,text='Blue',padx=20).grid(row=3, column=0)

    entry1.insert(1,e1)
    entry2.insert(1,e2)
    entry3.insert(1,e3)

    print(entry1.get())

    save=Button(settings,text='SAVE!',command=settings_save)
    save.grid(row=6, column=0, columnspan=2)


def settings_save():                     #save values to a file
    file = open("data.txt",'a')
    file.write('\n' + str(entry1.get()) + ' \n' + str(entry2.get()) + '\n' + str(entry3.get()))
    file.close()


button=Button(w,text='Settings', command=show_settings).pack()
w.mainloop()

当我 运行 它时,它有效,但是当我在设置 window 中单击 'SAVE!' 按钮时,它说:

 File "C:/Users *filepath*", line 89, in settings_save
        file.write('\n' + str(entry1.get()) + ' \n' + str(entry2.get()) + '\n' + str(entry3.get()))
    NameError: name 'entry1' is not defined

我怎样才能让它发挥作用?

entry1 对象是 show_settings() 方法的局部对象,因此在 settings_save()

中不可见

将对象移动到 class 中,它将可见

# import modules

from tkinter import*  
from tkinter import messagebox as mb
import os.path

...
entry1 = None;
entry2 = None;
entry3 = None;
...

def show_settings():

    settings=Tk()
    settings.geometry('400x200')

    entry1=Entry(settings)
    entry2=Entry(settings)
    entry3=Entry(settings)

   ...

    save=Button(settings,text='SAVE!',command=settings_save)
    save.grid(row=6, column=0, columnspan=2)


def settings_save():                     #save values to a file
    file = open("data.txt",'a')
    file.write('\n' + str(entry1.get()) + ' \n' + str(entry2.get()) + '\n' + str(entry3.get()))
    file.close()

...

w.mainloop()