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