我正在使用密码管理器,它分为几个类。当不满足特定条件时,我希望响铃以通知用户。在代码的简单版本中,我没有问题地使用了良好的旧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
谁能告诉我我的代码有什么问题? 谢谢。
答案 0 :(得分:0)
问题的根源是您的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()