PyQt5 - 在 QFileDialog 上单击取消关闭应用程序
PyQt5 - Clicking Cancel on QFileDialog Closes Application
我注意到,使用以下代码,如果您选择单击 "Import File" 的文件对话框上的 "Cancel",整个应用程序将关闭,而不是 return菜单并等待不同的选择。我怎样才能把它放到 return 到 mainMenu?
(注意:我注意到如果我在初始文件对话框调用后不放任何东西,它运行良好。)
代码:
import sys
import xml.etree.ElementTree as et
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainMenu(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QGridLayout()
# Create Objects for Main Menu
logoLabel = QLabel("TESTAPP")
logoFont = QFont("Broadway", 48)
logoLabel.setFont(logoFont)
versionLabel = QLabel("Version 0.1a")
copyrightLabel = QLabel("Copyright 2016")
importButton = QPushButton("Import File")
quitButton = QPushButton("Quit")
# Set Locations of Widgets
layout.addWidget(logoLabel, 0, 0)
layout.addWidget(versionLabel, 1, 0)
layout.addWidget(copyrightLabel, 2, 0)
layout.addWidget(importButton, 4, 0)
layout.addWidget(quitButton, 5, 0)
self.setLayout(layout)
self.setWindowTitle("NESSQL")
# Connect Buttons to Actions
importButton.clicked.connect(self.importFile)
quitButton.clicked.connect(self.close)
def importFile(self):
# Open dialog box to get the filename
file = QFileDialog.getOpenFileName(self, caption="File to Import", directory=".",
filter="All Files (*.*)")
data = et.parse(file)
root = data.getroot()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = MainMenu()
mainMenu.show()
app.exec_()
通过保持检查文件是否为空的条件可以避免这个问题。
你的情况是这样的。
def importFile(self):
# Open dialog box to get the filename
file = QFileDialog.getOpenFileName(self, caption="File to Import",
directory=".",filter="All Files (*.*)")
if not file[0] == "":
data = et.parse(file)
root = data.getroot()
saran vaddi 的回答对我没有用,但这个确实有用:
def importFile(self):
# Open dialog box to get the filename
file = QFileDialog.getOpenFileName(self, caption="File to Import", directory=".", filter="All Files (*.*)")
# If file is blank, simply return control to calling function
if not file:
return
我注意到,使用以下代码,如果您选择单击 "Import File" 的文件对话框上的 "Cancel",整个应用程序将关闭,而不是 return菜单并等待不同的选择。我怎样才能把它放到 return 到 mainMenu?
(注意:我注意到如果我在初始文件对话框调用后不放任何东西,它运行良好。)
代码:
import sys
import xml.etree.ElementTree as et
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MainMenu(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QGridLayout()
# Create Objects for Main Menu
logoLabel = QLabel("TESTAPP")
logoFont = QFont("Broadway", 48)
logoLabel.setFont(logoFont)
versionLabel = QLabel("Version 0.1a")
copyrightLabel = QLabel("Copyright 2016")
importButton = QPushButton("Import File")
quitButton = QPushButton("Quit")
# Set Locations of Widgets
layout.addWidget(logoLabel, 0, 0)
layout.addWidget(versionLabel, 1, 0)
layout.addWidget(copyrightLabel, 2, 0)
layout.addWidget(importButton, 4, 0)
layout.addWidget(quitButton, 5, 0)
self.setLayout(layout)
self.setWindowTitle("NESSQL")
# Connect Buttons to Actions
importButton.clicked.connect(self.importFile)
quitButton.clicked.connect(self.close)
def importFile(self):
# Open dialog box to get the filename
file = QFileDialog.getOpenFileName(self, caption="File to Import", directory=".",
filter="All Files (*.*)")
data = et.parse(file)
root = data.getroot()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = MainMenu()
mainMenu.show()
app.exec_()
通过保持检查文件是否为空的条件可以避免这个问题。 你的情况是这样的。
def importFile(self):
# Open dialog box to get the filename
file = QFileDialog.getOpenFileName(self, caption="File to Import",
directory=".",filter="All Files (*.*)")
if not file[0] == "":
data = et.parse(file)
root = data.getroot()
saran vaddi 的回答对我没有用,但这个确实有用:
def importFile(self):
# Open dialog box to get the filename
file = QFileDialog.getOpenFileName(self, caption="File to Import", directory=".", filter="All Files (*.*)")
# If file is blank, simply return control to calling function
if not file:
return