Keyboard Event Binding With tKinter - Python Tkinter GUI Tutorial #44

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from Cody me calm and in this video I want to show you how to bind keyboard events with Kenter and Python alright guys like I said in this video we're gonna talk about binding keyboard events with kinter but before we get started if you liked this video I want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out Kona me.com where I've dozens of courses with hundreds of videos to teach you to code these coupon code youtube one to get $30 off membership that's all my courses videos and books for one-time fee it just $49 which is insanely cheap alright keyboard events with kinter so what am I talking about well up until now whenever we want to run a function in our kinter apps we click a button usually and that fires a command the thinner runs the function but that's not always what you want to do you may want to run something based on some other event maybe you want to click on a drop-down box and you don't want to click a button you just want to click on something in the drop-down box and then you want an action to take place or maybe you want to type something on the keyboard somewhere you know you want to hit the Q letter on your keyboard and you want something to happen how do we do these sort of event-driven things with Kenter now what JavaScript is very easy to do these sort of things Java scripts were made for that but with Kenter it's a little bit different so we're gonna start to talk about it in this video I'm just gonna give you the very basics in this video in the next videos we're going to you know go into this in more detail I know we were talking about classes in the last video and we're gonna get back to classes but I've been getting a lot of questions about this exact thing recently so I wanted to touch on it really quickly before we move forward with classes and also we'll use this in classes as well so let's pull up our code and I created a file called bind dot pi and this is just the same starter code we've had forever so up until now and you notice I'm not gonna use classes in this video just be confusing and we're just gonna talk about this binding keyboard events and just that in this video so up until now if we wanted to take an action we clicked a button so let's just do that real quick so let's go my button equals and this is button we want this in route want the text to say click me and we want some action to take place we want to command and let's call this clicker right so then we want to my button dot let's just pack this on the screen give this a pad Y of 20 just for some spacing okay we've got this button we can click it now we need a clicker function so define clicker and then say we had a label let's call it my label equals a label and that's in route and then want the text to say you clicked button my label dot hack and that should work so let's say this in run just to make sure this is working so Python Bank I we've got this boom we click it you clicked a button alright very basic very simple that's what we've always done up until now well we can do something called binding and binding allows us to sort of say hey when this happens bind it to this particular widget so in our case here we've got a button widget and up until now we use this command clicker thing to call a function but instead we want to do binding so to do that we just go my button and this will be the case for almost any widget you want to use if you want to use this on a label if you want to use this on a drop-down you'll just go the name of the thing dot bind so now instead of buying we need to pass a couple of things we need to need to pass the event and the action right so what was the event that we want to capture well we usually do that with quotation marks and then it's almost always these opening closing brackets right and now what we want in this case I want to grab a mouse click a left mouse click so you want to left-click on your mouse that would be button - one and we'll talk about these different ones of these in a second now what action do we want to take place well we want this clicker function to be called right so now we have to pass this event into this clicker function so we do that just by going event like that we could save this okay so now if we come back here and run this guy again but you can say click me I left clicked on this button with my mouse and it says you click the button now if I right click on the button nothing happens at all right if I left click it clicks it if I right click nothing happens so let's change that so say we wanted a right-click well that would be button - 3 button - is the middle mouse button most mice don't have a middle button anymore but they used to and that was button to button 3 is the right mouse button so if we save this and come back here and let's run this again now if we left click nothing happens if we right click though boom this pops up and you can see the buttons not even being pressed I'm just right clicking on my mouse you can't tell because it's not on camera well let's see this see what we can do here this is the right mouse button my mouse is moved alright let's try this all right very carefully see now if I left click the button gets clicked but nothing happens all right click it happens so that's kind of cool and that's all there is to it so so we can pass certain things through here we can pass coordinates where on the screen did we click the button so let's concatenate here and that's just event dot X and let's concatenate a space and then event dot Y now this will return an integer so we have to change this to a string just convert this real quick here STR okay so if we save this and run it like again and now it's this thirty and seven if we click it over here v 5 and 16 if we click it up here right if we outs if we click outside of the button nothing happens if you're right if we left click nothing happens but we right click that number changes now why would you need that I don't know you might not but if you're playing a game if you're making a game or something you need to know where the mouse is actually clicked to do something you would do like that let's see what else we can do there are a lot of these event things button 1 and button 3 are the ones for your mouse so we can determine if the mouse goes over the thing we can do enter did that mouse enter this widget so if we save this and run it give this a try here watch this as soon as I drag my mouse over boom boom not clicking any buttons at all I'm just entering this widget it's firing that thing so that's an interesting one you could also do leave so we have enter and we have leave if we save this and run it so we're in here as soon as we leave boom we're in here soon as we leave boom that's kind of interesting hey all kinds of different events you can do we can do focus in so that means the the keyboard focus is on that widget so think if you like tab through different things when you tab onto it it will do it I don't know if we'll be able to see this or not since we don't have any other things tab on too but there we go I tabbed you can see it's been highlighted because I cabbed on to it now if I click somewhere else well there's nothing else to click on so we can only see this once but you can do focus in you could also do focus out if you tab away from it let's see return I don't know if we can do this or not with it a button return is like hitting the actual Enter key on your keyboard so let's run this and see I'm not sure if this will work or not so if we hit enter now because we're not actually well okay so I tabbed over now if I hit enter it works enter enter enter because I've tabbed on here and highlighted it right so it's sort of like activated so that's cool let's try the key one real quick let's see if we can hack something that together for key now this will do any key on your keyboard right so let's run this and I'm gonna tab over to highlight it now I'm going to hit the S key as soon as I do boom it happens now we can actually grab that key we can go see you click the button you clicked let's say you clicked this button and it's event dot key so if we save this and run it so tabbed I'm gonna hit the D key oh we got an error what happened let's see event key event has no attribute key oh it's not key it's car that's right so what character did we press so if we highlight and I click deed you click the D button now if I hit a s W Q so that's really kind of cool you can get the width and the height of the widget you can pass by you know event dot with an event height you can do key symbol that would be he s Y M save this and run it you like hitting the shift key now if I hit shift and whoops shift and run away from us say the shift in the one so this would be the ex clannish mark see it says exclaim shift and the plus button plus shifting question mark forward slash period you know different symbols that's cool etc cetera so a lot of people have asked how do you do drop downs this video is getting a little bit long so I think we'll do that maybe in the next video but you get the idea you can play around with this now and it's you know a lot better in some cases to do it this way versus calling a command on a button like we used to do there are certain instances where you just need to do something like this where you need to grab a keyboard event or you know moving the mouse around the the app in some way or things like that so pretty easy and pretty cool so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm which really helps the channel out and I really appreciate and check out Cody be calm or you can use coupon code youtube one to get $30 off membership seepages $49 to access all my courses hundreds of videos and the PDF versions of all my best-selling coding books join over eighty five thousand students learning me to code just like you name is John elder from Co to me calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 55,146
Rating: undefined out of 5
Keywords: tkinter event binding, tkinter event handling, tkinter event add, tkinter event, event binding tkinter, event binding tkinter python, event binding python, python event binding, python tkinter event handling, python tkinter events, python tkinter button click event, tkinter button click event, tkinter button click, tkinter mouse click on canvas, tkinter mouse click event, python tkinter mouse click position, tkinter mouse click, tkinter, python gui tutorial
Id: GLnNPjL1U2g
Channel Id: undefined
Length: 11min 14sec (674 seconds)
Published: Tue Mar 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.