0

I am trying to call "def getfile(self,text)" and every time it returns "argument 1 has unexpected type "str"

How do I avoid that? Here is the code:

from colorama import Fore, Back, Style
import configparser
from configobj import ConfigObj
from pyexcel import save_as
from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_DE(object):
        def setupUi(self, DE):
            DE.setObjectName("DE")
            DE.resize(1149, 517)
            font = QtGui.QFont()
            font.setPointSize(10)
            DE.setFont(font)
            self.GermanyBrowse = QtWidgets.QPushButton(DE)
            self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
            self.GermanyBrowse.setObjectName("GermanyBrowse")
            self.GermanyBrowse.clicked.connect(self.getfile(DE))

        def getfile(self,text):
            filter = "Excel (*.xlsx *.xls *.xlsm *.csv)"
            fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
                None,
                "Select file",
                "",
                filter)
            config = ConfigObj("Path.ini", encoding='latin-1')
            config["Path"][f"{text}"] = fileName
            config.write()
            return fileName

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    DE = QtWidgets.QDialog()
    ui = Ui_DE()
    ui.setupUi(DE)
    DE.show()
    sys.exit(app.exec_())

And what am I doing wrong that makes this not work? Thank you!

Radu
  • 35
  • 7
  • 1
    Post your traceback please. Also, I think you meant to do `self.GermanyBrowse.clicked.connect(self.getfile)` nothing more. – Torxed Jan 30 '19 at 15:06

1 Answers1

1

Try it:

#from colorama import Fore, Back, Style
#import configparser
#from configobj import ConfigObj
#from pyexcel import save_as

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_DE(object):
    def setupUi(self, DE):
        DE.setObjectName("DE")
        DE.resize(1149, 517)
        font = QtGui.QFont()
        font.setPointSize(10)
        DE.setFont(font)
        self.GermanyBrowse = QtWidgets.QPushButton("MyButton", DE)
        self.GermanyBrowse.setGeometry(QtCore.QRect(240, 60, 91, 21))
        self.GermanyBrowse.setObjectName("GermanyBrowse")

        #self.GermanyBrowse.clicked.connect(self.getfile)       # (DE)     <---vvvvv
# +++
        self.GermanyBrowse.clicked.connect(lambda ch,text=self.GermanyBrowse.text() : self.getfile(text))

    def getfile(self, text):

        print("\ntext ------>", text)                            # +++

        filter = "Excel (*.xlsx *.xls *.xlsm *.csv)"
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
            None,
            "Select file",
            "",
            filter)

        print("fileName -->", fileName)  

#            config = ConfigObj("Path.ini", encoding='latin-1')
#            config["Path"][f"{text}"] = fileName
#            config.write()

        return fileName

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    DE  = QtWidgets.QDialog() #QDialog() QWidget() 
    ui  = Ui_DE()
    ui.setupUi(DE)
    DE.show()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • 1
    If OP wants to preserve the argument `DE`, I suggest to have a look at this: https://stackoverflow.com/questions/6784084/how-to-pass-arguments-to-functions-by-the-click-of-button-in-pyqt – Torxed Jan 30 '19 at 15:30
  • If i do this, I would need to use the saved variable (fileName) in another file, in another function. And I could not figure out a way to save that variable and call it to another file, in another function. Is there a way? Thank you! – Radu Jan 30 '19 at 19:57
  • Thank you! It works. But before closing the question I wanted to ask you if there is any way to run this script and while this script is running, after using the browse button, to use the saved path as a variable in another file. Thanks so much for your help. – Radu Jan 31 '19 at 11:20