gpt4 book ai didi

python - self.bell()创建一个RecursionError

转载 作者:行者123 更新时间:2023-12-03 01:31:49 24 4
gpt4 key购买 nike

我正在研究密码管理器,它分为几个类。当不满足特定条件时,我希望响铃以通知用户。在一个简单的代码版本中,我使用了很好的旧self.bell()没问题(所有代码都在一个类下-不好,因此是升级版本)。
在升级的代码中,我得到了递归错误。

我经历了我能想到的一切,没有喜悦。我似乎找不到足够接近的案例可以在各种论坛上找到。
我在Windows7上使用pycharm 2019.1.3。

主文件:

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()

问题所在的文件:
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

我收到此错误消息:
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

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

最佳答案

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

我根本看不到您为什么要从Tk继承的任何原因。而是从Toplevel继承并正确初始化它:

class createNewPassword(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.title('Create a password')
...

def savePassword(self, condition):

if condition:
pass
else:
self.bell()

关于python - self.bell()创建一个RecursionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57112244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com