由于 if 语句,无法在 PyQt5 中调用消息框



我使用的是Python 3和PyQt5。我使用QFileDialog.getOpenFileName在我的程序中打开不同的文件,但不超过4个,如果你试图打开另一个文件,但似乎无法打开,我想显示一个警告消息框。

import pandas
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog, QMessageBox
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QIcon
class MainWindow(QMainWindow):
def __init__ (self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(640, 480))
self.setWindowTitle("'File' test")
self.count = 0
self.datasets = []

openFile = QAction(QIcon('open.png'), '&Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open documents')
openFile.triggered.connect(self.openCall)

menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
fileMenu.addAction(openFile)

def openCall(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileName(self, "Select one file to open", "C:PROGFIN", "CSV files (*.csv)", options=options)
if files:
if self.count < 4:
self.datasets.append(files)
self.count += 1
print(self.datasets)
print(self.count)
else:
# The message box should go here

只需添加

QMessageBox.warning(self, "Title", "Description")

最新更新