【问题标题】:place() takes from 1 to 2 positional arguments but 3 were given?place() 接受 1 到 2 个位置参数,但给出了 3 个?
【发布时间】:2021-08-17 18:32:10
【问题描述】:

我对 TypeError 感到不满:“place_configure() 接受 1 到 2 个位置参数,但给出了 3 个”。我不明白为什么 tkinter 对我给 place 方法的参数数量有问题。通常我会用硬编码风格放置(x = 45,y = 240)并一直更改y值。现在我想让使用变量更容易,因为可能有人想更改一次值。当我这样做时,会出现错误。已经感谢您的帮助。

        x = 45;
        y = 240;
        self.counter = 0

        

        # Creation of checkbuttons
        self.values_checkboxes_bitness={"32 and 64 Bit": 0, "32 Bit": 1, "64 Bit": 2}
        self.values_checkboxes_debugRelease={"rebug and release": 0, "debug": 1, "release": 2}

        for text in self.values_checkboxes_bitness:
            self.counter+=1
            bitnessCB = Checkbutton( text=text,variable=self.int_var_one, onvalue=self.values_checkboxes_bitness[text], offvalue=3,
                               command=partial(self.checkbox_clicked, self.int_var_one, self.int_var_one_comp,
                                               self.int_var_one_no_checkout,
                                              self.int_var_one_buildtype))
            bitnessCB.pack()
            bitnessCB.config(font=("Helvetica", 10))

        for text in self.values_checkboxes_debugRelease:
            self.counter+=1

        relDebCB =  Checkbutton(text=text,variable=self.int_var_one_comp, onvalue=self.values_checkboxes_debugRelease[text], offvalue=3,
                           command=partial(self.checkbox_clicked, self.int_var_one, self.int_var_one_comp,
                                           self.int_var_one_no_checkout,
                                          self.int_var_one_buildtype))

        relDebCB.config(font=("Helvetica", 10))
        if self.counter > 0:
            y += 30
            relDebCB.place(x,y)
        else:
            relDebCB.place(x,y)```

TypeError: place_configure() takes from 1 to 2 positional arguments but 3 were given




【问题讨论】:

  • 我认为您需要提供关键字参数为relDebCB.place(x=x,y=y)

标签: python tkinter checkbox arguments


【解决方案1】:

当您使用调用relDebCB.place(x,y) 时,您将三个参数传递给方法.place()。第一个参数是存储在relDebCB 中的对象。另外两个是位置参数。

所以,如果你的方法定义只有两个参数,你传递的参数太多了。

由于您没有提供代码,我猜函数定义类似于:

def place(x,y):
   pass ## Here goes your code

相反,它应该是这样的:

def place(self,x,y):
   pass ## Here goes your code

类的任何方法的第一个参数都应该是self,因为传递给任何方法的第一个参数是对象本身。

编辑:当然,除非您没有方法 .place() 但想要使用 tkinter 的场所管理器,正如 Space 评论的那样。然后,您只需要使用命名参数而不是位置参数来调用它:

relDebCB.place(x=x,y=y)

【讨论】:

  • 由于引发了异常TypeError: place_configure() ...,我的猜测是OP试图调用place Tk几何管理器,而不是定义同名的方法。
  • 谢谢!没有想到这一点并修改了答案。我自己从不使用这种方法,因为我是一个严格的 .grid() 用户。
【解决方案2】:

xy 参数必须指定为键值对。

relDebCB.place(x,y) 更改为relDebCB.place(x=x,y=y)

【讨论】:

    【解决方案3】:

    place 方法的奇怪之处在于,第一个参数指定是否要将其移动到另一个主/根窗口,这并不常见。因此,您必须使用键值对指定您想要的 x 位置和 y 位置,如下所示:

    widget.place(x = x, y = y)
    

    您还可以使用relxrely,指定将其移动到页面的百分比。例如,将其移动到中心并使其停留在那里,而不考虑屏幕大小,将需要以下代码:

    widget = Frame(..., anchor = CENTER)
    widget.place(relx = 0.5, rely = 0.5)
    

    您必须将锚指定为CENTER,或者您可以使用"center",因为它们是相同的。

    【讨论】:

    • 非常感谢!这很有帮助。
    • Frame 没有anchor 选项,你的意思是place(..., anchor=CENTER)
    猜你喜欢
    • 2022-01-04
    • 2021-01-08
    • 2020-07-22
    • 2019-06-28
    • 1970-01-01
    • 2023-03-03
    • 2016-10-13
    • 2019-03-12
    • 2019-04-20
    相关资源
    最近更新 更多