How to Create a Restricted File Browser in Python for Windows - python

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)
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)
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.

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!

Related

Applying LiveServer Logic for Tkinter File

The people who are familiar with the Live Server of VS Code, would have easily understood what is the main motive of this question.
But for others, here's the explanation:
Main motive of Live Server is to Automatically Reload Your Site on Save in web development! (Which get changed for python tkinter).
When ever I change something in my python file which contains tkinter code, the change should be reflected in the main window (the main window should not re-open to reflect the changes).
I have tried to search on web as well as on stack over flow, but all the results are for updating value in entry, label, buttons etc. But what I want is, the whole window should be updated when ever I change something in my main file, and the main window should not be reopened to do so. So in short, updating whole window without closing it, on every changes in the main file or automatically reload your program on save without reopening!
What have I tried?:
I tried to detect change in file using os.getsize which satisfied the first part of my question, but however I am not able to solve the second part i.e window should not be closed.
import os
main__tkinter_filename="myfile.py"
initial_filesize=os.path.getsize(main_tkinter_filename) # Getting size of the file for
# comparison.
while 1:
final_filesize=os.path.getsize(main_tkinter_filename)
if final_filsize<intial_filesize or final_filesize>initial_filesize:
webbrowser.open(main_tkinter_filename)
Example:
from tkinter import *
root=Tk()
root.mainloop
results in the below GUI:
If i have added a=Label(text='text')anda.pack() after root=Tk(), it should show me the label, and if i have removed the same code, it should remove them.
I will answer your question by the best of my understanding,
I have some (a few projects of my own, still way too limited) experience with flutter which has hot-reload feature (same as you described above, which you want with python, mainly tkinter), I recently switched to python for gui (Loved it!), so I would like to share my research here:
I was successfully able to set up hot-reload both with kivy (kivymd hot reload, which comes with watchdog and kaki, which works real-time), and with tkinter, while there is a hitch with the later, you will have to press Ctrl + R as to reload the tkinter window, but it works without having to re-run the python program, I will leave the link to the found resources here, hope it helps with your query!
To setup hot-reload with tkinter (requires Ctrl + R), please refer here.
To setup hot-reload with kivy/kivymd (real-time), which I personally prefer, you can find the official docs here.
To mention, I use the above on Manjaro (Arch linux) with pycharm, atom, but I have also tried and have made it run successfully on Windows 10 with vs code (worked like charm)
Hope I could be of help! If you face any problem regarding the same, please feel free to ask! Thanks!
After digging around I have finally found out a way to implement hot reload feature (which #Stange answers provides) but just updating the selected frame or code.
The basic idea is constanly reading the file and executing the selected code, and removing the object in a list which are meant to be removed.
# Live Checker.py
import keyboard
while 1:
if keyboard.is_pressed("Ctrl+r"):
with open('test.py','r') as file:
file_data=file.read()
file_data_start_index=file_data.find("'#Start#'")
file_data_end_index=file_data.find("'#End#'")
exec_command=file_data[file_data_start_index:file_data_end_index]
with open('exec_log.txt','w') as txt_file:
txt_file.write(exec_command)
Here I am constantly checking if if ctrl+r key is pressed, and if pressed
it reads the file,
writes the selected code from the file into a txt file.
I have specified the start and end of the code to be updated by #Start# and #End# respectively.
# Main.py
def check():
with open('exec_log.txt','r') as exec_c:
exec_command=exec_c.read()
if len(exec_command)==0:
pass
else:
print(exec_command)
exec('for i in root.winfo_children():i.destroy()\n'+exec_command)
print('exec')
with open('exec_log.txt','w') as exec_c:
pass
root.update()
root.after(100,check)
root.after(100,check)
And in the main file, i have added the above code which continusly check if exec_log.txt file has any changes, and if changes are there, then it executes them and all so destroys the widget specified in the remove_list.
This is just a temporary solution which in my case helps me to implement the hot reload feature in tkinter.

Dialog box to select option from list in Python on Windows?

I need to create a dialog box that allows user to select one option from a list in Python on Windows using PyWin32 library. PyWin32 has a DialogBox function, but I cannot find any examples how to use it and I never used it before. Could anybody give me some advice?
The window should be something similar to the one below - this has been created using Zenity (scroll bar is unneeded, this has been added by Zenity itself; I'm perfectly fine with a window that just lists the options - there will be no more than about 5-6 of them), but I would rather like to avoid using external tools like Zenity, I also cannot install other libraries on the system except PyWin32 that is already installed.
Have to reply to myself :). Within the directory where PyWin32 is installed, there is a file pythonwin\pywin\dialogs\list.py that contains a sample class ListDialog implementing exactly such a dialog. It can be either used directly "as is", with some code like following:
import pywin.dialogs.list
result=pywin.dialogs.list.SelectFromList('Select level', ['standard', 'advanced', 'expert'])
print(result)
or it can be copied to a separate file and modified to change the style/layout/behavior of the window and imported from the modified file.

python 3 create a GUI to set directory in a program

I would like to create a GUI that pops up asking where to download a file using python. I would like it to be similar to the interface that Google Chrome uses when downloading a file as that looks pretty standard. Is there a default module or add on that I can use to create thus GUI? or will I have to create myself? any help would be appreciated.
If You mean the dialog window, in which You chose where to put file, it's tkinter.filedialog (https://docs.python.org/3/library/tkinter.html), which would give the most native look and feel.
But if You mean the dialog, in which You chose whether to save file in default location or specify another one, there's no such widget, but You may build it on Your own. For that case You probably should dig into Chromium sources, to determine how exactly it acts(https://chromium.googlesource.com/)
There are a number of GUI toolkits you could use, including:
Kivy (modern touch-enabled)
Tkinter (bundled with Python)
These have file chooser widgets, which you could use that would provide standard-looking interfaces to your file system.
How do you want to run this program?

Python Tkinter: Adding widgets to file dialogs

I am using Tkinter with Python 2.6 and 2.7 for programming graphic user interfaces.
These User Interfaces contain dialogs for opening files and saving data from the tkFileDialog module. I would like to adapt the dialogs and add some further entry widgets e.g. for letting the user leave comments.
Is there any way for doing so?
It seems that the file dialogs are taken directly from the operating system. In Tkinter they are derived from the Dialog class in the tkCommonDialog module and call the tk.call("tk_getSaveFile") method of a frame widget (in this case for saving data).
I could not find out where this method is defined.
call method is defined in _tkinter.c, but there is nothing interesting for your particular task there. It just calls a Tcl command, and the command tk_getSaveFile does all the work.
And yes, when there is a native file dialog on the operating system, tk_getSaveFile uses them (e.g. GetSaveFileName is used on Windows). It could be possible to add widgets there, but not without tampering with C sources of Tk. If you're sure that your target uses non-native Tk dialogs, you could add something to its widget hierarchy by hacking ::tk::dialog::file:: procedure from Tk (see library/tkfbox.tcl).
I would rather take an alternative implementation of tk_getSaveFile, written in pure Tcl/Tk and never using the OS facility. This way, we can be sure that its layout is the same for all OSes, and it won't suddenly change with a new version of Tk. It's still far from trivial to provide a convenient API for python around it, but at least, it is possible.
I had to get rid of the canvasx/y statements. That line now simply reads set item [$data(canvas) find closest $x $y], which works well. $data(canvas) canvasx $x for its own works well but not in connection with find closest, neither if it is written in two lines.

Drag drop file onto Python script

I'm looking for a cross-platform way of making my Python script process a file's path by implementing a drag n drop method. At the moment I manually go to the terminal and use the sys.argv method:
python myscript.py /Python/myfile.xls
However this is slow and "techy". Ideally I would a quick and interactive way of allowing a file be processed by my Python script. I primarily need this to work for Mac but cross-platform would be better.
If you want to use Tkinter, have a look at the Tkinter DnD binding from here http://klappnase.bubble.org/TkinterDnD/index.html
When run, the binding shows an example with a listbox that allows you to drag a file on to it.
Do you want to drag and drop the myfile.xls onto your python script within your file navigator ? Say Finder or whatever on Mac, Explorer on Win, Nautilus etc. ? In that case there will not be a simple cross-platform solution, given that you will have to hook into different software on different systems.
For a Mac specific solution try AppleScript - here is a sample
And for something Pythonic there is http://appscript.sourceforge.net/ , http://docs.python.org/library/macosa.html
Otherwise the solution is in the answer above. Use a custom GUI built in Tk, or wx or QT. You can look up their respective documentation for drag and drop, they do have cross-platform ways of doing it.
It'd be easiest to just write a small GUI with Tkinter or something similar and have the user select a file from within the GUI. Something along these lines:
import tkFileDialog
f = tkFileDialog.askopenfilename()
# Go on from there; f is a handle to the file that the user picked
I'm not aware of any cross platform methods to get a script to work with drag and drop, however. This is probably easier, though.

Categories

Resources