0

I want to create some frames in Tkinter, that should be updated periodically. Here is the code for on of them:

from Tkinter import *
import time
import random

class KopfFrame:
    def __init__(self,master):
        frame = Frame(master,bg="tan")
        frame.pack(side=TOP,expand=YES, fill=BOTH)
        self.ZeitLabel = Label(frame)
        self.ZeitLabel.pack(side=RIGHT, expand=NO,ipadx=2, ipady=2)
        self.refresh()

    def refresh(self):
        self.ZeitLabel.configure(text=time.strftime("%H:%M:%S"))
        # call this function again in 5 seconds
        #print(self)
        self.after(5000, self.refresh)

root = Tk()
K = KopfFrame(root)
root.mainloop()

But, when I run it, I have the error:

AttributeError: KopfFrame instance has no attribute 'after'

I am pretty sure, that the way of calling is the problem. So if someone could help me, thaf I would either be thankful for a hint to a good tutorial for functions, classes an how to call an use them.

1
  • What did you expect self.after() to do? Your class does not define that method, perhaps something else has such a method? Commented Jan 3, 2017 at 16:32

1 Answer 1

1

You are trying to call self.after(), with self being an instance of your KopfFrame class. That class does not have such a method defined.

Perhaps you wanted to call the Frame.after() method here. You'll need to store a reference to the Frame object you pass in first, then call after on that:

class KopfFrame:
    def __init__(self,master):
        # make frame an attribute on self
        self.frame = Frame(master, bg="tan")
        self.frame.pack(side=TOP,expand=YES, fill=BOTH)
        self.ZeitLabel = Label(self.frame)
        self.ZeitLabel.pack(side=RIGHT, expand=NO,ipadx=2, ipady=2)
        self.refresh()

    def refresh(self):
        self.ZeitLabel.configure(text=time.strftime("%H:%M:%S"))
        # now you can reach the frame and call after on that:
        self.frame.after(5000, self.refresh)

What I changed:

  • Instead of making frame only a local in the KopfFrame.__init__ method, make it an attribute on self.

  • Use the self.frame reference to the frame in KopfFrame.refresh() to access the Frame.after() method.

Sign up to request clarification or add additional context in comments.

4 Comments

Now the the Error is: global name 'frame' is not definied
@Puetong0815: corrected, I had forgotten to adjust one frame reference in the __init__ method.
Yepp, in that way it works. Do You know some tutorials, that can explain me better that kind of problems? Or is this just learning by doing?
@Puetong0815 I'm sorry, it has been decades since I used Python tutorials, and I'm not in a position to recommend anything. Learn by doing is how I did it anyway. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.