Posts: 741
Threads: 122
Joined: Dec 2017
Hi,
In a win11 python 10.x environment, I can start the file explorer from within python like so:
import os
def show_explorer(path):
os.startfile(path)
show_explorer(os.path.abspath("c:/windows/explorer.exe")) Almost what I want. I would like it to open on a specific directory outside python.
Why?
The setup: inside python resides a tree and each extremity is a directory.
I want to click on a tree node, automatically navigate to the directory and manipulate it. (paste a pdf, click on a pdf, view a pic...etc)
If I do this inside tKinter, I cannot manipulate the directory.
Can this be done?
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 2,128
Threads: 11
Joined: May 2017
You can use different kinds of filedialog with tkinter .
There are different types of filedialog : - filedialog which returns a
str to a file or directory
- filedialog which returns a
list with str
- filedialog which returns an open FileObject
- filedialog which returns a
list with open FileObjects
You should also read this: dialog.html#module-tkinter.filedialog
Demo Code:
from io import IOBase
from pathlib import Path
from tkinter import Tk
from tkinter.filedialog import (
askdirectory,
askopenfile,
askopenfilename,
askopenfilenames,
askopenfiles,
asksaveasfile,
asksaveasfilename,
)
from tkinter.messagebox import showinfo
dialog_functions = (
askdirectory,
askopenfile,
askopenfilename,
askopenfilenames,
askopenfiles,
asksaveasfile,
asksaveasfilename,
)
file_types = (
("text files", "*.txt"),
("video files", ["*.mp4", "*.mkv", "*.avi"]),
("All files", "*.*"),
)
showinfo(
"Info",
"Visit https://docs.python.org/3/library/dialog.html#module-tkinter.filedialog",
)
for func in dialog_functions:
# directory has no filetypes
title = f"{func.__name__}: {func.__doc__}"
if "directory" in func.__name__:
ret_val = func(
title=title,
initialdir=Path.home().joinpath("Downloads"),
)
else:
# it's a file dialog, so it knows filetypes
ret_val = func(
title=title,
initialdir=Path.home().joinpath("Downloads"),
filetypes=file_types,
)
showinfo("Title", f"Return value of {func.__name__}\n\n{ret_val}")
# closing the FileObject if it's an instance of FileObject
if isinstance(ret_val, IOBase):
ret_val.close()
showinfo("File closed", f"The file '{ret_val.name}' has been closed.")
elif isinstance(ret_val, list):
for file_obj in ret_val:
if isinstance(file_obj, IOBase):
file_obj.close()
showinfo("Files closed", "The files has been closed")
Posts: 741
Threads: 122
Joined: Dec 2017
Aug-04-2022, 09:10 AM
(This post was last modified: Aug-04-2022, 09:11 AM by DPaul.)
dear DeaD_EyE,
I am familiar with the tKinter filedialogs.
All I need is to add some parameter to this line:
show_explorer(os.path.abspath("c:/windows/explorer.exe")) So that it goes to a specific dir.
I do not need a filedialog to search for it, i know the one I want.
But maybe that is impossible.
Thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 4,802
Threads: 77
Joined: Jan 2018
Aug-04-2022, 09:25 AM
(This post was last modified: Aug-04-2022, 10:00 AM by Gribouillis.)
(Aug-04-2022, 09:10 AM)DPaul Wrote: But maybe that is impossible. You could probably start the explorer with the subprocess module and convenient arguments. Use this page for the syntax of the arguments.
Also I don't use Windows but I heard it is getting rid of Internet Explorer. Upgrade to Linux.
Posts: 2,128
Threads: 11
Joined: May 2017
Don't use os.startfile nor os.system for this.
from subprocess import Popen
def select_object(path):
return Popen(["explorer.exe", f"/select,{path}"])
if __name__ == "__main__":
select_object(r"C:\Users") If you want more control: https://www.apriorit.com/dev-blog/727-wi...ith-python
(I don't read this, because I'm a Linux user, I don't care much about Windows)
carecavoador likes this post
Posts: 741
Threads: 122
Joined: Dec 2017
(Aug-04-2022, 09:56 AM)DeaD_EyE Wrote: Don't use os.startfile nor os.system for this. Bingo! It works!
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 741
Threads: 122
Joined: Dec 2017
(Aug-04-2022, 09:25 AM)Gribouillis Wrote: Also I don't use Windows but I heard it is getting rid of Internet Explorer. Upgrade to Linux. Really ?
When the animals still could speak, some people were already telling me "Unix is the future!"
Now it's linux.
I'll give you this: I regret DOS. It was wonderful.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 4,802
Threads: 77
Joined: Jan 2018
(Aug-04-2022, 03:22 PM)DPaul Wrote: I regret DOS. It was wonderful. I understand that, DOS was not yet a spyware!
Posts: 2,128
Threads: 11
Joined: May 2017
Windows is actually quite ok, if there is no built-in spyware.
The other problem with Windows 11 is the coming loss of control.
Soon, Windows will be like the Google Play Store.
Application violates against policy => will be automatically deleted.
You won't be able to install applications the traditional way in a few years.
Only from the MS App Store.
I don't expect Microsoft to exist in its current form in the future.
We will see.
Posts: 741
Threads: 122
Joined: Dec 2017
OK guys, I was only joking.
For those who remember, I had a brief encounter with OS/2.
Made unix look attractive for a moment.
Luckily "windows for workgroups" came along.
Everybodu happy.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
|