PyQt QFileDialog - select file or directory with the same dialog? - python

Is it possible to select either file or directory with the same type of file dialog, using native OS capabilities? Preview on MacOS, for example, can open every valid document in a directory, if only the directory is chosen.
import sys
from PyQt5 import QtWidgets
from PyQt5.Qt import *
app = QtWidgets.QApplication(sys.argv)
selected = QFileDialog.getOpenFileNames(None, "select something")
print(selected)
quit()

Related

Open "Select path" window

In my project I created GUI using Qt Designer. It has few input fields and button that read from those fields and create PDF file. It working fine, but I want to select path through window "Select path" (like on picture).
https://4d.developpez.com/documentation/4DDocV11/IMG/U0067001.PNG
How I can do that? I have been thinkig about those variants:
GUI have special button which open "Select path" window, and pass path to pdf creation function;
"Select path" open automaticaly when I press "Create PDF" button.
Main.py of my project:
import sys
import numpy as np
import window_8
import help_window_dialog
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
from PyQt5.uic import loadUi
from tables2 import pdf_create
from Verification_1 import verification
from window_8 import Ui_MainWindow
from help_window_dialog import Ui_Help_w
class Window(QMainWindow, Ui_MainWindow, Ui_Help_w):
def __init__(self):
super(Window, self).__init__()
self.ui = window_8.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Calc_Button_level_N.clicked.connect(self.Level_N_cal)
self.ui.setNumbers_N.clicked.connect(self.Fill_N)
self.ui.Calc_Button_level_L.clicked.connect(self.Level_L_cal)
self.ui.setNumbers_L.clicked.connect(self.Fill_L)
self.ui.Calc_Button_level_H.clicked.connect(self.Level_H_cal)
self.ui.setNumbers_H.clicked.connect(self.Fill_H)
self.ui.Prin_out_res_N.clicked.connect(self.Out_Res_N)
self.ui.Help_Button_N.clicked.connect(self.open_window)
[some other functions and operations]

Qt Designer error with Python throwing error when I try to load image

I'm trying to load a logo into a python scripts UI. I'm using Qt Designer and I created a label and set pixmap to the image. The image loads fine in the designer but when I import the ui file into the python script I get this error message
C:\Users\Mason\AppData\Local\Continuum\anaconda3\python.exe "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py"
Traceback (most recent call last):
File "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py", line 9, in <module>
UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
File "C:\Users\Mason\AppData\Local\Continuum\anaconda3\lib\site-packages\PyQt5\uic\__init__.py", line 201, in loadUiType
exec(code_string.getvalue(), ui_globals)
File "<string>", line 30
import 3_rc
I still get that error even if I take the image out of the ui file and reload it. What am I doing wrong?
from PyQt5 import QtCore, uic, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PyQt5.QtGui import QIcon, QPixmap
import sys
UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
class MyApp(UIClass, QtBaseClass):
def __init__(self):
UIClass.__init__(self)
QtBaseClass.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushbutton_click)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Qt relies on a resource system for images and other assets. When you add an image in qt designer, it creates a resource file. You have to convert that resource file, just like with a ui file (uic is a shortcut to this process). The autogenerated ui file you get with uic is looking for that resource file, but can't find it.
You can convert your resource file to python with this, depending on your qt version:
pyrcc5 -o 3_rc.py your_resource_file_here.qrc
More info:
Related question: Importing resource file to PyQt code?
https://www.riverbankcomputing.com/static/Docs/PyQt4/resources.html
https://www.riverbankcomputing.com/static/Docs/PyQt5/resources.html

how to import resource file in pyqt5

I have use QtDesigner to turn a picture into a .qrc file, Then I use pyrcc5 to turn this .qrc file into a .py resource:
pyrcc5 source.qrc -o source.py
then I import this source.py file in my .py file. I removed _rc so it become like this
import source
now when I run the .py file itself the picture shows up. But when I run it from a MainPage the picture does not show.
This is my main page:
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
class MainPage(QDialog):
def __init__(self):
super(MainPage,self).__init__()
loadUi('king.ui',self)
app= QApplication(sys.argv)
widget = MainPage()
widget.show()
sys.exit(app.exec_())
So any solution for this ?

Python FileDialog to Get Folderpath not file

I am using Python 3.6. I also use PyQt. I use Qt Designer to develop a form. But I need a code to open a filedialog box, allow the user to navigate to the desired FOLDER (not file) and then click and print/get the folderpath. I cannot find how to do this. I appreciate anyone's help here. Thanks.
You can use the getExistingDirectory() method of QFileDialog by applying the necessary filters:
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
directory = QtWidgets.QFileDialog.getExistingDirectory(None,
"Select Directory",
QtCore.QDir.currentPath())
print(directory)

How to have file and directory dialog in same window in PyQt5?

For now i have two scripts one for file dialog and one of directory dialog. I want to have same option in one scripts in pyqt5. How can i do that?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
def hello():
app=QApplication(sys.argv)
w=QWidget()
w.resize(1100,800)
b=QPushButton(w)
b.move(10,30)
b.clicked.connect(on_click_me)
b.setText("Add_Files")
w.show()
sys.exit(app.exec_())
def on_click_me():
dialog=QFileDialog()
#dialog.setFileMode(QFileDialog.Directory);
#dialog.setFileMode(QFileDialog.AnyFile);
dialog.exec_()
#print(fileName)
hello()
Like this code will allow me to select either files or directory depending upon QFileDialog.Directory or QFileDialog.AnyFile method. Can't i can create something so i can open whatever i want directory or file at the selection time.

Categories

Resources