Using button functions with arguments in tkinter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there's one more topic I do want to cover and that's going to be a shorter one and that is functions with arguments inside of a button and the keynite among you might have seen we have already fixed all of this because when we call the print function this one here we literally called a function with an argument and this one was only executed when we are actually pressing the button which means with this Lambda here we can call functions with arguments inside it's literally as simple as that although alternatively you could also create a function that returns and never functions and have arguments via that so let's explore both of those here I have a python file if I execute this you can see we have a basic tkinder window it doesn't do anything right now but I want to add two widgets let me add a comment here I want to have an entry this once again we get with ttk DOT entry this one is going to need the window as the master and I want to pack this entry widget besides that I need a button this I get with ttk DOT button and here we need to window as the master and for the text I just want to have button this button I also want to pack if I call this pack and execute it we get an entry widget and a button and basically what I want to do is this button should have a command that enables us to call some kind of function let me call it the button function and this button function needs to have an argument the argument is going to be the content of this entry widget to get the content we could use get here but I want to do this properly so I'm going to create an entry string and this is going to be a TK string VAR variable and let's set a start value to test this entry string I now want to pass into this button function when we are calling this function although the function doesn't exist right now so let's create it all the way at the top I want to create a button function this now does need to have a parameter let's call it the entry string all I really want to do in here is to print a button was pressed on top of that I also want to print entry string dot get with that we have a basic setup and try to think ahead if I were to execute all of this what would we get I suppose the best way to find out is to actually run this program meaning if I execute the code we can see in the bottom left we already have a button was pressed and test which is if I move this widget to the site this is what we have gotten from this function the problem is we got it without pressing the button on top of that if I press the button as much as I want nothing is going to happen so let's talk about what the problem here is the problem with this line right now is that python executes the code from the top to the bottom like so that should be fairly obvious and when it comes to this line here it sees a function and since we are calling this function with this bit here python is executing all of this which means we can see a button was pressed and we can see the value of this string variable here the issue then however is that this function is going to return none because we don't Define a return value as a consequence the value that this command is going to get is a none which doesn't do anything we don't get an error but when python tries to execute anything here it's getting command none and simply nothing is going to happen as a consequence of all of this we can see these two values when we are starting the program and afterwards nothing happens when we press the button and well there are two ways to fix this issue the easiest one the one that you are probably going to use basically all the time is you just wrap this function inside of a Lambda function this Lambda here tells python to only execute this code when we are pressing the button the reason why this works is that this Lambda function by default is not being called and then the Lambda function itself returns this function and then this is what the command value sees which means now if I execute a code we can't see any value and if I type in here and press on the button we can see a button was pressed and test and I just saw I made a mistake because this entry widget needs to have the text variable like so and this should be the entry string now let's try this again now we can see test inside of the entry widget and if I press button we can see a button was pressed and test on top of that if I change the value in here to something else and press the button again now we get a button was pressed and something else meaning this is working quite well and if you understand return values and functions this should be fairly straightforward although you could make it more explicit so let's say that you really do not want to use a Lambda function and you insist on using a function that you can call like this if you insisted on doing that you would have to create two functions wrapped inside of each other let's start with the outer one let me call it outer function and this one is going to have a parameter let's call it parameter inside of this function we're going to Define another function and this one I'm going to call the inner function this one doesn't have any parameters and this function is actually going to execute the code and here I want to print a button was pressed and on top of that I want to print the parameter which in this case is going to be this entry string which means we can run the get method on it once we have that all I have to do is to return the enough function and with that down here I can call the outer function and this should still work meaning if I run the code now nothing happens that's a good start and if I click on a button we can see a button was pressed and test and if I change the text to something else this one is also going to work when we come to this line here python is going to execute this function so we are executing all of this inside of this function we are creating another function this one is going to get a simple string and printed that one's quite easy and it also gets the value from the parameter which is the one we created down here the important part is this return in a function because this is what's being returned to the command argument so when we press the button this is what we actually get once again if you understand functions and how they exchange values with return this should be fairly straightforward so I hope that was helpful and most of the time you don't really have to think about it too much if you want to call a function with an argument you just use Lambda and then the function you want to call with the argument in this case the entry string that way all of this works perfectly fine
Info
Channel: Atlas
Views: 15,299
Rating: undefined out of 5
Keywords:
Id: Il5ujscQ0ps
Channel Id: undefined
Length: 8min 3sec (483 seconds)
Published: Sun Dec 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.