我正在开发一个密码管理器,分为几个类。当某个条件不满足时,我想要一个铃声来通知用户。在代码的一个更简单的版本中,我使用了很好的旧的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有人能告诉我我的代码出了什么问题吗?谢谢。
发布于 2019-07-19 21:38:09
问题的根源在于您的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()https://stackoverflow.com/questions/57112244
复制相似问题