简体   繁体   English

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

[英]Errors while reading data in PyQT from excel file: GtkDialog mapped without a transient parent. This is discouraged

I try to read dataframe from excel file and print it after button click.我尝试从 excel 文件中读取 dataframe 并在单击按钮后打印。

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_())

When I click button, I have such message:当我单击按钮时,我有这样的消息:

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

How should I solve that problem?我应该如何解决这个问题?

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]))

The Gtk warning is just what it is: a warning . Gtk 警告就是它的本质:警告 You can ignore that.你可以忽略它。 Qt tries to use the system native file dialogs whenever possible, which might result in some warnings in the consolle output. Qt 尽可能尝试使用系统本机文件对话框,这可能会导致控制台输出中出现一些警告。

Your issue is related to something else: there are rare cases for which PyQt functions don't return the same signature as reported in the official Qt (C++) documentation.您的问题与其他问题有关:在极少数情况下,PyQt 函数返回的签名与官方 Qt (C++) 文档中报告的签名不同。

QFileDialog static methods is one of such cases, as QFileDialog.getOpenFileName() always returns a tuple: the selected file path and the selected file type filter . QFileDialog 静态方法就是这种情况之一,因为QFileDialog.getOpenFileName()总是返回一个元组:选定的文件路径和选定的文件类型过滤器 This is also clear from the output of your code (which I suppose is caused by cancelling the dialog):从您的代码输出中也可以清楚地看出这一点(我认为这是由取消对话框引起的):

('', '')

The first value is the selected file (in this case, none) and filter (again, none, as there was no selected file).第一个值是选定的文件(在本例中为无)和过滤器(同样为无,因为没有选定的文件)。

The solution is to assign two values for what the static returns:解决方案是为静态返回的内容分配两个值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM