Jeffrey Whitney
Jeffrey Whitney

Reputation: 25

Trying to use QFileDialog in pyQT6 to OPEN a file, but it keeps giving me the Save dialog

I am trying to use QFileDialog in PyQT6 to open a file, but it keeps giving me the Save dialog, so that when I select a file it asks me whether or not I want to overwrite it. What am I doing wrong here?

from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton, QLabel, QFileDialog
from PyQt6 import uic
import sys

fname = QFileDialog.getOpenFileName(
    self,
    "Open File",
    "c:\\gui\\images",
    "All Files (*);;Python Files (*.py);; PNG Files (*.png)",
)

Upvotes: 1

Views: 7446

Answers (1)

user16776498
user16776498

Reputation:

Your issue is probably elsewhere in your code, next time please add more information.


Here is a simple working example:

from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton, QFileDialog
from PyQt6.QtCore import pyqtSlot
import sys


class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        btn = QPushButton(self)
        btn.setText("Open file dialog")
        self.setCentralWidget(btn)
        btn.clicked.connect(self.open_dialog)
    
    @pyqtSlot()
    def open_dialog(self):
        fname = QFileDialog.getOpenFileName(
            self,
            "Open File",
            "${HOME}",
            "All Files (*);; Python Files (*.py);; PNG Files (*.png)",
        )
        print(fname)
        
    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_gui = Main()
    main_gui.show()
    sys.exit(app.exec())

Upvotes: 1

Related Questions