简体   繁体   English

place() 从 1 到 2 个位置 arguments 但给出了 3 个?

[英]place() takes from 1 to 2 positional arguments but 3 were given?

I srtuggle with the TypeError: " place_configure() takes from 1 to 2 positional arguments but 3 were given".我对 TypeError 感到困惑:“ place_configure() 从 1 到 2 个位置 arguments 但给出了 3 个”。 I don't understand why tkinter has a problem with the number of arguments I give to the place method.我不明白为什么 tkinter 对我给 place 方法的 arguments 的数量有问题。 Normally I did place(x=45, y=240) and changed the y value all the time in a hard coding style.通常我会用硬编码风格放置(x = 45,y = 240)并一直更改y值。 Now I want to make it easier using variables, for the case that maybe someone wants to change the values one time.现在我想让使用变量更容易,因为可能有人想改变一次值。 When I do this, the Error appears.当我这样做时,会出现错误。 Thanks for your help already.谢谢你的帮助。

        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




When you use the call relDebCB.place(x,y) you pass three arguments to the method .place() .当您使用调用relDebCB.place(x,y)时,您将三个 arguments 传递给方法.place() The first argument is the object stored in relDebCB .第一个参数是存储在 relDebCB 中的relDebCB The other two are positional arguments.另外两个是位置 arguments。

So, if your definition of the method only has two arguments, you pass one too many.因此,如果您的方法定义只有两个 arguments,那么您传递的太多了。

Since you did not provide the code, I guess that the function definition looks something like:由于您没有提供代码,我猜 function 定义类似于:

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

Instead, it should look like:相反,它应该看起来像:

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

The first argument in any method of a class should be self , as the first argument passed to any method is the object itself. class 的任何方法中的第一个参数应该是self ,因为传递给任何方法的第一个参数是 object 本身。

EDIT: Unless, of course, you don't have a method .place() but want to use the place manager of tkinter, as Space commented.编辑:当然,除非您没有方法.place()但想要使用 tkinter 的场所管理器,正如 Space 评论的那样。 Then, you would just have to call it with named arguments instead of positional arguments:然后,您只需使用名为 arguments 而不是位置 arguments 来调用它:

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

The x and y parameters must be specified as key-value pairs. xy参数必须指定为键值对。

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

The weird thing about the place method is that the first argument specifies if you want to move it to another master/root window, which is uncommon. place方法的奇怪之处在于,第一个参数指定是否要将其移动到另一个主/根 window,这并不常见。 So, you must specify that you want the x position and y position, like this, with key-value pairs:因此,您必须指定您想要 x position 和 y position,像这样,具有键值对:

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

You can also use relx and rely , which specify what percentage of the page to move it to.您还可以使用relxrely ,它们指定将其移动到页面的百分比。 For example, moving it to the center, and making it stay there, without regard for screen size, would require this code:例如,将其移动到中心并使其停留在那里,而不考虑屏幕大小,将需要以下代码:

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

You must specify the anchor as CENTER , or you can use "center" , as they are the same.您必须将锚指定为CENTER ,或者您可以使用"center" ,因为它们是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM