Python Tkinter Beginner Entry Widget and Get Method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is thinking through code here with another video and a new tkinter widget today we're going to be talking about the entry widget before we actually begin coding let's ask ourselves what exactly is an entry widget and the technical description pretty much just says that it is going to be a widget that will allow your user to give input to your gui application through a one line text string in simpler terms it's going to be a text box that is going to allow the person using your application to write on and whatever they write on that box you can use in your gui application in any type of program you're going to write it is very likely that you will need the user to input something for later use after all a very simplified diagram for software is the one showing only three steps the input the process and the output today we are solely focusing on the input part and the entry widget does exactly that on this video we're going to be using a very basic entry widget simply to demonstrate its functionality our initial code is also very basic we have a mainframe that is placed inside of our root window to start your entry widget you're going to give it a name i'm going to call mine entry 1 and then you're going to use the ttk module with the ttk module you're going to access the entry class and as always you always need to put the parent widget which in this case will be the mainframe [Music] that's as basic as it gets now you always know that you need to use a geometry manager in order to place your widget inside of your window so to do so we're going to use the grid method i'm going to place it on the row 0 column 0 and simply to demonstrate the frame that is behind it we're going to add some padding and when we run the current application we see that it's there it's fully functional i have the entry box and i can write something inside of it like hello world now however since we know how to create it we need to actually understand how we can use the value inside and the first method that i want to show you guys is the get method so the get method is used whenever we want to retrieve the string of text that is written by the user on screen right now you can see the way to use it inside of your code you simply use the object name of your widget and call the method through the dot operator all right so we're going to get to the code how about we try to use the get method in a print statement very simple example we're going to write this is my entry value and in here we're going to use entry one dot get a little typo over here [Music] if we run this application what do we realize okay this is my entry value nothing comes out now complete beginners might assume that it is simply because there's nothing written here but even if i try to write hello nothing's happening right if i close my application once again we finish with exit code 0 but we still don't have that value the reason why is because all of these lines of code like i previously explained in my first video is you're initializing all of the classes of widgets before you run your application your application actually only appears in main loop so everything that you're initializing here will appear on your root which is your main window and your main loop that's what's going on on your code you're you're stuck in a loop basically when you're running your gui application but everything that is run before is truly run before this line so whatever you have written here is getting initialized fine but if it's a print statement it's going to get run before your main loop and then obviously since in your main loop you have your entry widget you didn't put anything in there you're not actually getting the value and just to actually show you guys that we're gonna use the time sleep over here and we're gonna sleep for three seconds let me just make sure i imported time perfect uh there you go this is my entry value we wait for three seconds and that's where my main loop happens and then my entry widget is actually active before it was inactive so this obviously is ineffective how about we try something else so maybe some of you may say that sure fine what if you actually give your a value to your entry widget and then you try to use it when you close your main loop since there's a value on your entry widget maybe that could work let's give it a shot so we're going to print over here and say this is after main loop entry equal [Music] okay so we have our main loop happening and obviously since we're stuck in a loop we're stuck on this line of code 29 and this is not yet run by our python application so when i close this okay we get an error and this error tk enter tcl error invalid command name and then frame entry what's happening here you get this error simply because as soon as you close your window application everything gets destroyed so all the things that you created here are getting destroyed and then what it's telling you here is that once you call the get method here let's try it again if you realize well it's on line 31 this is where i'm getting it line 31 that's when everything got destroyed so i can't use this get method anymore so now you may be wondering fine if i can't use it that way how are you actually going to code your gui application so you can use the get method on your entry widget the reason behind this is simply because a tk inter gui application is a loop that is event based and what an event based application is it's simple an application that is always waiting from an action from the user of the system so an action could be the click of a button uh the the pressing of a key also on your keyboard so those events are going to trigger methods that are linking values with one another and that's when you're actually going to be using your gui application so this is another limitation of the get method if you want to use it properly you need to trigger it with an event and to do so there is no better way than creating a button and now i know that i haven't shown you guys button just yet uh buttons just yet but uh this one's gonna be very basic so we are still gonna have a video on buttons and obviously we're gonna place it on my mainframe and the text for my button is gonna be get text we're gonna place it in our grid we're going to pull it right before right below our entry widget on the same column and when i run this application this is what i mean with events guys when i have my my entry here and i click on get text i need to actually link this button this action of clicking my button i need to link it with a method that i need to define on my own and that's when i can use the get method so we're going to do that just now we're going to go to the top of our code i'm going to define my function right below it right below root and just a little technicality a little sidebar guys i've been calling them methods but actually a method is a function that is already associated to a class to a class that it has been defined before by either you or the module you're using if you define a function on your own it's called a function period not a method so methods are functions that are associated to classes and then functions are simply something that you can define on your hard code uh whenever you want for a specific purpose so that's just a little cyborg something that i wanted to correct just not to confuse that many people with my wording so right now we're going to define a function called get text no input necessary all we're going to do is going to print into the console inside of the button and we're going to test it out now we have our function that is going to do something and we have to trigger it with an event event based widget which is our button and tk enter being an event based application actually knows this and created the command option within these widgets so command is going to be equal to get text and do not write it down with a two parenthesis simply with the title now when i run my application perfect so now we're running our loop we are inside of our main loop on line 35 and i could do whatever right now but just to test it out we're going to click our button bam inside of the button now we know that this works so we're able now to have a triggering widget that is going to cre create an event to do whatever we want inside of our tk inter application so our test was successful therefore it is time for us to print out whatever we wanted to get from the entry widget in the first place so inside of entry one and we're going to put entry that get [Music] let's try this again running my application i'm gonna write down hello world click on get text inside of entry one hello world and that is the only way that you get the get method working and by the way guys this can go on and on as you're running the widget uh the the application sorry as you're running your gui app this can go on and you can test it out again so let me just try something else testing we click on get text testing there you go you have it right there and i don't know banana [Music] one more time and that's it you know that it works this is how you can use the get method guys and you can do this also to configure other widgets for example if i want to add a label here [Music] in the bottom we're going to link it to mainframe and my label one we will place it right beside below our button which is at row two and column zero we're going to add some padding on both sides so that you can see it inside of your frame and we're going to make it sticky on north south east and west when i run my application okay so we have our label here and our entry here obviously your gui is not really pretty you would have other labels to say what this entry is for or this label would have something in there but you know right now for the for the purpose that we have right now we're just going to show it so over here you have all inside of your label and if we go with the logic that i explained to you guys before and obviously this is gonna fail guys if i put text here and i put entry one dot get because i showed it to you guys with a print statement but i want to show you guys with another widget that actually would display the text that you get on your entry widget so when i run this application i can write hello get my text but there is no way that it is printing on my label and obviously by now you know why the difference between this one uh the the printing statement and the label is that one of them is triggered by an event inside of my main loop the other one there is no way to get this uh get this method right here you need to you need to really trigger it so obviously you know this you can configure inside of your function so we're going to call our label configure and then text is going to be equal to entry one dot get [Music] interesting so let's run this widget again so what should i write here so i like fruit so i'm going to put grape in there and when i click on get text not only can i use it in the back end with a print statement or associated to a value that i want inside of this function but now i have it also interacting with another widget now we've reached the end of today's video i'd like to thank everybody that has watched all the way till the end i truly appreciate the views and i'd like to thank all of you my viewers right now i recently reached 10 subscribers that was one of my first milestones i am so happy about that so just give a shout out to all of you that are that are interacting with me and watching my videos and leaving a like i truly appreciate that and you guys are the best by the way there's going to be more videos coming from for the entry widget just i would like to keep this short with the get method uh i want you guys to simply test your code test the get method play with your entry widget and think of the limitations that having an event-based triggered application can can have with you so uh there are some limitations and i'm gonna show you guys on the next entry on entry widget video that i'm gonna post but i want you guys to think about it and test it out on your own remember when you're a good programmer you don't only know how to get to one from point a to point b you also know all the wrong ways to get to from point a to point b always think about that so test it out and check out all the limitations that you have with the get method i will see you guys on my next video
Info
Channel: Thinking Through Code
Views: 2,825
Rating: undefined out of 5
Keywords: #python #tkinter #gui #programmer #coding #coder #python lessons
Id: -l8GTXtnrxA
Channel Id: undefined
Length: 13min 37sec (817 seconds)
Published: Sun Jul 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.