Text Box Widgets in Tkinter - Python Tkinter GUI Tutorial #99

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys john elder here from coding.com and in this video we're going to look at text boxes with kinter and python all right guys like i said in this video we're going to look at text boxes but before we get started if you like this video you 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 codamy.com where i have dozens of courses with hundreds of videos to teach you to code use coupon code youtube1 to get 30 off membership that's all my courses videos and books for one time fee is just 49 which is insanely cheap okay so text boxes and kinter are really kind of cool actually and we haven't talked about them until now but we're going to talk about them for the next few videos because you could do an enormous amount of stuff with these things you could put text in them you could put images in them you could put other windows in them you could do all kinds of stuff you can use them to make text editors and we may do that in the future and but most importantly you can use them to do multiple lines of text as you can see here you know in the past we've done a lot with entry boxes but those are generally just like one line right so you can make the box bigger but it's still just like one line of text here we can have as many lines as we want and uh that's cool so this is what we're gonna do in this video we're just gonna get into the very basics of this because like i said there is a lot going on with this text widget and that's gonna take us a while to sort of unpack it all so let's go ahead and close this i've got a file i'm calling it text dot pi we've got our basic starter code that we always have our main loop i'm using the sublime text editor and the git bash terminal as always so first things first let's just create a text box i'm going to call this my text and it's going to be a text widget and we want to put it in root and that's really all we need to do we can also designate the height and width so i'll just go ahead and do that let's go width equals 60 and height equals 20. now these are not in pixels this is in text widget whatever's units i don't know it's sort of like a list box where it's not pixels so you just kind of have to sort of play around with these numbers to get it to look the way you want it to look so let's go my underscore text dot pack and let's get this cpad y of 20 push it down screen a little bit okay so let's go ahead and save this and run it just to see what this looks like right off the bat so python text dot pi and you see we've got our thing and we can type stuff so we can change the font color or the font size if we want you know just like we've done a zillion times before so let's go font equals and let's go helvetica and let's be like i don't know 16 or something save this run it pull this over and now we have you know slightly bigger text maybe a little bit easier to read so okay so far so good piece of cake this is just you know sort of what we've always done with widgets in the past very similar but here's where it's gonna start to change a little bit let's create a button and let's make that button clear the screen so actually i'm going to create a frame because we're going to have several buttons so i'm going to call this button frame and it's a frame widget and we just want to put it in root and let's go button frame dot pack and just pack this onto the screen so inside of here we can create our buttons so let's create a clear button and that's going to be a button and we want to put this in the button frame and we want the text to equal clear screen and let's give this a command of clear so let's put this on the screen so clear underscore button we want to grid these let's put it in row 0 and column 0 and the reason why oops zero and the reason why i'm grading these is because we're going to have several buttons we want to put them all sort of next to each other in a line so we're going to grid these guys so okay we've got our button now let's create our clear function so let's come up here to the top and let's create clear function and let's define this guy as clear so now what we want to do is clear our text box so we've typed some stuff in the text box how do we delete all these things so our text box is called my underscore text so we call the delete function and this is normal we've deleted things like this in the past and just like in things we've deleted in the past like list boxes for instance uh even entry boxes we want to put a range of positions here so normally we would start at zero and then we want to delete everything and we would go to end now this is how we delete delete things from entry boxes this is how we delete things from list boxes i believe we've done this a lot of times and you would think this is how you do it but actually you don't because text boxes don't start at zero like everything else in python you know python lists start at zero top will start at zero all these kinder widgets we've done always start at zero not so with the text box it starts at 1.0 so it doesn't even start at one it starts at 1.0 so i think probably this has to do with positioning if we want to get really granular and delete things like the 14th character on line 1 we would probably go something like this but anyway it starts out at 1.0 and we could designate to to go to the end so let's go ahead and save this and run it and see what this looks like did i mention it is friday here in las vegas stuff that's right it is friday far day friday there we go vegas baby i have no plans for the weekend oh yeah i do actually have plans going out for drinks tomorrow so let's see you know what we've done ah we need to make this whole thing bigger so this button works on here so okay there we go so now if we clear the screen boom that works so okay so that's that so let's move our width down to like 40 by 10 i don't know save this let's run this guy again okay so that's a little bit better size-wise so okay anyway moving right along so now we've got something we've typed into the text box right how do we get that back out and do stuff with it you know add it to a variable save it to a file that's one of the things we're going to be looking at in the future videos how to save the stuff that we type into that box into like a text file that we could save on our computer so the first thing the first step in that obviously is getting the text out so how do we do that well let's come down here let's create another button and let's call this the get text underscore button i don't know and this is a button and we want to put this in the button frame and let's get the text equal and set the text equal to get text and let's give this a command of get underscore text and we'll make this function in just a second so now let's get text button dot grid this guy and let's put this in row zero and column equals one let's give this a pad x of like 20 to you know keep the buttons apart a little bit so okay we've got that now let's create a function say grab the text from the text box so let's define this guy get text so now how do we get this stuff well let's come back down here again and underneath here let's create a label like we always do and that's going to be a label and we just want to put this in root and the text will be nothing right now and let's my underscore label dot pack and give this a pad y of 20 to push it down the screen a little bit so now we can take my label and let's output whatever's in our text box into our my label and slap it up on the screen so we can do this as we always have we can config this and set the text equal to what well usually with other widgets we get things and you can get from your text box as well so we can go my underscore text dot get and this is the function now just like when we deleted things we need to declare a range of positions that we want to get from the text box so you can get only one line you can get you know many lines you can do all these different things so again we start out what position do we want to start we want to start the very first line it's not zero like we would normally think it's again 1.0 and we want to go to the end just grab everything so all right let's go ahead and save this and run this guy so let's go this is stuff uh line two line five or whatever now if we get the text it outputs it here and you'll notice it even outputs the you know blank lines so this is stuff blank line line two line four whatever and we're good to go we can clear this and that's cool so so like i said we can get a range of text out of here so instead of uh getting from one to end we could say from 1 to 3.0 so this will get from one up until line three it won't actually copy line three so it'll copy line one in line two and it'll stop at line three so that's what it means this range means it does not include this so if you wanted line three you would instead type four and you would stop at line four so let's go ahead and save this and run it to see that this works so let's go line one line two line three and line four now if we get the text remember we're getting from one line from line one to line three but not including line three so it should just copy these first two lines so if we click this boom line one and line two and that's how that works so that's the text box very sort of introductory stuff for this thing like i said there's a ton of stuff you could do you can use this to create a text editor and all kinds of other things you know just right out of the box it's handy if you need something you know like an entry box but with multiple lines if that's all you want to use it for it works great for that and it's just this easy to use it if you want to you know do something more complicated put images in there create you know rich text files with uh all kinds of different things you can do that you can save these things to files you can open text files and output them here and it will keep the general format right you know line breaks and things so just all kinds of stuff you could do with this thing and like i said we're probably going to be looking at this for the next few videos play around with this since there's so much that we can do with this and that's cool so that's all for this video if you liked it be sure to smash the like button below subscribe to that channel give me a thumbs up for the youtube algorithm and check out codeme.com where you can use coupon code you do one to get 30 off membership so you pay just forty nine dollars to access all my courses over forty five courses hundreds of videos and the pdf of all my best-selling coding books join over a hundred thousand students learning to code just like you my name is john elder from codeme.com and i'll see you in the next video
Info
Channel: Codemy.com
Views: 91,687
Rating: undefined out of 5
Keywords: tkinter text box, tkinter text widget, tkinter text editor, tkinter text size, python tkinter text widget, python tkinter text editor, python tkinter text box, python tkinter text input, python tkinter text box size, python tkinter text label, python tkinter text editor example, tkinter text multiple lines, python tkinter multiple lines, python gui tutorial for beginners, tkinter text, python tkinter text, tkinter text box clear
Id: Qrmab6lSzU4
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Fri Jul 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.