在kv文件中使用FlatButton后,出现了“ AttributeError:'NoneType'对象没有属性'bind'

时间:2019-01-26 20:14:16

标签: python kivy

我正在尝试在我的kv中实现FlatButton,但我不断收到与AttributeError相同的错误:'NoneType'对象没有属性'bind。单独使用Button即可正常工作。

from flat_kivy.flatapp import FlatApp
from kivy.uix.touchripple import TouchRippleBehavior
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import (StringProperty, NumericProperty, ObjectProperty,
ListProperty, DictProperty, BooleanProperty)


class Login(Screen):
    pass

class MainScreen(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

theRoot = Builder.load_string('''
ScreenManager:
    Login:

<Login>:
    FlatButton:
        text: 'Click Here'
        size_hint: (.4,.25)
''')

class TouchRippleApp(FlatApp):
    def build(self):
        return theRoot

if __name__ == '__main__':
    TouchRippleApp().run()

这是Flat_Kivy中的FlatButton代码。我陷入了这个问题。

class FlatButtonBase(GrabBehavior, LogBehavior, TouchRippleBehavior,
                     ThemeBehavior):
    color = ListProperty([1., 1., 1.])
    color_down = ListProperty([.7, .7, .7])
    border_size = ListProperty([0, 0, 0, 0])
    text = StringProperty('')
    alpha = NumericProperty(1.0)
    style = StringProperty(None, allownone=True)
    color_tuple = ListProperty(['Grey', '500'])
    font_color_tuple = ListProperty(['Grey', '1000'])
    ripple_color_tuple = ListProperty(['Grey', '1000'])
    font_ramp_tuple = ListProperty(None)
    font_size = NumericProperty(12)
    eat_touch = BooleanProperty(False)

    def on_color(self, instance, value):
        self.color_down = [x*.7 for x in value]


class FlatButton(FlatButtonBase, ButtonBehavior, AnchorLayout):
    pass


class RaisedFlatButton(RaisedStyle, FlatButton):
    pass

1 个答案:

答案 0 :(得分:0)

也许更简单的方法是使用.kv语言(在要使用Builder.load_string加载的字符串中)创建FlatButton类

尝试将其添加到您的kv字符串中:

<FlatButton@Button>: # create a class "FlatButton" that inherits the kivy Button background_normal: "" # Get rid of the kivy Button's default background image background_down: "" # Get rid of the kivy Button's default background image when clicked # Set the background color to transparent if no action is happening to the button # If the button is clicked, it will change it to fully white background_color: (1,1,1,0) if self.state == 'normal' else (1,1,1,1)

,然后您可以在python端删除与FlatButton类相关的所有代码,但为kv创建可使用的基类除外。例如。您在python代码中所需的就是

class FlatButton(): pass