我试图调用“ def getfile(self,text)”,并且每次返回“参数1的类型均为意外” str
如何避免这种情况? 这是代码:
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_())
我在做什么错而导致此操作不起作用?谢谢!
答案 0 :(得分:1)
尝试一下:
#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_())