How to bind an event with an event handler with Tkinter with Python 3

# Click Counter
# From the book: Python Programming for the Absolute Beginner
# 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.bttn_clicks = 0    # the number of button clicks

    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

    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")

app = Application(root)


Leave a Reply

Your email address will not be published. Required fields are marked *