Button Command in Tkinter | Button Command Function With Arguments in Tkinter Using Lambda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you how to use functions as commands for the buttons in your python TK inre applications and we also going to see how to use the Lambda if you need functions with arguments by the way before we get started you can get the source code of some of my videos and support the channel at the same time the link will be in the description down below you can also find a few links to join amazing learning platforms to take your skills to the next level and a lot more go and check them out and thank you for supporting the channel I really really appreciate you so first first of all let's import SDK as usual all right then the root window just a simple 300 by 400 window and then just this so I don't want that to be resizable all right in this video I'm not going to use a class structure but on bigger projects you definitely need to use it now let's actually do something like main frame so I'm going to create the main frame where we're going to place a little button they we're going to use for the example so let's actually do something like don't know white then main frame Puck field okay both respond true I just like to make a main frame and then inside the main frame I actually put all the other widgets okay so basically this is the main container configure zero weight one which is basically so that the main frame stretches to cover the whole window and then down here I'm going to create the bottom one which is tk. bottom button the parent is the main frame of course and then text just button and then of course we need to actually place the button on the screen so going to use grid column is going to be zero and row zero and then of course down here we need to run the main Loop otherwise we are not going to see anything so main Loop like that so let's try to run that so python so as you can see you've got this button here and now that we have our ugly button on the screen by the way to style the buttons properly I've got other dedicated videos where I can explain you how to style it properly and how to add icons to that etc etc so as usual everything will be in the description box down below so let's say that when we click this button here okay we want to call a simple function without Arguments for now and later we'll see more complex scenarios so let's write the function up here yeah so up here we're going to write left task which is what we want to do when we click the button and we just want to print something like button clicked and of course we need to add it down here so command task like this so as you can see I've just added task without parenthesis but why I'm going to explain you why as usual so when you write functions with parentheses you're actually calling that function right but in this case you don't want to call the function straight away you want to call it only when you click the button so to do that you just need to write the function object here so as you can see this is the function object and then python andk iner under the hood we call this function when you click the button so basically that's why python expects a function object here because then when you click the button it will actually call that function for you by the way if you like the content of this Channel and you want to support it so that I can keep making videos like this check out all the ways you can do that down in the description you can for example get a source code of some of my videos make a small one of donation or even use the affiliate links to join amazing learning platforms and a lot more all the links will be in the description box down below and thank you so much for supporting my work I really really appreciate it if you write parenthesis like this when you run the application python goes through the code from top to bottom okay and when it's time to create the button it goes through all the options so here main frame text button Etc and then when you reaches the command here it says task with the parenthesis it thinks that it needs to run this function to then get the function object that will be used when the user clicks the button okay so let's actually try to visualize that so you've got this task okay when you run the application python goes through that and it gets to command task so it basically runs this function here to get the command so he thinks that that function actually returns something like this so he thinks that by running this function you get the function object that will then be used when the user clicks the button but in this case unfortunately task doesn't return a function object it actually returns none okay because basically up here we are not returning anything we're just printing something okay so basically something like this happens so if goes through that it tries to create the button he reaches this he runs the task then task doesn't return anything so the command becomes none so then when you click the button we don't get anything why because basically you're trying to run none and none is not a function object so basically you don't run anything so the task function is run just once at the beginning just to get the command and as you're not returning any command then from that point on the command is going to be none and then when you click the button nothing happen okay I hope that makes sense so let's actually run the application okay so as you can see down here you get button clicked okay straight away we've just run the application for the first time and you get button clicked why because as I said the first time it goes through all the code it gets the command it runs task to get the command but actually task just prints this okay and then when you click the button nothing happens why because when you click the button you're basically trying to call none but it doesn't work it's like you didn't have any command basically okay let's actually do something like this to understand that better okay so let's close that okay so let's actually add another function here called get function object and then I return just the task object and down here I do something like get function object and I actually call it I actually call the get function object if we run that as you can see down here you don't get anything and when you click the button that works why because in this case python goes through the code it reaches this function here it calls this get function object function but this get function object function actually Returns the task function object and that task function object will then be used when the user clicks the button so something like this happens in this case so you get get function object like that python goes through the whole thing and then this becomes something like task like that because that's what that function returns so basically here the command is going to be task and then when you click the button of course that is run like this by TK Inta by Python and everything works as expected of course in this case you could have just written something like task like that okay it doesn't make sense to do something like this but that was just to to show you that one if you write the function with parenthesis then the function is executed straight away and two that TK Inta expects a function object as the command so that you can actually run that when you click the button by the way if you're enjoying the video hit the like button to let me know and also subscribe to the channel as well for videos like this one so now you might be thinking what if I have a function with Arguments for example or how can I write it without parenthesis don't worry I'm going to show you different methods you can use used to do that first of all let's actually delete this like that okay like this perfect so now task will'll take two arguments AR one and AR two and then down here we're going to print them AR two like this perfect of course now you know that you can't do something like first second because this is run straight away Etc Etc so everything that I explained you earlier okay so one way to do this is using another function okay something like let's say that you've got function here I do something like def code task and then Tusk first second like that and then here of course you need to write call task and this of course works so you click and that works of course this is one way to do that but if you have a lot of buttons and functions it could be quite bad because you would have two functions for each button etc etc so there is another way to do that which is using the Lambda let's actually delete this okay and go back to task like that so as I said at the beginning we need a way to get a function object but in this case with arguments and we can do that as I said using the Lambda which basically returns a function object so let's actually see a basic example and then we'll get to more complex things so instead of task like that we can just do something like this is just an example so this is not a working example we can do something like that let's pretend that task is without arguments just for the sake of Simplicity to actually explain you a little bit how the Lambda Works under the hood so to make this simple this basically returns a function object that if called will run the task function okay so something like this let's actually make an example down here so command let's actually copy this all right and this when this is run so the first time you open the application this is run straight away and this Lambda here will return something like new function object like that and then when the button is clicked something like this happens so python runs this this returns a new function object and then this new function object is like something like this of course this is as I said to make you understand how things work okay so basically you run this you create a new function object and this new function object is actually this function here and then when you actually click the button p will do something like like this so it will actually run this function object and this function object of course runs this function here which is our function so basically to recap you run the Lambda the Lambda will create a new function object like this under the hood the function object is something like this okay and then when you click the button python will actually call this function and as you can see as this function calls task in here then task is actually run okay this is a quite simple explanation just to make you understand how things sort of work under the hood the really good thing about this is that with the Lambda you can create function objects with even default arguments and you can do that by doing something like this so let's actually keep all of this so you can actually understand so up here you could do something like Lambda and then P one first AR two second and then ARG one AR two like that which is the same as doing something like this so instead of doing something like that you can do something like directly first second of course then you can have a lot of combinations and ways to do things etc etc but after this video you will have a basic understanding of this so you can then make things more complex etc etc so this will actually cover a lot of use cases but I'm sure that some of you will need something even more complex but you will actually find a way to do that of course you could even have like a variable here you could do something like VAR and do something like first and then use that variable here okay you can do a lot of things a lot of combinations this is just an example but you can then create more complex things okay and in this case the Lambda will create a function object that will also remember the default arguments okay so it does something like this so let's actually change here so task second and then new function object and this new function object is like this so first second so basically under the hood you sort of as I said this is just an example to make you understand it doesn't work exactly like this but it is close enough so that you can actually understand visually so basically when python goes here and runs this Lambda then it creates this new function object which is the function object of this new function here that let's say the Lambda created for you and this function object of course it remembers that it needs to call the task function with these two arguments okay so then when the new function object is called then task is called with first and also second like this okay so now let's actually comment this out and see if that works as expected because if it doesn't work it's not good right let's try Okay so button clicked button clicked first second for a second so it works perfectly and this could be quite useful if you had let's say three buttons okay and you want to do different things for each button but you don't want to write three different functions but you just want to write one function and then inside the function do one thing for one button and other thing for the other button etc etc so let's actually first do something like this so let's actually remove this okay so let's actually keep that so first of all up here I'm just going to add this and this because we're going to add three buttons then here I'm just is going to pass for a second here that then I will add okay so Buton one this is going to be botton two and this is going to be bottom three okay down here two and three row one and row two like that and then task I'm just going to pass button one button two and button three why am I doing this actually let's do something like that why am I doing this because basically up here I'm just going to do something like button and then as you can see I'm passing the button name so this is the button one button two and button three because basically I want to then call the same function here and I want to do different things based on the button that was clicked I could do this with an if Al if Etc but I'm going to use match statement which I made a video about and I highly recommend you go and watch it after this one because if you know how to use the match statement it could be quite powerful okay so when we click one of the buttons because we actually using the same task task task we want to do something like button case button one so if we click the button one I'm going to print something like button one clicked of course here you could have a lot of other things not just that like that and like that okay so button two and button three and button two and button three like this and as I said you could have more buttons and you could have more code for each button so now basically for each of these commands this this and this python created a function object that will call for this one the task function with button one as the argument then for this the button two and for this the button three so let's actually try and see if that works all right so let's do this okay so button one button two and button three 2 one 3 etc etc etc so as you can see you've created one function and then with the Lambda you are actually understanding if you're clicking one button or the other and you just have one function for all the buttons and this is also useful when you have maybe let's say that you want to do one thing for the botton one one things for the botton two another thing for the bottom three and then down here you have something like they you want to do every time so whether you click the first second and third button then you want to do something like print all I don't know something like that then this is quite useful because as I said as you can see you just do different things for each button and then you can do other things for all of them okay so now on the screen you should see another interesting video about Python and TK Inta so go and check that out don't forget to check out all the links in description to support the channel and get amazing things like the video if you liked it and subscribe to the channel as well and I'll see you in the next one bye
Info
Channel: Fabio Musanni - Programming Channel
Views: 1,474
Rating: undefined out of 5
Keywords: python, python3, programming, coding, learn python, python tutorial, python for beginners, tkinter button command function, tkinter button command lambda, button command tkinter, button command function with args tkinter python, how to use functions with arguments in tkinter python, how to use function with args in tkinter button commands, tkinter button command example, tkinter button command explained, tkinter tutorial, tkinter python tutorial
Id: nsJtQeAFeXo
Channel Id: undefined
Length: 18min 42sec (1122 seconds)
Published: Wed Nov 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.