AttributeError:“按钮”对象没有属性

Posted

技术标签:

【中文标题】AttributeError:“按钮”对象没有属性【英文标题】:AttributeError: 'Button' object has no attribute 【发布时间】:2020-05-31 10:07:41 【问题描述】:

在使用 Filechooser(全部在一个屏幕上)选择目录后,我尝试将函数(在 .py 中)绑定到按钮(在 kv.),如下所示:

.py:

class SyllWindow(Screen): #screen where all happens

    def select(self, *args):

        if args[1]:

            filepath = args[1][0]

            try: self.label.text = filepath

            except: pass

    def output_csv(corpus_root): #function

        corpus = PlaintextCorpusReader(corpus_root, '.*') #this must be irrelevant actually
        def reduce_dip(corpus_string):
            corpus_string = corpus_string.replace("Ei", "ö")
            corpus_string = corpus_string.replace("ei", "ö")
            # reduced
            corpus_string = corpus_string.replace("ie", "ö")
            return corpus_string
        vowels = [' ','a','e','i','o','u','ä','ö','ü','A','E','I','O','U','Ä','Ö','Ü']
        cfd_syll = nltk.ConditionalFreqDist(
            (textname, num_syll)
            for textname in corpus.fileids()
            for num_syll in [len(w) for w in ''.join(char for char in reduce_dip(corpus.raw(fileids=textname)) if char in vowels).split()])
        syll_dataframe = DataFrame(cfd_syll)
        return syll_dataframe.to_csv(path + '\silben.csv')            

    pass

.kv(按钮块):

Button:

                            text: '.csv'
                            font_size: 14
                            on_release: self.output_csv(self.filepath)

看起来像这样:

按“.csv”后我收到错误AttributeError: 'Button' object has no attribute 'output_csv' 我知道here、here 和here 描述的类似问题,但所有示例都使用 Python 语言,而我的问题似乎更多的是关于 Kivy 语言。

如果有任何建议,我将不胜感激。

完整的错误回溯:

File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\runpy.py", line 193, in _run_module_as_main
  "__main__", mod_spec)
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\runpy.py", line 85, in _run_code
  exec(code, run_globals)
File "C:\GUI Projects\gercort\main.py", line 147, in <module>
  Gercort().run()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\app.py", line 855, in run
  runTouchApp()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 504, in runTouchApp
  EventLoop.window.mainloop()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\core\window\window_sdl2.py", line 747, in mainloop
  self._mainloop()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\core\window\window_sdl2.py", line 479, in _mainloop
  EventLoop.idle()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 342, in idle
  self.dispatch_input()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 327, in dispatch_input
  post_dispatch_input(*pop(0))
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 293, in post_dispatch_input
  wid.dispatch('on_touch_up', me)
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
  self.dispatch('on_release')
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 703, in kivy._event.EventDispatcher.dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 1214, in kivy._event.EventObservers.dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 1098, in kivy._event.EventObservers._dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
  exec(__kvlang__.co_value, idmap)
File "C:\GUI Projects\gercort\gercort.kv", line 489, in <module>
  on_release: self.output_csv(self.filepath2)
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\weakproxy.cp37-win32.pyd", line 32, in kivy.weakproxy.WeakProxy.__getattr__

builtins.AttributeError: 'Button' object has no attribute 'output_csv'

【问题讨论】:

请将完整的错误回溯添加到您的问题中。 @KlausD.Added full tr​​aceback to the description 【参考方案1】:
on_release: self.output_csv(self.filepath2)

self 表示您正在从 Button 类调用函数,并且没有名为 output_csv 的函数,因此错误会告诉您。您在 SyllWindow 类中定义了该函数。如果该按钮也在该屏幕上,只需将其更改为

on_release: root.output_csv(root.filepath)

【讨论】:

是的,按钮在.kv的&lt;SyllWindow&gt; @Gavrk 你的 filepath2 变量在哪里定义? @Gavrk 也尝试使用 root 而不是 self,我更改了答案,再次检查。 @Gavrk 在 SyllWindow 类中使用 self.filepath 代替 'filepath' 名称。但不要更改 kv 文件。 @Gavrk 那是因为按钮,添加到你的函数名称 *args,所以它会像:def output_csv(corpus_root, *args)

以上是关于AttributeError:“按钮”对象没有属性的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:“超级”对象没有属性“__getattr__”[疑难解答:Kivy]

AttributeError: 'RDD' 对象没有属性 'show'

AttributeError:“NumpyArrayIterator”对象没有属性“类”

AttributeError:'list' 对象没有属性 'size'

AttributeError:“模块”对象没有属性“WebSocketApp”

AttributeError: 'float' 对象没有属性 'split'