从excel文件读取PyQT中的数据时出错:GtkDialog在没有临时父级的情况下映射.这是不鼓励的



我尝试从excel文件中读取数据帧,并在单击按钮后打印。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon
from excel_reading import *

class MyWin(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.hello)
def hello(self):
data_input_from_file = QtWidgets.QFileDialog.getOpenFileName(self,'header','filename','Excel (*.xlsx *.xls)')
print(data_input_from_file)

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())

当我点击按钮时,我收到这样的消息:

Gtk-Message: 00:03:53.573: GtkDialog mapped without a transient parent. This is discouraged.
('', '')

我应该如何解决这个问题?

I solved the problem: 
def hello(self):
data_input_from_file = QtWidgets.QFileDialog.getOpenFileName(self, 'header', 'filename', 'Excel (*.xlsx *.xls)')
print(type(data_input_from_file))
print(data_input_from_file)
print(pd.read_excel(data_input_from_file[0]))
Gtk警告就是这样:一个警告。你可以忽略这一点。Qt尽可能使用系统本机文件对话框,这可能会导致控制台输出中出现一些警告。

您的问题与其他问题有关:PyQt函数不返回与官方Qt(C++(文档中报告的相同签名的情况非常罕见。

QFileDialog静态方法就是这样的情况之一,因为QFileDialog.getOpenFileName()总是返回一个元组:所选的文件路径和所选的文件类型过滤器。这一点从您的代码输出中也很清楚(我想这是由于取消对话框引起的(:

('', '')

第一个值是选定的文件(在本例中为none(和筛选器(同样为none,因为没有选定的文件(。

解决方案是为静态返回的内容分配两个值:

filePath, filters = QtWidgets.QFileDialog.getOpenFileName(
self,'header','filename','Excel (*.xlsx *.xls)')
if filePath:
# do something

最新更新