1

So I'm currently trying to learn python and I'm following this guide on youtube. It put me through this. But When I run it I get the error:

Traceback (most recent call last):
  File "C:/Users/Frederik/Desktop/test.py", line 29
    app = Application(root)
  File "C:/Users/Frederik/Desktop/test.py", line 11, in __init__
    self.create_widgets()
AttributeError: Application instance has no attribute 'create_widgets'

This is my code:

from Tkinter import *

class Application(Frame):
""" GUI with click counter """

  def __init__(self, master):
    """ Init the frame """
    Frame.__init__(self,master)
    self.grid()
    self.button_clicks =0 #Counts the button clicks
    self.create_widgets()

    def create_widgets(self):
        """ Create button widget """
        self.button = Button(self)
        self.button["text"] = "Total Clicks: 0"
        self.button["command"] = self.update_count
        self.button.grid()

        def update_count(self):
            """ Increase click count """
            self.button_clicks += 1
            self.button["text"] = "Total Clicks: " + str(self.button_clicks)

root = Tk()
root.title("Button Counter")
root.geometry("200x100")

app = Application(root)

root.mainloop()
4
  • 4
    Is that the actual indentation in your file? Commented Oct 26, 2015 at 13:28
  • You need to indent the whole def __init__ by one indentation level to ensure that the method belongs to the type. Commented Oct 26, 2015 at 13:29
  • you’re actually defining functions inside functions. have your defs at the same indentation level and all will be good. Commented Oct 26, 2015 at 13:32
  • you simply has to unindent each def statement (including func body). Commented Oct 26, 2015 at 13:42

1 Answer 1

1

As Mathias commented, the problem is in your indentation. Python requires proper indentation, as that's how it determines the beginning and ending of blocks.

Here is how your code should look:

from Tkinter import *

class Application(Frame):
    """ GUI with click counter """

    def __init__(self, master):
        """ Init the frame """
        Frame.__init__(self,master)
        self.grid()
        self.button_clicks =0 #Counts the button clicks
        self.create_widgets()

    def create_widgets(self):
        """ Create button widget """
        self.button = Button(self)
        self.button["text"] = "Total Clicks: 0"
        self.button["command"] = self.update_count
        self.button.grid()

    def update_count(self):
        """ Increase click count """
        self.button_clicks += 1
        self.button["text"] = "Total Clicks: " + str(self.button_clicks)

root = Tk()
root.title("Button Counter")
root.geometry("200x100")

app = Application(root)

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

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.