Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建button类时获取递归错误_Python_Class_Button - Fatal编程技术网

Python 创建button类时获取递归错误

Python 创建button类时获取递归错误,python,class,button,Python,Class,Button,我正在为python项目创建一个button类,在运行时出现以下错误: my_Button = Button(master) [Previous line repeated 496 more times] RecursionError: maximum recursion depth exceeded Process finished with exit code 1 这是我创建的类: from tkinter import * import random class Window:

我正在为python项目创建一个button类,在运行时出现以下错误:

my_Button = Button(master)
[Previous line repeated 496 more times]
RecursionError: maximum recursion depth exceeded

Process finished with exit code 1
这是我创建的类:

from tkinter import *
import random


class Window:

    def __init__(self, master):
        master.title('Black Jack')
        master.configure(bg='green')
        master.geometry('500x500')


class Button:

    def __init__(self, master, text, font, x, y):
        master = master
        self.text = text
        self.font = font
        self.x = x
        self.y = y
        my_Button = Button(master, text=text, font=font)
        my_Button.pack(x=x, y=y)
代码如下:

from tkinter import *
import Classes

Helvetica = font = ('Helvetica', 15, 'bold italic')
Times = font = ('times', 13, 'bold italic')

root = Tk()
window = Classes.Window(root)
playButton = Classes.Button(root, 'Play', font, 200, 200)

为什么会出现递归错误?我没有使用任何循环。

从您的代码中,您需要检查以下代码,这些代码将递归调用
\uuuu init\uuu
方法

my_按钮=按钮(主控,文本=文本,字体=字体)

调用Button()方法时,每次都在调用
init
方法。因此,这将陷入一个循环中。根据我的理解,下面的代码行由于正在进行的递归将永远不会执行


my_Button.pack(x=x,y=y)

为什么按钮上有按钮?也许my_Button行是一个需要删除的早期实验?
my_Button=Button(master,text=text,font=font)
可能不在
\uuu init\uu
函数的范围内,或者根本不存在。你认为它在做什么?我认为它在创建一个按钮,比如var=button()。如果它存在于init之外,如何创建按钮并向其添加变量?
初始化
按钮
。当您调用
按钮(root,'Play',font,200200)
时,您是在隐式调用
按钮。\uuuu init\uuuu
。问题与主循环无关,只与递归调用按钮有关。如何递归调用按钮?我没有循环。是的,你没有循环,但是递归是循环的替代方法。您的
按钮
类正在调用自己的构造函数,该构造函数是递归。(顺便说一句,循环有时被视为避免递归和随之而来的堆栈溢出的方法)。我不明白。它如何调用自己的构造函数?init不应该在代码中运行一次并创建该按钮,然后转到主代码的下一行吗?它通过
\uuuuu init\uuu
方法中的这一行
my\u button=button(master,text=text,font=font)
调用自己的构造函数。因此,每次它到达这一行时,它都会重复调用构造函数方法。因此,这一行的
my_按钮.pack(x=x,y=y)
将永远不会执行。一定要阅读以更好地理解复发