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)
Related
I'm new to python and I don't know much yet. So, I wanted to convert my python project to an exe file, and so I followed the steps like in every youtube tutorial, terminal, pyinstaller and stuff (I use Pychram), but soon I realized that the icon I set for my exe isn't loading unless it's in the same folder as dist and .spec file. I decided to search some, I found a way on this site: https://clay-atlas.com/us/blog/2020/11/04/python-en-package-pyinstaller-picture/ (3rd method), but i was unable to decode it, I just need to replace something with my filename but I don't understand where exactly, like is pic2str a variable or a project name? Thanks.
(Code might be slightly shifted in here copy from the website if you need)
import base64
def pic2str(file, functionName):
pic = open(file, 'rb')
content = '{} = {}\n'.format(functionName, base64.b64encode(pic.read()))
pic.close()
with open('pic2str.py', 'a') as f:
f.write(content)
if __name__ == '__main__':
pic2str('test.png', 'explode')
And this:
import sys
import base64
from io import BytesIO
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from test import Ui_MainWindow
from PIL import Image, ImageQt
from pic2str import explode
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Load byte data
byte_data = base64.b64decode(explode)
image_data = BytesIO(byte_data)
image = Image.open(image_data)
# PIL to QPixmap
qImage = ImageQt.ImageQt(image)
image = QPixmap.fromImage(qImage)
# QPixmap to QLabel
self.ui.label.setPixmap(image)
self.ui.label.setScaledContents(True)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
sys.exit(app.exec_())
I found a solution,
you just have to use
--onefile
so the .exe file doesn't require any connection to the folder, and use
--icon insertpicturename.ico
to make the file have the icon you need, you can convert an image to .ico using online converters. And, I would also recommend using
--noconsole
if it fits your needs, it just removes the console showing up before opening the file.
you can try auto-py-to-exe, its a convenient little script that will allow you to use pyinstaller from a GUI and it will create the required terminal command for you. you can get it using pip install auto-py-to-exe and launch it from your terminal with the command auto-py-to-exe, you will see a section called Icon where you can open a .ico file through a file broswer window
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()
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.
How can I make a popup QInputDialog without a parent Application using Pyside?
When the user double clicks this python file I want it to display the Input dialog and that's it.
import sys
import os
from PySide import QtGui, QtCore
def network_copy():
text, ok = QtGui.QInputDialog.getText(None, 'Test | Network Copy', 'Enter name')
if ok and text:
print 'Great'
network_copy()
If you run your example, it will print a debug message like this: QWidget: Must construct a QApplication before a QPaintDevice. So, obviously, this is just not possible.
I'm guessing that all you want to do is avoid having to start an event-loop. However, this is not an issue with dialogs, because they run their own event-loops. So just create the QApplication before showing the dialog:
app = QtGui.QApplication(sys.argv)
network_copy()
i have a link in my QWebkit, which points to a pdf file.
But when the link is clicked, it can't display the pdf file.
is there a way to make it happen?
If you enable plugins through QWebSettings and have a PDF viewer installed that provides a browser plugin such as Acrobat then you should see the PDF rendered using the plugin inside your QWebView:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/test/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())
This code isn't working for me on Windows with the latest version of Acrobat X (it just shows a progress bar but no PDF - proof that the plugin is loading, just not working) but I'm sure this is how I've done it before. Give it a try and let me know.
Webkit doesn't include a PDF viewer. You'll need to have some way of rendering it - whether you pass it off to a different viewer (Adobe PDF viewer or something else), render it in the control in some way you devise (you could even try rendering it in JavaScript for fun).
This is Gary Hudges code for PyQt5:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtCore import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/data/progetti_miei/python/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())