Transcript:
What’s going on, guys? John, the elder here from Cody Mecom in this video. I’m going to show you how open files with a file dialog with Kinter and Python. All right in the last video. We talked about opening other windows in your program in this video. I want to show you how to use the file dialog box to open files. No matter what the file is anywhere on your computer, but before we get started. If you like this video, want to see more like it. Be sure to smash the like button below. Subscribe to the channel and check out. Kodi me. Comm, where I have Dozens of courses with hundreds of videos that teach you to code use coupon code Youtube to get $22 off memberships. That’s all my courses, videos and books for a one-time fee, just $27 What’s yours insanely cheap? Okay, so you’ve got your program? You want to open a file? Maybe an image, maybe a PDF file anything at all. How do you do that with Kenter? Well, it’s really, really easy. You just use something called a file dialog and we need to import this. It comes with Kenter, but we need to import it so from Tkinter import file dialog. Okay, and to use this. We just call roof dot file name now. I should say this won’t actually open a file. It’ll just return the name of the file in the location of the file, So let’s say, you know, we’ve got these images in our Gui slash images directory, so it’ll return. C : Gui Kohler Slash images slash Aspen 1 PNG then. When you have that location, then you can then open that file in your program programmatically. So we’ll look at how to do that, but first things first, let’s just look at this dialog box, so we go file dialog and then dot ask. Open File name, right, and then inside of here, we got to pass a whole bunch of different things first. We need to tell it. What directory to start in? So when the box pops up? What directory do you want to be showing? So you go initial dir and set that equal to whatever you want. So if you just want the C directory, just do that if we for us, we want. Gui forward slash images! We do that, all right, so then we want title. You know, this is just the title of the box that pops up. It’ll have a little caption title at the top, and you could put anything you want. Let’s go select a file that’s good, and now we need to tell it types of files to show. So, you know, you could just put all the files or you could specifically say, just show the PNG files because we want to open a PNG file. You may have PNG files. You might have JPEG images. You might have gif images, gif images. How are you pronounce that you might have bitmap images? Whatever you can designate that here so to do that, we just we call the C file types, plural? I spot write file types. There we go and then set that equal to whatever you want and inside of here, We can designate a bunch of different file types if we want by putting them in parenthesis and then separating them by parental, so let’s see. Is that the right number of parenthesis? I think so, so let’s just start out with we want. I don’t know PNG files. Um, yeah. PNG files, and this first bit is just a little description and that’ll pop up in a drop-down box and we’ll see that in just a second, so then we need to tell it what type of file and PNG czar start with star DOT PNG and the store is basically saying open any files, but they have a name of any kind star DOT PNG, right. So that’s it for PNG. If we want to then designate say, for instance, all the files we can go see again, just type in a little description, all files and then a comma and then the type of file this is, This is just wildcard. We want all the files of all the different types, so we would go any name Dot any extension, all right, Star Dot star and then that closes and then okay. I think that’s right. I get a little messed up with all the different parentheses, but I think we closed them all, so lets. Just save this real quick and run it. I should mention I name. This file file Dot Pi. And I just started out with a basic starter code that we always start out with. You’re familiar with that already. So let’s head back over here and run this guy. Oops, what did? I do file dial. Oh, I misspelled something, of course. I did file dialog there. We go that looks better. Save it! Run it over when I did It just automatically puffed up this box and you’ll notice We’re in c4 slash Gui forward slash images because that’s what we designated, and then we have all these PNG files. These are the the images that we did when we made that image viewer several videos ago. So I’m just gonna reuse these, and now you look see, it, says PNG files or all files that comes from this little thing right here. PNG files and all files and we can type anything at all. We want right, lets. See if we come back here your code. Come back, actually, there we go if we click here for all files, it doesn’t really change because the only thing we have here are PNG files right, so lets. Close this real quick and let’s change this to Oops, JPG. And then let’s change the description to JPEG files. Okay, so let’s save this and run it again. There’s nothing listed because it’s calling the default is the first one you list and it’s calling for the JPEG files and when there are no JPEG files in this directory, so if we click all files boom, now they all pop up, right, so if we click this, it closes and nothing happens, so let’s take a look real quick and see what what happens when you click a thing what it returns so we could just return this root file name thing in a label, so let’s just go. I don’t know my label equals set it as a label. We want it in root and we want the text to equal just that. Let’s just pack this on the screen. Real quick, save this guy or run it again. A JPEG files. We want all files, let’s call. Aspen -. When we do that. Here’s our main program. It just returns the location. Aspen – right. So if you actually want to do something with that if you want to, for instance, open that, then you need to do that separately, but that’s okay, because we know the location, and now we can just open that location. We learned how to open images several videos ago when we looked at that image viewer app that we built. So we use pull up our code here. Is this image thing here? So lets. Go what we want. My underscore image equals image. TK. Dot photo image. I spell that right image. TK. Photo image there. We go turn blue there and we want image dot open and we want. What do we want to open? We want to open this thing. Whatever it is, root file name, right, so now we go my image label set that equal to a label and we don’t have to put it in root. We could just go image equals my image. Which is this thing and we want to pack this on the screen real quick, so let’s save that. Run it, see if this works and we get jpg all files, let’s open. Aspen -. Oh, image TK. Is not defined you. I thought that looked weird. Oh, lowercase. K alright, so lets. Save this one more time. Pull this over! Alright, so we want all files on a spend. And when we do, our main screen pulls up this thing, it’s also listed that because we have that on there and it’s it worse. So very cool now. One thing that you’ll notice is whenever I run this thing, it just pops up the file dialog box right away, and that’s probably not really practical, so let’s play around with that a little bit. Let’s create a button. Let’s go my button and set it equal to a button and we want this in root and we want the text to equal. Open File and we want the command to equal open. Now we need it. We need to define an open method here function and inside of here. I’m just going to put all this stuff, right. Have all this over, so we’ll remember when we open an image in a function like this. We need to create a global for its variable, so my underscore image that should work and our button. We need to hack this guy. Okay, so lets. Save this and run it. I almost certainly screwed that up. Somehow let’s see how I did it. Oh, steer object has mmm-hmm. What do we do, lets? See my button? Oh, we left it all out, all right. Get rid of that! Save it until Jeff screwed that up. All right. This is getting crazy! Let’s clear this screen. Run it one more time. All right. That looks good, so we pull this over. We got the button. It says open file. We do that and the little box pops up. We call all files here, So Van Aspen and boom it opens. It also puts this on the screen here. We don’t necessarily have to have that. That’s pretty cool so very easy, very simple to open any kind of file. And in this video, we did images because we’ve already worked with images before so we’re already familiar. But you know, you can do a PDF file. You can do an HTML file. You can do a Python file. If you wanted to open it and put its contents on the screen. Whatever you want to do, you can do with this open. Dialog this file dialog thing. And it’s pretty simple. The main thing you have to remember is just that it’s not opening the file. The file dialog box is just returning the location. It’s allowing you to click a file and select it, and then it just returns to the location. Once you have that location, you can open it. In any of the ways that we’ve already learned how in the past as far as opening images or or whatever you could, you know, paste out the contents and a big label. If you wanted whatever you want, you can do so 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 and check out coding Mecom or you can use coupon code Youtube to get twenty two dollars on membership. So you pay just twenty seven dollars to access all my courses, Hundreds of videos and the PDF versions of all my best-selling coding books join over 50,000 students learning to code Just like you. My name is John Elder from Khou-tv Comm. We’ll see in the next video.