【发布时间】:2020-10-20 17:00:16
【问题描述】:
我正在尝试获取一个私有类装饰器,或者至少是一个与类工作相关的装饰器。我一直在尝试使用内联类,但目前在运行它时遇到以下问题:
click_ImportToDB() missing 1 required positional argument: 'self'
如果您可以查看以下内容,我已经删减了很多内容并让我知道我做错了什么,那就太好了:
class frmApplication(tk.Frame):
# THIS IS THE PRIVATE INLINE CLASS DECORATOR THAT I AM TRYING TO BUILD.
class _form_validation():
def __init__(self, func):
self.func = func
def __call__(self):
# Wrap the function up in error trapping and form validation
try:
# Ensure the strDBPath and config file have the same value for the database
# Ensure the database actually exists
# Now call the function
self.func()
except Exception as Ex:
strReport=Ex.args[0]
messagebox.showerror("Error",strReport)
#BACK TO THE frmApplication CLASS
def __init__(self,FormParent=tk.NONE):
.....
def __populateDatabaseFrame(self):
....
def __populateButtonFrame(self):
#First, add a frame
self.__objButtonFrame=ttk.Frame(self.master,border=2,relief=tk.SUNKEN)
self.__objButtonFrame.grid(row=1,column=0,columnspan=1,sticky=tk.N+tk.S+tk.E+tk.W,padx=10,pady=10)
F=self.__objButtonFrame
#Configure the columns of the frame
F.grid_columnconfigure(0,weight=1)
#Now add an upload to database button into the frame
# THERE IS A CALL TO click_ImportToDB HERE !!!!!
F.__btnImportToDB=ttk.Button(F,text="Upload to Database",command=self.click_ImportToDB)
F.__btnImportToDB.grid(row=0,column=0,sticky=tk.E+tk.W,padx=10,pady=10)
#Now add a button to show the data
.....
def __createDatabase(self):
...
def __selectDatabase(self):
...
#Public Functions.
def click_SelectDB(self):
...
def click_NewDB(self):
....
@_form_validation
def click_ImportToDB(self):
frmTop=tk.Toplevel(self.master)
frmTop.transient(frmTop.master)
frmTop.focus_set()
frmTop.grab_set()
objFrm=vID.frmImportToDB(frmTop)
frmTop.wait_window()
del objFrm
非常感谢
标记
【问题讨论】:
-
你能分享你的包装代码吗?
-
您好 Nivardo,代码非常庞大且复杂。如果这很复杂,请不要担心。我认为向一个类添加一个装饰器会很简单,但我似乎无法让“自我”部分正确传播。我在网上没有找到任何示例,所以可能无法完成。
标签: python-3.x class python-decorators