使用移交函数参数调用方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用移交函数参数调用方法相关的知识,希望对你有一定的参考价值。

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

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)

我想解决它有点像这样,但我只是不知道如何做到这一点,并在研究这个问题时找不到任何解决方案:

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:

ld = cl.loader(filePath, cl.widget)

然后你在装载机中使用它:

widget.clearcontents(self)

等等

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

以上是关于使用移交函数参数调用方法的主要内容,如果未能解决你的问题,请参考以下文章

再次调用“onCreateView” - Android

如何从片段 KOTLIN 中调用意图 [重复]

Python函数

方法

第四篇 函数

JAVA方法的定义