PySimpleGUI: is there any way to create a new folder from FileSaveAs? - python

But I would like for the user, when using a "Save As" function, to also be able to create a new folder in which to put the file.
Is there any way to do this? I've looked through the call reference and the demo programs and don't see anything. FileSaveAs works beautifully for existing folders, but there doesn't seem to be any parameter to get it to make new folders.

Using the sg.SaveAs() chooser button in PySimpleGUI opens a "Save As" dialog box. It has specific options provided for the user to create folders that are not normally shown when browsing for a file to open.
If you wish to create them for the user, you can use os.mkdir prior to showing the dialog.

Related

"Browse file" window freeze code execution in pywinauto

I am working on automating program that requires choosing file path.
After I click the browse button using code:
dlg.child_window(auto_id='btnBrwsBinFile').click()
browse window opens and freeze execution of code and I cannot control the popup window.
I have tried also different approach. I have edited text filed using code:
dlg['Edit'].set_text(path)
but then program do not see the path, it treats the field like it was never edited, like it was empty
I would like to ask if someone solved this issue before.
try:
dlg.child_window(title='btnBrwsBinFile').click_input()
pywinauto documentation:https://pywinauto.readthedocs.io/en/latest/code/pywinauto.findwindows.html
You can use UISpy.exe to get the title, class name

How to open a file in the user's default program on button click with tkinter?

Can you please suggest how to open a file in the user's default program on button click with python's tkinter?
I think that problem occures because that file you are trying to open is not in the same folder as the program you are trying to run. If it is not in same folder you need to put whole path but I recomend you to make folder every time you are making new project and then put all files, images, text documents ect. you will need in that folder.

How can I use the same dialog box in Tkinter to browse and select files and directories?

I am using Tkinter for building a GUI for a python script. I need a button which opens up a dialog box which allows me to select both files and directories.
Till now, I've come across
tkFileDialog.askdirectory(parent=root, title=dirtext1)
which allows just to select directories in the dialog box
and,
tkFileDialog.askopenfilename(parent=root, title=filetext)
which allows me to just select files. As of now, I access these dialog boxes using separate buttons, each of which calls one of these functions.
Is there anyway to select either a file or a folder using a single dialog box?
I don't think so. There is no built-in class to do it easily
Investigation
If you look at the tkFileDialog module's source code, both the Open and the Directory classes inherit from _Dialog, located in tkCommonDialog.
Good so far; these classes are simple and only extend two methods. _fixresult is a hook that filters based on your options (promising), and _fixoptions adds the right tcl parameters (like initial directory).
But when we get to the Dialog class (parent of _Dialog), we see that it calls a tcl command by a given name. The names built-in are "tk_getOpenFile" and "tk_chooseDirectory". We don't have a lot of python-level freedom of the command after this. If we go to see what other tcl scripts are avaliable, we are disappointed.
Looks like your options are
ttk::getOpenFile
ttk::getSaveFile
ttk::chooseDirectory
ttk::getAppendFile
Conclusion
Rats! Luckily, it should be quite easy for you to make your own dialog using listboxes, entry fields, button, (and other tk-builtins), and the os module.
Simple Alternative
From your comments, it looks like a viable simple work-around might be to use
directory = os.path.dirname(os.path.realpath(tkFileDialog.askopenfilename()))
They'll have to select a file, but the "Open" button will "return a path", in the sense that the path is computed from the file location
But I really want it!
If for some reason you really want this behaviour but don't want to remake the widget, you can call tcl scripts directly. It may be possible to copy the code from getOpenFile and provide more loose arguments that allow for selecting a more generic object. This isn't my speciality and seems like a really bad idea, but here is how you call tcl directly and here is where you can learn more about the underlying commands.
I've had a similar issue.
In the end, I used askopenfilenames() (plural) and split the path from the files. Then with a radiobutton, ask the user to choose if they want to process all the files in the directory, or just those they selected.
filetypes = [('All files', '*.*'), ('CSV files', '*.csv'),]
data_list = askopenfilenames(title='Select folder', filetypes=filetypes)
data_dir = data_list[0].rsplit('/', 1)[0]
I mention it because askopenfilenames() doesn't get suggested much, but is closer to selecting a folder, as can ctrl+A all files.

Create button to browse folders and select a file in python

I'm developing a python widget using tkinter where I need a button which will operate like any of the open button we see today. Basically I want it to help users to browse a file, select it and click ok. What i want is , as soon as the user clicks 'ok', it'll copy the file from that location and save it into a particular location in a particular name, which will be then used by rest of my widget.
Thanks in advance.
Take a look at the various widgets in tkFileDialog. They could be very useful. Particularly, you might want askopenfilename which will pop up a dialog where you can browse for a particular file -- When the user hits OK it returns a filename which you can do whatever you want with.

Create Explorer.exe's File - Context Menu in WxPython application

I have a WxPython app that, among other things, has a integrated file-browser.
I want to be able to create a system-default file context menu (e.g. what you get if you right-click on a file in windows explorer) when a user right clicks on one of the items within my application.
Note: I already know how to create my own context menu (e.g. wx.EVT_LIST_ITEM_RIGHT_CLICK), I want the Windows context menu.
To clarify, I do not want, or need to modify the existing system context menu, I want to be able to display it for a specific file within my application.
Basically, I know what was right clicked on, and where the mouse pointer is (if it's needed). I want to create the system context menu there, just like it works in windows explorer.
If you have python win32 installed, then look under the directory <PYTHON>/lib/site-packages/win32comext/shell/demos/servers. This contains a file context_menu.py which has sample code for creating a shell extension.
Update: I think you want the folder_view.py sample.

Categories