Skip to main content
 首页 » 编程设计

python之Kivy 按钮小部件 : Attribute Error, 没有属性 'fbind'

2025年02月15日8mq0036

两三个小时以来,我一直在努力寻找问题的解决方案,但我就是无法准确地找到问题出在哪里,即使在 SO 上或 Kivy 文档中查看其他用户的问题时也是如此。

我目前正在网上学习一些教程,并开始研究 this simple one

我的代码与他的不完全匹配,但即使在复制本教程的确切代码作为测试时,我也会得到完全相同的错误,与 Kivy 中的 Button 小部件相关:

AttributeError: 'Button' object has no attribute 'fbind' 

这可能与某种安装问题有关吗?谁能帮我解决这个问题?

[INFO              ] Kivy v1.8.0 
[INFO              ] [Logger      ] Record log in C:\Users\NoirFluo\.kivy\logs\kivy_17-03-01_64.txt 
[INFO              ] [Factory     ] 157 symbols loaded 
[DEBUG             ] [Cache       ] register <kv.lang> with limit=None, timeout=Nones 
[DEBUG             ] [Cache       ] register <kv.image> with limit=None, timeout=60s 
[DEBUG             ] [Cache       ] register <kv.atlas> with limit=None, timeout=Nones 
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_pygame, img_pil, img_gif  
[DEBUG             ] [Cache       ] register <kv.texture> with limit=1000, timeout=60s 
[DEBUG             ] [Cache       ] register <kv.shader> with limit=1000, timeout=3600s 
[DEBUG             ] [App         ] Loading kv <C:/Users/NoirFluo/Desktop/Python/Kivy/TestProblemButton\screens.kv> 
[DEBUG             ] [Window      ] Ignored <egl_rpi> (import error) 
[INFO              ] [Window      ] Provider: pygame(['window_egl_rpi'] ignored) 
[DEBUG             ] [Window      ] Display driver windib 
[DEBUG             ] [Window      ] Actual window size: 800x600 
[DEBUG             ] [Window      ] Actual color bits r8 g8 b8 a8 
[DEBUG             ] [Window      ] Actual depth bits: 24 
[DEBUG             ] [Window      ] Actual stencil bits: 8 
[DEBUG             ] [Window      ] Actual multisampling samples: 2 
[INFO              ] [GL          ] OpenGL version <4.5.13464 Compatibility Profile Context 21.19.407.0> 
[INFO              ] [GL          ] OpenGL vendor <ATI Technologies Inc.> 
[INFO              ] [GL          ] OpenGL renderer <AMD Radeon HD 7800 Series> 
[INFO              ] [GL          ] OpenGL parsed version: 4, 5 
[INFO              ] [GL          ] Shading version <4.50> 
[INFO              ] [GL          ] Texture max size <16384> 
[INFO              ] [GL          ] Texture max units <32> 
[DEBUG             ] [Shader      ] Fragment compiled successfully 
[DEBUG             ] [Shader      ] Vertex compiled successfully 
[DEBUG             ] [ImagePygame ] Load <C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\data\glsl\default.png> 
[INFO              ] [Window      ] virtual keyboard not allowed, single mode, not docked 
[INFO              ] [Text        ] Provider: pygame 
GLEW initialization succeeded 
 
Traceback (most recent call last): 
 
  File "<ipython-input-1-414e3aba4aa1>", line 1, in <module> 
    runfile('C:/Users/NoirFluo/Desktop/Python/Kivy/TestProblemButton/main.py', wdir='C:/Users/NoirFluo/Desktop/Python/Kivy/TestProblemButton') 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 
 
  File "C:/Users/NoirFluo/Desktop/Python/Kivy/TestProblemButton/main.py", line 26, in <module> 
    ScreensApp().run() 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\app.py", line 766, in run 
    root = self.build() 
 
  File "C:/Users/NoirFluo/Desktop/Python/Kivy/TestProblemButton/main.py", line 23, in build 
    return Manager() 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\uix\screenmanager.py", line 791, in __init__ 
    super(ScreenManager, self).__init__(**kwargs) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\uix\floatlayout.py", line 66, in __init__ 
    super(FloatLayout, self).__init__(**kwargs) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\uix\layout.py", line 63, in __init__ 
    super(Layout, self).__init__(**kwargs) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\uix\widget.py", line 173, in __init__ 
    Builder.apply(self) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\lang.py", line 1566, in apply 
    self._apply_rule(widget, rule, rule) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\lang.py", line 1672, in _apply_rule 
    self.apply(child) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\lang.py", line 1566, in apply 
    self._apply_rule(widget, rule, rule) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\lang.py", line 1670, in _apply_rule 
    child = cls(__no_builder=True) 
 
  File "C:\Users\NoirFluo\Anaconda2\lib\site-packages\kivy\uix\behaviors\button.py", line 83, in __init__ 
    self.fbind('state', self.cancel_event) 
 
AttributeError: 'Button' object has no attribute 'fbind'` 

这是来自 screens.kv 文件的代码:

#:kivy 1.8.0 
 
<ScreenOne>: 
    Button: 
        text: "On SCREEN 1 >> Go to Screen 2" 
        on_press: root.manager.current = "screen2" 
 
<ScreenTwo>: 
    Button: 
        text: "On SCREEN 2 >> Go to Screen 3" 
        on_press: root.manager.current = "screen3" 
 
<ScreenThree>: 
    Button: 
        text: "On SCREEN 3 >> Go to Screen 1" 
        on_press: root.manager.current = "screen1" 
 
 
<Manager>: 
    id: screen_manager 
 
    screen_one: screen_one 
    screen_two: screen_two 
    screen_three: screen_three 
 
    ScreenOne: 
        id: screen_one 
        name: "screen1" 
        manager: screen_manager 
 
    ScreenTwo: 
        id: screen_two 
        name: "screen2" 
        manager: screen_manager 
 
    ScreenThree: 
        id: screen_three 
        name: "screen3" 
        manager: screen_manager 

这是main.py的代码:

# -*- coding: utf-8 -*- 
from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.properties import ObjectProperty 
 
class ScreenThree(Screen): 
    pass 
 
class ScreenTwo(Screen): 
    pass 
 
class ScreenOne(Screen): 
    pass 
 
class Manager(ScreenManager): 
    screen_one = ObjectProperty(None) 
    screen_two = ObjectProperty(None) 
    screen_three = ObjectProperty(None) 
 
class ScreensApp(App): 
 
    def build(self): 
        return Manager() 
 
if __name__ == "__main__": 
    ScreensApp().run() 

编辑:嗯,编辑已经吃掉了我的“你好,伙计们!”,我似乎无法通过编辑我的帖子把它放回去,出于某些原因,所以我会把它放在这里:你好,伙计们! ^^'

请您参考如下方法:

Kivy 1.8.0 是一只恐龙,很快就会有一个比当前的旧稳定版 (1.9.1) 更新的版本。

我已经尝试了您的代码并且它工作正常,而且,我相信它甚至在 1.8.0 上也能正常工作(不确定,如果需要,请检查已关闭的 issues)。

现在您可以执行以下操作之一:

  • 从您要使用的标签中完全重新安装您的 Kivy 安装(如果它是 1.8.0,我们就去做吧)

  • 安装稳定版

  • 安装最新的 Kivy 版本

检查 instructions关于如何这样做,如果不够或太难,请使用 this installer .请注意,这 不是! 与您现在拥有的 kivy.bat 相同(1.8.0 包中有这样的文件,现在不存在了),所以它不能替代旧的。