0

I have the following problem: Every time I try to open a file via the QFileDialog, it appears 3 times in a row. Somehow it doesn't make sense. Do I need a condition for this? sorry for the question, but I couldn't find anything in the documentation on this matter. So in the end, when I click the open button, I just want the dialog to appear only once when I select a file

here is the code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(846, 564)
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.openBtn = QtWidgets.QPushButton(Form)
        self.openBtn.setObjectName("openBtn")
        self.verticalLayout.addWidget(self.openBtn)
        self.textEdit = QtWidgets.QTextEdit(Form)
        self.textEdit.setObjectName("textEdit")
        self.verticalLayout.addWidget(self.textEdit)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.openBtn.setText(_translate("Form", "open"))

class MainWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)

        self.openBtn.clicked.connect(self.on_openBtn_clicked)

    def on_openBtn_clicked(self):
        filename = QtWidgets.QFileDialog.getOpenFileName(filter="Some Files (*.*)")[0]
        if filename:
            print(filename)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    app.exec()


BanAnn
  • 159
  • 1
  • 7

0 Answers0