__init_yu()缺少2个必需的位置参数

2024-09-27 00:13:11 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮帮我。在这个创建按钮的简单tkinter程序中,我提供了所有三个参数,但是关于位置参数的错误出现在屏幕上。对不起,我的英语很差。

from tkinter import *

class Button:
def __init__(self, row, column, frame):
    self.row = row
    self.column = column
    b = Button(frame).grid(row = self.row, column = self.column)

tk = Tk()
b1 = Button(row = 1, column = 1, frame = tk)
tk.mainloop()

错误是:

RESTART: C:\Users\vnira\Documents\python.projects\Flappy Bird\whiteboard.py
Traceback (most recent call last):
File "C:\Users\vnira\Documents\python.projects\Flappy Bird\whiteboard.py", line 11, in
b1 = Button(row = 1, column = 1, frame = tk)
File "C:\Users\vnira\Documents\python.projects\Flappy Bird\whiteboard.py", line 7, in init
Button(frame).grid(row = self.row, column = self.column)
TypeError: init() missing 2 required positional arguments: 'column' and 'frame'

提前谢谢


Tags: pyselfinitcolumnbuttonframeuserstk
2条回答

Button类中的__init__中,您正试图处理Button类的新实例:

b = Button(frame)

由于button.__init__接受3个参数,row, column, frame脚本失败。如果同时传递rowcolumn,则会遇到递归问题,在这种情况下,无限量地创建Button的新实例。在

编辑:正如在评论和其他答案中指出的,tkinter有自己的Button类,您正在重写它,这就是为什么您应该尽量避免这样做的原因

^{pr2}$

而只需import tkinter并调用tkinter.Button。在

from tkinter import *

class Buttons:
    def __init__(self, row, column, frame):
        self.row = row
        self.column = column
        b = Button(frame).grid(row = self.row, column = self.column)

tk = Tk()
b1 = Buttons(row = 1, column = 1, frame = tk)
tk.mainloop()

当tkinter有一个class按钮时,您创建了一个class按钮。使用您自己的变量名可能会有帮助:)我想它是在尝试递归地生成您创建的Button类的一个实例,而不是在tkinter模块中创建一个Button类的实例。在

相关问题 更多 >

    热门问题