1

I’d like to create a restricted folder/ file explorer in Python (I have version 2.7.9, but I don’t mind changing that) for Windows.

Essentially, I want to initially specify the folder to which the code opens. For example, the code should initially open to: C:\Users\myName\Desktop\myDemoFolder (the user must not know this folder simply by looking at the GUI).

The user must be able to browse downwards (deeper into folders) and backwards (but only up to the initial folder to which the code opens). The user must be able to click to open a file (for example: pdf), and the file must automatically open in its default application.

An example of what I’d like is presented in figure 1. (The look of the interface is not important)

Figure 1: What I would like to get

Currently, I am able to get figure 2 using the code presented here:

from Tkinter import Tk
from tkFileDialog import askopenfilename

Tk().withdraw() 
filename = askopenfilename()
print(filename)

Figure 2: What I currently have

Research has indicated that it is not possible to change the default buttons in Tkinter windows. Is this true? If it can’t be done with Tkinter (and that’s fine), how else can we do it?

I’d happily choose simple, non-Tkinter code (perhaps using wxPython’s wx.GenericDirCtrl()) rather than elaborate Tkinter code, but no restrictive libraries please.

A modular design approach is not needed. I’d rather have simple (functional) code that is shorter than object-oriented code.

Community
  • 1
  • 1
  • as far as I know you can't change tkinter default buttons but you can set the path of initial directory when user opens for e.g: `askopenfilename(initialdir='path/which/you/want/to/set')` and show us what you have tried SO is not a code writing site. – shivsn Jul 05 '16 at 08:03
  • 1
    It is a lot of code you are asking for, since I can't think of a way that tkinter offers internally to achieve it. It needs to be built from the ground up. – Thingamabobs Feb 26 '23 at 22:41

4 Answers4

3

In general you would need to write your own hardware VM Sandbox, as whatever you do to open one folder or file in Windows will allow to break free into parents or children or sideways.

How else do exploits rapidly consume your data locations ?

Even open readme.txt in NotePad allows the user to circumnavigate their Hello World of folders and files. Hence MS keep trying to lockdown Printing Problems, but offer save as or print to folder.

For example with a common everyday file like a PDF, is it needs to shout out its location, to the system or it cannot be able to navigate its referenced locations, classic case yesterday somebody did a bad download.re.name.pdf and the file could not find links to page 3 onwards, because its own filename was not found.

So most files types opened by defaults, need to be able to navigate any other location in that same system, for saving temporary work files as they are viewed e.g. a DocX will need to keep a temp file or 3 open.

IF you use a containerised application like Sandie or Windows Own Sandbox then there is no navigation outside the root.

K J
  • 8,045
  • 3
  • 14
  • 36
0

I was trying to do the same thing when I realized that maybe you could create all the buttons you need and then set the color of the buttons you don't need to your background color using:

button-name.config(bg = "background-color")

Just change the "button-name" to your button's name and set "background-color" to the background color!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

You can set the initial folder with the argument

tk.filedialog(initialdir="{some folder}")

But to restrict the useres selection path you could go an write your one costume file selector with os and mirroring the files with a tkinter canvas. I hope this helps somewhat

Kirbs_btw
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Blue Robin Mar 09 '23 at 04:32
0

Here is the simplest file explorer that completes your objective:

import os
import tkinter as tk
from tkinter import ttk


class Filexplorer:
    def __init__(self, root, dir):
        self.root = root
        self.root.title("RifloSnake")
        self.tree = ttk.Treeview(self.root)
        self.tree.pack(side="left", fill="y")

        self.root.bind("<Return>", self.open)
        self.tree.bind("<Double-Button-1>", self.open)
        self.root.bind("<BackSpace>", self.back)

        self.path = None
        self.original_path = dir
        self.open_folder(dir)

    def open_folder(self, folder_path):
        self.path = folder_path
        self.tree.delete(*self.tree.get_children())
        parent = self.tree.insert("", 0, text=self.path, open=True)
        for item in os.listdir(self.path):
            if os.path.isdir(os.path.join(self.path, item)):
                self.tree.insert(parent, "end", text=item, open=False)
            else:
                self.tree.insert(parent, "end", text=item)

    def open(self, event):
        item = self.tree.focus()
        if self.tree.item(item)["text"] == "..":
            self.open_folder(os.path.abspath(os.path.join(self.path, os.pardir)))
        elif os.path.isdir(os.path.join(self.path, self.tree.item(item)["text"])):
            self.open_folder(os.path.join(self.path, self.tree.item(item)["text"]))
        else:
            os.startfile(os.path.join(self.path, self.tree.item(item)["text"]))

    def back(self, event):
        if self.path != self.original_path and self.path != self.original_path + '\\':
            self.open_folder(os.path.abspath(os.path.join(self.path, os.pardir)))

Initialize it like this, inputting the directory as parameter:

root = tk.Tk()
app = Filexplorer(root, "specified-directory")
root.mainloop()

You can open the files by double-clicking or selecting it and pressing Enter, and go back upwards pressing Backspace.

Adding buttons, features and improving the looks is up to you. Good Luck!

RifloSnake
  • 327
  • 1
  • 8