AttributeError:这个方法有什么问题?

时间:2013-09-07 04:38:37

标签: python tkinter

第一个问题是我收到错误:

“AttributeError:'实例在''

之后没有属性'

我认为这与我宣布倒计时()的方式有关,但我不知道如何修复它。

class App:
    def __init__(self, master):
        frame = Frame(master)
        master.title("Araña en la Cabaña")
        master.configure(background="black")

        master.puntuacion= Label(master, text="Araña en la Cabaña", font=("American Typewriter", 40),bg="black", fg="red", justify=CENTER)
        master.puntuacion.grid(row=0, column=2)

        master.team_A_label= Label(master, textvariable= team_A, font=("American Typewriter", 30),bg="black", fg="red")
        master.team_A_label.grid(row=1, column=1)
        master.team_B_label= Label(master, textvariable= team_B, font=("American Typewriter", 30),bg="black", fg="red")
        master.team_B_label.grid(row=1, column=3)

        master.team_A_score_label= Label(master, textvariable= team_A_score, font=("04B", 35),bg="black", fg="yellow").grid(row=2, column=1)

        master.team_B_score_label= Label(master, textvariable= team_B_score, font=("04B", 35),bg="black", fg="yellow")
        master.team_B_score_label.grid(row=2, column=3)

        master.team_at_play_label= Label(master, textvariable= team_at_play, font=("American Typewriter", 30),fg="yellow", bg="black")
        master.team_at_play_label.grid(row=3, column=2)

        master.pregunta_start = Label(master, text="¿Cómo se traduce....?", font=("American Typewriter", 30),fg="orange",bg="black")
        master.pregunta_start.grid(row=6, column=2)

        master.pregunta_finish = Label(master, textvariable = pregunta, font=("American Typewriter", 30),fg="green",bg="black")
        master.pregunta_finish.grid(row=7, column=2)

        master.clock_label = Label(master, text="El Reloj:", font=("American Typewriter", 20),fg="red",bg="black")
        master.clock_label.grid(row = 6, column= 0)

        master.reloj = Label(master, textvariable = time_left, font=("American Typewriter", 30),fg="red",bg="black")
        master.reloj.grid(row = 7, column= 0)
        master.team_answer = Entry(master, textvariable=team_answer, width=50)
        master.team_answer.grid(row=8, column=2)
        master.team_answer.focus_set()
        master.feedback_label = Label(textvariable= feedback, font=("American Typewriter", 30),fg="green",bg="black")
        master.feedback_label.grid(row=9, column=2)
        master.traduccion_label = Label(master, text = "La traducción correcta es:", font=("American Typewriter", 20),fg="green",bg="black")
        master.traduccion_label.grid(row=10, column=1)
        master.give_correct_answer = Label(master, textvariable = give_answer, font=("American Typewriter", 20), fg="green", bg="black")
        master.give_correct_answer.grid(row=10, column =2)
        master.respond_button = Button(master, text="Responder",bg="black", command=check_response, justify=CENTER, borderwidth=.001)
        master.respond_button.grid(row=10, column=3)
        master.bind("<Return>", check_response)

        master.continue_button = Button(master, text="Adelante", bg="black", command=player_turn)
        master.continue_button.grid(row=10, column=4)



    def countdown(master, remaining = None):
        if remaining is not None:
            remaining = remaining

        if remaining <= 0:
#             master.label.configure
            time_left.set("¡Se acabó el tiempo!")
        else:
#             master.reloj.configure(text="%d" % master.remaining)
            time_left.set(remaining)
            remaining = remaining - 1
            master.after(1000, master.countdown)

2 个答案:

答案 0 :(得分:0)

countdown应将self作为其第一个参数:

    def countdown(self, master, remaining = None):
        if remaining is not None:
            remaining = remaining

        if remaining <= 0:
#             master.label.configure
            time_left.set("¡Se acabó el tiempo!")
        else:
#             master.reloj.configure(text="%d" % master.remaining)
            time_left.set(remaining)
            remaining = remaining - 1
            master.after(1000, master.countdown)

就像现在一样,master指向App实例,而您希望作为master传递的内容实际上是remaining。相关地,我怀疑你的真实代码是不同的,如果它给你那个特定的错误信息,因为使用这段代码你应该早一点得到错误。

答案 1 :(得分:0)

错误消息正好告诉您问题所在:

  

“AttributeError:'实例在''

之后没有属性'

这意味着,您正在执行的某个地方self.after(...),但您尚未定义名为after的方法,并且您也没有继承过该方法。 after是所有tkinter小部件都可用的方法,但App不是小部件,不会从一个小部件继承。

由于您将master传递给了倒计时方法,并且master是一个小部件,您可以在之后调用:

def countdown(self, master, ...):
    ...
    master.after(...)

另一个解决方案是将master保存为应用的属性,并使用它而不是将其传递给函数:

def __init__(self, master):
    self.master = master
    ...
def countdown(self, ...):
    ...
    self.master.after(...)