不匹配的位置参数错误即使没有不匹配 [英] Mismatched positional arguments error even though no mismatch

查看:79
本文介绍了不匹配的位置参数错误即使没有不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可点击的标签,点击时应该传递参数.但是,当我点击标签时,我得到:

I have a clickable label that is supposed to pass parameters when clicked. However, when I click the label, I get:

TypeError: configure_towns() takes 0 positional arguments but 1 was given

显然,这意味着在某处调用 configure_towns 时收到了意外参数,但在我的代码中没有任何地方发生这种情况.我有另一个按钮,它利用工厂函数将参数传递给函数,并且没有任何问题.

Obviously this means that somewhere a call to configure_towns is being made that's receiving an unexpected parameter, but nowhere in my code does this occur. I have another button that utilizes a factory function to pass parameters to the function, and that works without any issue.

configure_towns 函数在别处被调用两次:

The configure_towns function is called twice elsewhere using:

self.town_factory(self.place_list, self.group.layout, None).__call__()

并按预期工作.只有在点击标签后才会抛出错误.

and works as intended. It's only after the label is clicked that the error is thrown.

这里是工厂函数的代码以及可点击标签的配置位置

Here's the code for the factory function and where the clickable label is configured

def town_factory(self, place_list, grp_layout, label):
  def configure_towns():
    # Testing if the function call works
    if label != None:
      print(label + ' clicked')

    for place in place_list:
      lab = place_list[place]['Name'] + ' ' + place_list[place]['Kind']

      open_lab = QLabel('\u25b6')
      open_lab.mouseReleaseEvent = self.town_factory(place_list, grp_layout, lab)
  return configure_towns

任何帮助将不胜感激.

这是一个完整的应用程序,您可以将其复制并粘贴到 Python 编辑器中以显示我的问题.您会注意到 Button 工作正常,而标签返回所述错误,即使 configure_towns() 已成功调用以首先显示标签.

Here's a full application you can copy and paste into a Python editor to show my issue. You will notice that the Button works just fine, while the label returns the stated error, even though configure_towns() is successfully called earlier in order to display the labels in the first place.

from PyQt5.QtWidgets import *

app = QApplication([])

main_layout = QVBoxLayout()

class MainWindow(QWidget):    
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle('Title')
        self.setGeometry(1200, 200, 350, 500)

        # Preparing the button
        first_layout = QVBoxLayout()
        first_layout.addWidget(QWidget())
        self.note_factory(first_layout, -1)()
        main_layout.addLayout(first_layout)

        # Preparing the label
        self.place_list = ['First', 'Second', 'Third']

        second_layout = QVBoxLayout()
        second_layout.addWidget(QWidget())
        self.town_factory(self.place_list, second_layout, None)()
        main_layout.addLayout(second_layout)

    def note_factory(self, layout, count):
        ## Every time you click the button, the counter increases
        count += 1
        def configure_notes():
            temp = layout.itemAt(0).widget()
            temp.deleteLater()

            temp = QWidget()
            temp.layout = QVBoxLayout()
            temp.setLayout(temp.layout)

            label = QLabel(str(count) + ' clicks')
            temp.layout.addWidget(label)

            button = QPushButton('Click')
            temp.layout.addWidget(button)
            button.clicked.connect(self.note_factory(layout, count))

            layout.addWidget(temp)

        return configure_notes

    def town_factory(self, place_list, layout, label):
        ## Every time you click "Click Me!" The appropriate label appears.
        def configure_towns():
            temp = layout.itemAt(0).widget()
            temp.deleteLater()

            temp = QWidget()
            temp.layout = QVBoxLayout()
            temp.setLayout(temp.layout)

            if label != None:
                lab = QLabel(label + ' clicked')
                temp.layout.addWidget(lab)

            for place in place_list:                
                p_grp = QHBoxLayout()  

                open_lab = QLabel('Click Me!')
                open_lab.mouseReleaseEvent = self.town_factory(place_list, layout, place)

                p_grp.addWidget(open_lab)

                p_grp.addWidget(QLabel(place))
                p_grp.addStretch(1)

                temp.layout.addLayout(p_grp)

            layout.addWidget(temp)
        return configure_towns

window = MainWindow()

window.setLayout(main_layout)

window.show()
app.exec_()

推荐答案

我想你忘了把 self 作为 configure_towns 参数.

I think you forgot to put a self as configure_towns parameter.

def configure_towns(self):
    # Testing if the function call works
    if label != None:
        print(label + ' clicked')
    ...

这篇关于不匹配的位置参数错误即使没有不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆