I the following program, can someone please explain these two lines. "def __init__(self, master):, & Frame.__init__(self, master)"

One other thing. Shouldn't the method "update_count" use a return to return the new count? thanks.

# Click Counter
# Demonstrates binding an event with an event handler

from Tkinter import *

class Application(Frame):
    """GUI application which counts button clicks. """
    def __init__(self, master):
        """ Initialize the frame. """
        Frame.__init__(self, master)
        self.grid()
        self.bttn_clicks = 0    # the number of button clicks
        self.create_widget()
        
    def create_widget(self):
        """ Create button which displays number of clicks. """
        self.bttn = Button(self)
        self.bttn["text"] = "Total Clicks: 0"
        self.bttn["command"] = self.update_count
        self.bttn.grid()
        
    def update_count(self):
        """Increase click count and display new total. """
        self.bttn_clicks += 1
        self.bttn["text"] = "Total Clicks: " + str(self.bttn_clicks)
        
# main
root = Tk()
root.title("Click Counter")
root.geometry("200x50")

app = Application(root)

root.mainloop()

Recommended Answers

All 3 Replies

self

Automatically becomes the object of the class.

Like:

class SomeClassName:
    def SomeMethod(self):
        *some code*
obj1 = SomeClassName
obj2 = SomeClassName

So self is replaced with the obj name.

master, is simply the main frame window. If you notice this line:

root = Tk()

Master would usually be replaced with the root window, or the main window.

Because the "update_count" variable is in the same function that the button is made in, it does not need to use return.

To say it in simple terms, the line
def __init__(self, master):
is the class constructor and allows you to pass external variables like master (root) to the class. Remember, you used Application(root)

Since you inherited frame in line:
class Application(Frame):
you use
Frame.__init__(self, master)
which in turn makes the frame instance self
So self is the frame instance and also the global carrier for the methods in the class.

Now
self.bttn = Button(self)
makes the button part of the frame and you can access self.bttn in the other methods of the class. Class methods have self as the first argument, like def update_count(self):

Line
self.bttn["command"] = self.update_count
is a reference to the method and not a call. So it shouldn't return anything anyway.

self

Automatically becomes the object of the class.

Like:

class SomeClassName:
    def SomeMethod(self):
        *some code*
obj1 = SomeClassName
obj2 = SomeClassName

So self is replaced with the obj name.

master, is simply the main frame window. If you notice this line:

root = Tk()

Master would usually be replaced with the root window, or the main window.

Because the "update_count" variable is in the same function that the button is made in, it does not need to use return.

A lot of errors in that, better study up on classes some more!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.