![]() ![]() NewPath = pathlib.Path(currentPath.get()).parent ![]() This will once again trigger the pathChange() function. After that, we just need to call the set(string) function on our StringVar and set it to this new path. Here we will use the parent attribute of pathlib.Path() object to get the parent folder of our current one. In the changePathB圜lick(), we made it so we can enter folders now we want the opposite: we want to be able to go back. # Set new path, will trigger pathChange function.Ĭt(path) Going One Folder Up Path = os.path.join(currentPath.get(), picked) # get the complete path by joining the current path with the picked item If it's False, we will set the StringVar to the new path, which triggers the pathChange() function we defined earlier and update the displayed files. If this turns out to be True, we call the os.startfile(path) with our path to open the file with its standard program. We check if the given path is a file with the os.path.isfile(path) function. We continue by joining with os.path.join() this picked file or folder with our current path, which is stored in a StringVar. The latter returns an array of all the selected items that's why we only need the first item. We supply the list.get() with the first value that is returned by the list.curselection(). We start by getting the name of the picked item by combining two functions. The changePathB圜lick() function does what it says on the box: It handles when the user clicks on an item in the list and then changes the path or opens the file. List.insert(0, file) Changing Path by Click or Enter # Inserting the files and directories into the list # Get all Files and Folders from the given Directoryĭirectory = os.listdir(currentPath.get()) Last but not least, we loop over every item in the directory list and insert it into the list with the insert(index, name) method. After that, we clear our list with its delete(start, end) method. We start by getting a list of all files and folders in a given path with the os.listdir() function. It will update the list of files and folders and is responsible for displaying them. This will be called every time our path changes. Let's start with the pathChange() function. This parameter will ensure that the functions will be called correctly in either case. On the other hand, keyboard bindings will send the keyboard event to the function, but we don't need that info. For one, there are called from buttons or menus, and these kinds of calls don't send any arguments to the supplied command functions. This is because the functions are being called from two inputs. ![]() Some of these functions have the parameter event=None and you notice that these event parameters aren't used in the function. id_rowconfigure(1, weight=1) Handler FunctionsĪfter the Tkinter setup, we will continue making some functions that handle most things happening when the user does something. ![]() # set a title for our file explorer main window Keep in mind that you can call these functions on any container widget. We will place our most essential widgets there, so they get a lot of space. These two functions ( grid_columnconfigure(), and grid_rowconfigure()) ensure that the second column and the second row expand. Next, we configure one column and one row. # Increas Dots Per inch so it looks sharperĬ(True) Tkinter Setup This will result in smoother graphics: from tkinter import * Calling the function in the last line will do just that. The ctypes import is optional we simply enable high dpi (dots per inch). W e get the os module this holds a special role since we do all the file interactions using it, such as getting all files in a directory or adding files. We are adopting some features from the standard file explorer like the line-edit add the top, opening files with their usual program, and adding new files or folders.Īs always, we import the needed libraries. These concepts are common to many coding-related projects, so completing this project will help you gain a deeper understanding of how they are used.In this article, we will make a simple file explorer with Python and its GUI Library Tkinter. We will then create functions to allow users to set a countdown time and will start the countdown. This program focuses on creating a GUI using the Tkinter library. These are the concepts that you will use and build upon as you code this project. Before starting this project, you should already have experience with creating simple Tkinter GUIs (Graphical User Interfaces), variables, and functions. This is a beginner-level project for those who are new to Python. This countdown program is the perfect way to build both your creative thinking and problem-solving skills while making a functional computer program. Projects like this allow students to practically apply the abstract concepts that they learn to create a final product. ![]()
0 Comments
Leave a Reply. |