AttributeError: 'function' object has no attribute error [closed]

情到浓时终转凉″ 提交于 2021-02-05 12:27:54

问题


#-*- 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!