请帮帮我。在这个创建按钮的简单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'
提前谢谢
在
Button
类中的__init__
中,您正试图处理Button
类的新实例:由于
button.__init__
接受3个参数,row, column, frame
脚本失败。如果同时传递row
和column
,则会遇到递归问题,在这种情况下,无限量地创建Button
的新实例。在编辑:正如在评论和其他答案中指出的,
^{pr2}$tkinter
有自己的Button
类,您正在重写它,这就是为什么您应该尽量避免这样做的原因而只需
import tkinter
并调用tkinter.Button
。在当tkinter有一个class按钮时,您创建了一个class按钮。使用您自己的变量名可能会有帮助:)我想它是在尝试递归地生成您创建的Button类的一个实例,而不是在tkinter模块中创建一个Button类的实例。在
相关问题 更多 >
编程相关推荐