Python 具有已移交函数参数的方法调用

Python 具有已移交函数参数的方法调用,python,pyqt,widget,pyqt5,Python,Pyqt,Widget,Pyqt5,我目前正在开发一个基于Qt、pyqt和Python作为我语言的UI。我想知道是否有任何方法可以定义一个方法,我可以将一个variabel交给以后在函数调用中使用的方法。目前我是这样解决的: def loader(self, filePath, id): #firstTable, secondTable and thirdTable are three different Table Widgets in the UI if id == "1": tab = self.ui

我目前正在开发一个基于Qt、pyqt和Python作为我语言的UI。我想知道是否有任何方法可以定义一个方法,我可以将一个variabel交给以后在函数调用中使用的方法。目前我是这样解决的:

def loader(self, filePath, id):
#firstTable, secondTable and thirdTable are three different Table Widgets in the UI
    if id == "1":
        tab = self.ui.firstTable
    elif id == "2":
        tab = self.ui.secondTable
    elif id == "3":
        tab = self.ui.thirdTable

    tab.clearContents()
    with open(filePath, "r", encoding="utf-8") as file:
        rowCount = tab.rowCount()
        columnCount = tab.columnCount()
        csvReader = csv.reader(file, delimiter=",", quotechar="'")
        for row in csvReader:
            if csvReader.line_num > rowCount:
                tab.insertRow(csvReader.line_num - 1)
            for i in range(0, columnCount):
                tab.setItem((csvReader.line_num - 1), i, QtWidgets.QTableWidgetItem(row[i]))
    tab.sortItems(1)
ld = cl.loader(filePath, cl.widget)
我想这样解决它,但我只是不知道怎么做,在研究这个问题时没有找到任何解决方案:

def loader(self, filePath, widget):
#variabel widget would be the corresponding object name I want to adress, like firstTable, secondTable, thirdTable
#...
    self.ui.widget.clearContents()
#...
    self.ui.widget.rowCount()
我希望我能正确地解释我的问题,有人能帮助我解决它。 谢谢大家!

编辑:以下是我当前调用加载程序的方式:

 self.loader("file.csv", "1")

如果widget是某个类cl中的一个方法,您可以像这样调用loader:

def loader(self, filePath, id):
#firstTable, secondTable and thirdTable are three different Table Widgets in the UI
    if id == "1":
        tab = self.ui.firstTable
    elif id == "2":
        tab = self.ui.secondTable
    elif id == "3":
        tab = self.ui.thirdTable

    tab.clearContents()
    with open(filePath, "r", encoding="utf-8") as file:
        rowCount = tab.rowCount()
        columnCount = tab.columnCount()
        csvReader = csv.reader(file, delimiter=",", quotechar="'")
        for row in csvReader:
            if csvReader.line_num > rowCount:
                tab.insertRow(csvReader.line_num - 1)
            for i in range(0, columnCount):
                tab.setItem((csvReader.line_num - 1), i, QtWidgets.QTableWidgetItem(row[i]))
    tab.sortItems(1)
ld = cl.loader(filePath, cl.widget)
然后在loader中使用它的位置:

widget.clearcontents(self)
等等


请记住,方法只是一个具有隐含的第一个参数(类引用)的函数。

您能发布调用加载程序的代码吗?我想这可能比你想象的要容易。非常感谢!现在它开始工作了,我不知道我以前在想什么