0

I try to read dataframe from excel file and print it after button click.

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]))
8
  • Qt tries to use the native file dialog when calling QFileDialog static methods. This might result in some warning messages like yours, which should not be a problem and can normally be ignored. Besides the error, does your program work as expected? Commented Oct 16, 2020 at 22:32
  • No. I wanted to open file and print it. And I failed Commented Oct 16, 2020 at 22:45
  • Sorry but "I failed" is not an useful answer. What failed? Does the dialog show? Commented Oct 16, 2020 at 22:46
  • I don't get opened file. I can open open Excel file with pandas read_Excel function. But my aim is to open Excel table with getopenfilename function Commented Oct 16, 2020 at 22:52
  • I didn't ask that. Please answer the question: Does the file dialog show? If so, what is the output, besides the Gtk warning? Commented Oct 16, 2020 at 22:57

1 Answer 1

1

The Gtk warning is just what it is: a warning. 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.

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.

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. 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.