Python 3.x 用Python编写装饰函数

Python 3.x 用Python编写装饰函数,python-3.x,python-decorators,Python 3.x,Python Decorators,抱歉,如果它是重复的,但我正在努力找到一个解决办法。我试图在下面写一个装饰函数 def search_func(sheetname): def insider(function): def wraper(*args, **kwargs): file = openpyxl.load_workbook("C:\\Users\khisr\OneDrive\Documents\TestPage.xlsx") currnet_sh

抱歉,如果它是重复的,但我正在努力找到一个解决办法。我试图在下面写一个装饰函数

  def search_func(sheetname):
    def insider(function):
        def wraper(*args, **kwargs):
            file = openpyxl.load_workbook("C:\\Users\khisr\OneDrive\Documents\TestPage.xlsx")
            currnet_sheet = file[sheetname]
            function(currnet_sheet)
        return wraper
    return insider         


@search_func('Passwords')
def Longin(self, currnet_sheet):
    Name = self.User_name.get() + str(self.Password.get())
    for i in range(1,current_sheet.max_row +1):
       for j in range(1,current_sheet.max_column+1):
           if current_sheet.cell(i,j).value == Name:
通过按钮调用该函数

self.Button = tk.Button(self.Main_frame, text='Enter', bg = 'green', command = self.Longin)
我犯了一个错误

"Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/khisr/OneDrive/Documents/PythonProjects/Dubai_Project/login.py", line 71, in wraper
    function(currnet_sheet)
TypeError: Longin() missing 1 required positional argument: 'currnet_sheet"
任何帮助或赞扬都是非常感谢的


Cheers

方法在查看时绑定到self。装饰方法时,结果装饰方法是在
self
上查找的,而不是底层方法。您需要显式传递
self
参数

def search_func(sheetname):
  def insider(function):
      def wraper(self):  # the decorated method receives self...
          file = openpyxl.load_workbook("C:\\Users\khisr\OneDrive\Documents\TestPage.xlsx")
          currnet_sheet = file[sheetname]
          function(self, currnet_sheet)  # ...and should pass it on
      return wraper
  return insider
请注意,如果您不想只支持常规方法,则必须(部分)复制


你的装饰师很好。问题在于调用
Longin
方法的对象。可能是因为
tk.Button
希望传递的是函数而不是方法(方法需要
self
)。如果您希望
Longin
成为类的一部分,请尝试将其转换为
@staticmethod
。谢谢,您能详细介绍一下@staticmethod吗?目前,@search_func(‘Passwords’)通过了“Paswords”,这是页面,我希望打开def search_func(sheetname)函数。我如何传递需要使用@staticmethod打开的页面名称?不是错误,但您可能希望在decorator中返回:
返回函数(currnet\u sheet)
,否则,如果您装饰返回内容的方法/函数,decorator将使用它。抱歉,staticmethod无法工作。您可以使用
self
。尝试使用lambda:
self.Button=tk.Button(self.Main\u frame,text='Enter',bg='green',command=lambda sheet:self.Longin(sheet))
谢谢,我尝试了所有建议,但现在似乎出现了此错误。文件“C:\ProgramData\Anaconda3\lib\tkinter_init_uuu.py”,第1705行,在调用返回self.func(*args)文件“C:/Users/khisr/OneDrive/Documents/PythonProjects/Dubai_Project/login.py”,第70行,在wrapper函数中,在范围(1,当前工作表.max\u行+1)内的i的Longin中:名称错误:名称“当前工作表”不正确defined@user3064089这看起来是另一个问题。它与此处的代码和问题无关。它抱怨没有找到“当前工作表”,但正如您在传递给函数的代码中看到的那样。@user3064089:您的意思是
currnet\u工作表
?Python对变量名中的输入错误很敏感。(实际上,如果不是所有其他编程语言的话,也是如此。)
def search_func(sheetname):
  def insider(function):
      def wrapper(self):  # the decorated method receives self...
          file = openpyxl.load_workbook("C:\\Users\khisr\OneDrive\Documents\TestPage.xlsx")
          currnet_sheet = file[sheetname]
          function.__get__(self, type(self))(currnet_sheet)  # ...and should pass it on
      return wraper
  return insider