Я новичок в pyqt5, и мне нужна помощь с закрытием диалогового окна файла в моем приложении. Пользовательский интерфейс создан с использованием QT Designer. Когда я выбираю файл и нажимаю кнопку открытия, диалоговое окно файла сначала закрывается, а затем снова открывается. Это моя проблема. Не должен открываться снова.
Я пытался использовать следующие функции в диалоговом окне fileDialog.close()
и fileDialog.hide()
, но не смог заставить его работать должным образом.
Я использую два разных файла: один для главного окна, а другой - для диалога файлов. Из главного окна я использую следующий класс
class Main (QtWidgets.QMainWindow):
def __init__(self):
super(Main, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.btnChooseFile.clicked.connect(self.chooseFile)
def chooseFile(self):
fileDialog = OpenFileDialog.FileDialog()
fileDialog.openFileNameDialog()
app = QtWidgets.QApplication([])
win = Main()
win.show()
sys.exit(app.exec())
Другой класс выглядит как OpenFileDialog.py и выглядит так:
class FileDialog(QWidget):
def __init__(self):
super(FileDialog, self).__init__()
self.title = 'Choose image-file'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.openFileNameDialog()
self.openFileNamesDialog()
self.saveFileDialog()
self.show()
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
if fileName:
print(fileName)
def openFileNamesDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileNames(self,"QFileDialog.getOpenFileNames()", "","All Files (*);;Python Files (*.py)", options=options)
if files:
print(files)
def saveFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(self,"QFileDialog.getSaveFileName()","","All Files (*);;Text Files (*.txt)", options=options)
if fileName:
print(fileName)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FileDialog()
sys.exit(app.exec_())
Файл пользовательского интерфейса выглядит следующим образом:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>winMain</class>
<widget class="QMainWindow" name="winMain">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>280</width>
<height>320</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>280</width>
<height>320</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>280</width>
<height>320</height>
</size>
</property>
<property name="windowTitle">
<string>OCR</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="btnChooseFile">
<property name="geometry">
<rect>
<x>80</x>
<y>10</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Choose file</string>
</property>
</widget>
<widget class="QLabel" name="lblFile">
<property name="geometry">
<rect>
<x>40</x>
<y>40</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p><span style=" font-weight:600;">File:</span></p></body></html></string>
</property>
</widget>
<widget class="QLabel" name="lblFilePath">
<property name="geometry">
<rect>
<x>40</x>
<y>60</y>
<width>221</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string><html><head/><body><p><span style=" font-style:italic;">FilePath</span></p></body></html></string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>280</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Как я могу это исправить?
EDIT
Я исправил проблему, убрав следующую строку кода в функции chooseFile:
fileDialog.openFileNameDialog()