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

请帮帮我。在这个用于创建按钮的简单 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()

和错误:


重新启动:C:\Users\vnira\Documents\python.projects\Flappy Bird\whiteboard.py

Traceback(最近一次调用最后一次):

文件“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 () 缺少 2 个必需的位置参数:'column' 和 'frame'


提前致谢


三国纷争
浏览 356回答 2
2回答

牧羊人nacy

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 有一个 Button 类时,您创建了一个 Button 类。使用您自己的变量名可能会有所帮助:) 我想它是试图递归地创建您创建的 Button 类的实例,而不是创建 tkinter 模块内的 Button 类的实例。

HUWWW

在__init__在Button您要工作在一个新的实例类Button类:b = Button(frame)由于button.__init__需要 3 个参数,row, column, frame因此脚本失败。如果你没有传递row和column也,你会遇到的递归问题,可能无限地创造新的实例Button。编辑:正如评论和其他答案中所指出的,tkinter有自己的Button类,你正在覆盖,这就是为什么你应该尽量避免这样做from tkinter import *而只是import tkinter调用tkinter.Button.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python