简体   繁体   中英

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)",
)

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
import sys


class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        btn = QPushButton(self)
        btn.setText("Open file dialog")
        self.setCentralWidget(btn)
        btn.clicked.connect(lambda: self.open_dialog())

    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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM