- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- Python Versions
- Python Environment Setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control & Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break Vs continue
- Python pass Vs continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List Vs Tuple Vs Dict Vs Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python Keywords
- Python All Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List
Python Event Handling
Here you will learn about GUI event handling in python, or event driven programming with python.
Binding Widgets and Event Handlers
As you have already learned about gui development using python, but haven't written some code to handle event. Therefore let's bind widgets with some events.
Python Event Handling Program
Here is a simple event handling program in python.
from tkinter import *; class MyApp(Frame): def __init__(self, master): Frame.__init__(self, master); self.grid(); # below is the number of button clicks self.button_clicks = 0; self.create_widget(); def create_widget(self): self.buttn = Button(self, text = "Total Clicks = 0"); self.buttn["command"] = self.update_click_count; self.buttn.grid(); def update_click_count(self): self.button_clicks += 1; self.buttn["text"] = "Total Clicks = " + str(self.button_clicks); mainWindow = Tk(); mainWindow.title("GUI Event Handler"); mainWindow.geometry("300x200"); app = MyApp(mainWindow); mainWindow.mainloop();
Above event handling program counts the total mouse clicks by user. That is, initially it will be displayed as Total Clicks = 0 and when user click on that button, then it is update to Total Clicks = 1 and when again click on the same button, it will again update with incrementing the previous value (that is 1) by 1, and will become Total Clicks = 2 and so on.
Here is the initial output.

Here is the output produced after first click of mouse over the button.

And here is the output produced after 5th click of mouse over the button.

« Previous Tutorial CodesCracker Home »