问题
#-*- coding: cp857 -*-
from tkinter import *
###########################################################
rt=Tk()
rt.title("MY FILM ARCHIVE v1")
rt.resizable(False, False)
###########################################################
def add():
def addFilm():
db = open(r"C:\Users\PC\Desktop\db.txt", "a+")
add.enter.get()
global film
film=enter[addingform,"text"]
db.write(film + "\n")
db.flush()
db.close()
addingform=Tk()
addingform.title("Add Film!")
addingform.resizable(False,False)
label=Label(addingform,text="Enter your's film:",fg="red",font=("Flux",15, "bold"))
label.pack()
enter=Entry(addingform)
enter.pack()
button=Button(addingform, text="Add!",command=addFilm, font=("Flux",15, "bold"))
button.pack()
button=Button(text="Add Film!",command=add, font=("Flux",15, "bold"))
button.pack(expand="yes", anchor="center")
mainloop()
I push the button, I write film and I push the button again for add the film.I get AttributeError: 'function' object has no attribute 'enter' error
回答1:
Local variables in a function are not visible as attributes on that function. For a nested scope, just use the name directly:
def add():
def addFilm():
db = open(r"C:\Users\PC\Desktop\db.txt", "a+")
global film
film = enter.get()
db.write(film + "\n")
db.flush()
db.close()
Here enter
will be attached to addFilm()
as a closure, and Python will attach the parent scope enter
to the function for later dereferencing. You do need to store the returned value; I guessed that you wanted to assign it to film
here.
来源:https://stackoverflow.com/questions/20152764/attributeerror-function-object-has-no-attribute-error