Python 3 Programming Tutorial - Tkinter event handling

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

Creates a tkinter window using the tkinter module

Full Transcript

Hello way and welcome to part three of our tkinter tutorial series in Python 3. Where we left off, uh we made our window. Um and in fact, we can just go ahead and pop it up. Made our window here. Uh simple guey window. We can resize it. We can close it. We can click on this button, but we can't it's not doing anything. So we want to have some sort of event handling ability. So close this. And what we're going to want to do here is we say button. We give it you know self which could be bajumba text equals quit but then there's another parameter here that we can add in and we can say command and basically what command is is what do we want to happen right when we when someone clicks on this button when this button is like uh activated what do we want to happen well what we might want to happen is the following so self dot and then we're going to call it client_exit okay now client exit actually has no um doesn't exist yet. So that's not something that's you know built into tkinter or anything like that. So we define client exit. So it could have been called really anything we wanted. So now what we're going to do is same you know in our oops in our window class here um min class we have you know all these functions and stuff. So now what we can do is we can come down here and we can define a new function. Now uh this function we'll say define client_exit and then we'll say self and uh we'll just leave the parameters for now. And just as a quick aside um I've been getting some comments on one of my other series entirely and uh the the comment was that we're referencing functions before they've been defined. And so some people have been having trouble because they're trying to reference. They're it's like throwing an error saying, you know, this hasn't been defined yet and you're trying to reference it. Uh, and so they're saying that in the in the code it was for it's for a Pygame tutorial series and in the code it's basically something like this where we're referencing self.clientexit before we're actually at self or we're at client exit, right? So we're referencing this function before it even exists. And then we did it again up here, right? and we kept doing it. That's not against the rules of Python because right here we're just defining functions. And so functions are kind of like load you don't envision, you know, functions aren't really loaded until maybe the very end or it's it's hard for me to explain how why this is okay. It's basically acceptable because a definition of a function is not a running. So when you define a function, it's not being run. It's just being defined. And so when it really matters is when you call the function to be executed, it's going to say, okay, it's going as we load this function in, it's going to, you know, tell P or Python's going to be built like, okay, so we've got this quit button. It's a button. When it's clicked, we need to reference client exit, but it's not trying to reference client exit right at that moment. It's it's just storing to memory like what does it need to do with that function. So right now, putting self.clientexit client exit before the function client.exit. That's totally fine. That's not actually we're not reference I mean we are sort of you know in theory referencing it before it's been defined but we're not actually calling it before it's been defined. We're just storing it to memory. So a little bit different there. So if at the same time if like above class window let's say we called uh you know window um init window like that. Okay. Okay, if we did something like that, then yes, we would be defying um the principles because we we're calling first of all class window before it's been defined, but also init window before it's been defined. We're calling it to execution and we can't. So, just as a quick aside, not really quick, but um needed to be covered because like I said, some people were getting really confused about that in the Pi game tutorial series and maybe some people are getting confused about it now. Python is a scripting language, so it will go line by line, but it's only going to execute something that you're asking it to execute. So, um anyway, moving along. So, client x itself um actually can be very basic. We can say client exit self uh and all we really need to do is just exit. Okay, so we can call just this exit function. It's a built-in uh function into Python. So, with that, uh we can do this. And so, again, quit button. It's a button. It text says quit. Uh, and the command for it is self.client_exit. So when we click on the button, it's going to run that function. So it comes down here. It says client exit. What do we do? Exit. Done. So we can save that and we can run it. And up pops our guey. And we can hit quit. And just for the record, this pops up. The program is still running. Do you sure you want to kill it? We do. So we'll hit okay. And that's that's fine. Okay. So that is um some button or some error handling. Will you go away? That is um not error handling event handling uh with tkinter. Obviously we can make this much more complex. So in this sense all the button did was run client exit and all client exit did was exit. Now you can kind of foresee in the future some of these buttons might do some more fancy things than just that. Um but that's basically how you you would handle it. Okay. So, if anybody has any questions or comments regarding this video as as far as uh maybe what I was talking, you can also talk about uh referencing it before it's been defined if you if you're still confused about that. But if anybody else has any questions about whatever, uh feel free to leave those below. As always, thanks for watching. Thanks for all the support and the subscriptions. And until next time.

Original Description

In this tutorial, we cover tkinter event handling. In this scenario, we are adding a quit event to our quit button. In basically every circumstance, we're going to want to have our buttons actually do something or perform an action. This is called an event, and we can write code to handle events. In tkinter, this is as simple as adding a command, which we'll make into a function. Even though this function is a basic 1-line function that simply calls another function, we can see how we can later create more complex functions for our events. Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/ Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related AI Lessons

Up next
Salesforce Flow New Features (Summer '26) | Open Record, URL & Show Toast Messages
AITECHONE
Watch →