I am using static method:
path = QtGui.QFileDialog.getSaveFileName(self, SAVE_TO_STR, NAME_STR, 'CSV(*.csv)')
where I get path as full_path\some_name.csv
but I need to set different language to buttons and labels of dialog, so I've been looking at docs and find out that I can't do that with static method and I've come up with this code:
ddd = QtGui.QFileDialog(self, SAVE_TO_IN_OTHER_LANGUAGE_STR, NAME_STR, 'CSV(*.csv)')
ddd.setAcceptMode (QtGui.QFileDialog.AcceptSave)
ddd.setLabelText( QtGui.QFileDialog.Accept, "Save - in other language" )
ddd.setLabelText( QtGui.QFileDialog.Reject, "Cancel - in other language" )
ddd.setLabelText( QtGui.QFileDialog.LookIn, "Look in - in other language" )
if ddd.exec_():
path = QtCore.QString(ddd.selectedFiles()[0])
I am trying to set it to look like first one so my questions are:
path I get is ok, but missing .csv at the end, so it saves file with no extension.
should I manually add .csv at the end of the path?
when I choosing where to save and click on folder, "save" button turns to "open". How to change that button text to "Open" in other language?
folders list at the left side of dialog is not complex as when I use QtGui.QFileDialog.getSaveFileName() , it shows only My Computer and User, instead of modern tree with favorites and partitions under My Computer.
1) path I get is ok, but missing .csv at the end, so it saves file with
no extension. should I manually add .csv at the end of the path?
Answer: I think you shouldn't manually add .csv at the end of the path. In PyQt API have this solution to solve it, use QFileDialog.setDefaultSuffix (self, QString suffix);
pathQFileDialog = QtGui.QFileDialog(self)
pathQFileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
pathQFileDialog.setNameFilter('CSV(*.csv)')
pathQFileDialog.setDefaultSuffix('csv')
Reference: http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#setDefaultSuffix
2) when I choosing where to save and click on folder, "save" button
turns to "open". How to change that button text to "Open" in other
language?
Answer: My opinion of PyQt, No. In Qt (C++) at file qfiledialog.cpp I found your problem in method void QFileDialogPrivate::_q_updateOkButton() at line between 2886 and 2888. It force "&Open" label;
button->setEnabled(enableButton);
if (acceptMode == QFileDialog::AcceptSave)
button->setText(isOpenDirectory ? QFileDialog::tr("&Open") : acceptLabel);
Reference: https://qt.gitorious.org/qt/qt/source/57756e72adf2081137b97f0e689dd16c770d10b1:src/gui/dialogs/qfiledialog.cpp#L2796-2888
3) folders list at the left side of dialog is not complex as when I use
QtGui.QFileDialog.getSaveFileName() , it shows only My Computer and
User, instead of modern tree with favorites and partitions under My
Computer.
Answer: Because on Windows, Mac OS X and Symbian^3, this static function (QtGui.QFileDialog.getSaveFileName()) will use the native file dialog and not a QFileDialog.
Reference: pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#getSaveFileName
Regards,
Related
I am trying to create a way using pywinauto to select multiple files in Win Explorer.
The below code works great to select/get one file, but I am stuck on using the same approach to select multiple files.
The code is part of a method which takes a passed path, then splits the path string to get the filename. I use get_item(filename) to select the file. The last step is a right-click on the file.
if os.path.isdir(fso_to_select):
item_to_select = fso_to_select.split("\\")[-1]
self.__explorer_win = self.__application.Window_(top_level_only=True, active_only=True,
class_name='CabinetWClass')
self.__explorer_win.set_focus().maximize()
fso_item = self.__explorer_win.ItemsView.get_item(item_to_select)
fso_item.right_click_input()
I am trying to use the browse function in PySimpleGUI. I have used PySimpleGUI file browser specific file type to find out how to browse. However there are two file types that I want to choose from and multiple files need to be selected for this to work. My question:
How do you use the browse function for browsing two types of files? and also How do you allow multiple files to be browsed? and finally How do you tell each file apart?
I know that there is a key function for getting data but how can I do that for more than one file.
In case you are a tiny bit confused:
The user must select the browse function and must be able to choose from .txt and .Docx files while selecting more than one file. The program must be able to tell the difference between the files so a function can be run on each file separately.
My code so far:
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.FileBrowse(file_types=(("Text Files", "*.txt"),))],
[sg.Button('Lets GO!!!')]
]
# Create the Window
window = sg.Window('Test', layout).Finalize()
window.Maximize()
Can someone finish this code?
According to the documentation:
file_types=(("Text Files", "*.txt"),("CSV Files", "*.csv"),)
works.
I am trying to use PyQt to open a file dialog and then allow the user to select a new location to create a new directory which will be used in the program.
Currently my code looks like this:
dialog = QFileDialog()
dialog.setOption(QFileDialog.ShowDirsOnly, True)
dialog.setWindowTitle(title)
dialog.setAcceptMode(QFileDialog.AcceptOpen)
dialog.setNameFilter(nameFilter)
dialog.setFileMode(QFileDialog.Directory)
if dialog.exec_() == QFileDialog.Accepted:
return dialog.selectedFiles()[0]
However in the file dialog all the files are still shown and their is no option to select a directory if the user want to overwrite it.
The wanted outcome would show just the directories in the explorer.
Is there a way of doing this using PyQt file dialogs?
I have a GUI with a tool button next to a Combobox. The tool button opens a directory browser to select a file and then adds that file Name to the Combobox drop-down list.
def showFileOpenDialogLoad(self):
""" Opens dialog to get Load file path """
filename = QFileDialog.getOpenFileName(self, 'Open file',
'/home')
self.comboBoxFilePathLoad.addItem(filename)
This works perfectly, however, I then want the Combobox to directly display the last added filename. I have tried using .insertItem(0,filename)instead and then setting self.comboBoxFilePathLoad.currentIndex = -1but it still displays the first added filename even though in the Dropdown list the last added filename is now above the old and displayed one. Apparently in C# you would use cmbBox.SelectedIndex = cmbBox.Items.count -1 but Pyqt does not have a SelectedIndexmethod.
This does not seem like such a difficult question but somehow I can not find a solution to it online... The QComboBox Class Reference does not explain count and CurrentIndex either. Thank you for your help!
I'm writing a small Python code to join text files, and the files are selected as an user input. But it's important that I get the order of the users selection, as I want to join the files in the selected order. But I see that the list returned by getOpenFileNames does not preserve the selection order.
Does anybody have any suggestion to capture the selection order?
Thank you.
I originally wanted to suggest writing a callback for the currentChanged signal that tracks the selection, but it seems like this signal doesn't get called when using getOpenFileNames. An alternative would be displaying the dialog with show() and connecting a callback to filesSelected, which is called after the user clicks the "open" button on the dialog. The argument to the callback is a list with the selected files which seems to be in the order of their selection (just tested it on python3/pyqt4).
def callback(files):
joined_files = ''.join([open(f).read() for f in files])
do_something_with(joined_files)
dialog = QtGui.QFileDialog()
dialog.setFileMode(3) #allow selection of multiple files
dialog.filesSelected.connect(callback)
dialog.show()
One problem with this is that the order isn't indicated to the user in an easy way - the "file" textbox contains the ordered files' names, but this is messy when you select more than a few files. A better but slightly more complicated approach would be building a widget or dialog with a FileDialog for selecting files and a List/TableWidget holding the files to process, where the user could add Files one at a time. This would allow for a better overwiew as well as easy selection of files from multiple directories and better extendability (e.g. filtering, rearranging, sorting the selection).
self.filename = QtGui.QFileDialog.getOpenFileNames(
self,
"Cargar tu documento",
self.lastOpenedFile,
"*.doc;*.odt;*.pdf" )