首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >self.bell()创建一个RecursionError

self.bell()创建一个RecursionError
EN

Stack Overflow用户
提问于 2019-07-19 20:09:59
回答 1查看 103关注 0票数 0

我正在开发一个密码管理器,分为几个类。当某个条件不满足时,我想要一个铃声来通知用户。在代码的一个更简单的版本中,我使用了很好的旧的self.bell(),没有问题(所有的代码都在一个类下--不好,所以升级了版本)。在升级后的代码中,我得到了一个递归错误。

我已经经历了我能想到的一切,没有任何乐趣。在各种论坛上,我似乎找不到任何与我的案例足够接近的案例。我在windows7上使用pycharm 2019.1.3。

主文件:

代码语言:javascript
复制
from class_createNewPassword import *
from class_searchDatabase import *
from class_updateDatabase import *


class display(Tk):
    def __init__(self, master=None):
        # main window
        Tk.__init__(self, master)
        self.title('Password Manager')

        buttonCreate = ttk.Button(self, text='Create a new password', command=createNewPassword)
        buttonCreate.grid(column=0, row=0)

        buttonSearch = ttk.Button(self, text='Search the database', command=SearchDatabase)
        buttonSearch.grid(column=1, row=2)

        buttonUpdate = ttk.Button(self, text='Update the database', command=UpdateDatabase)
        buttonUpdate.grid(column=2, row=4)


if __name__ == "__main__":
app = display()
app.mainloop()

问题所在的文件:

代码语言:javascript
复制
from tkinter import *
from tkinter import ttk
import tkinter as tk
from class_makePassword import *


class createNewPassword(Tk):
    def __init__(self):
        createWindow = tk.Toplevel()
        createWindow.title('Create a password')

        Toplevel.connection = sqlite3.connect("mdp - Copy.db")
        Toplevel.cursor = Toplevel.connection.cursor()
        self.connection = Toplevel.connection
        self.cursor = Toplevel.cursor

        #bunch of tkinter variables and stuff, and some functions not related to the issue


    def savePassword(self, condition):

        if condition:  #writes data in the DB, works well
            pass
        else:
            print("problem")
            self.bell()  #problem is here

我收到这个错误消息:

代码语言:javascript
复制
Exception in Tkinter callback
Traceback (most recent call last):
File "F:\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "I:\01 Programs\01 On the way\SQL\password manager with classes\class_createNewPassword.py", line 249, in savePassword
self.bell()
File "F:\Python37-32\lib\tkinter\__init__.py", line 783, in bell
self.tk.call(('bell',) + self._displayof(displayof))
File "F:\Python37-32\lib\tkinter\__init__.py", line 2101, in __getattr__
return getattr(self.tk, attr)
File "F:\Python37-32\lib\tkinter\__init__.py", line 2101, in __getattr__
return getattr(self.tk, attr)
File "F:\Python37-32\lib\tkinter\__init__.py", line 2101, in __getattr__
return getattr(self.tk, attr)
[Previous line repeated 493 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

有人能告诉我我的代码出了什么问题吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-19 21:38:09

问题的根源在于您的createNewPassword类继承了Tk,但是您没有调用Tk.__init__来正确地初始化该类。

我完全看不出你为什么要继承Tk。相反,从Toplevel继承并正确初始化它:

代码语言:javascript
复制
class createNewPassword(Toplevel):
    def __init__(self):
        Toplevel.__init__(self)
        self.title('Create a password')
        ...

    def savePassword(self, condition):

        if condition:
            pass
        else:
            self.bell()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57112244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档