Python 3.x 如何将touchripple.py与Kivy lang一起使用?

Python 3.x 如何将touchripple.py与Kivy lang一起使用?,python-3.x,kivy,Python 3.x,Kivy,我正在尝试使用kivy.uix.behaviors中的touchripple.py创建一个按钮。然而,我最终失败了。有人能用Kivy lang演示一个简单的按钮触摸涟漪的例子吗?提前谢谢。 现在,只有涟漪效应还没有显现出来。请给我一些建议。谢谢 在rippleexample2.py中: from kivy.app import App from kivy.uix.touchripple import TouchRippleBehavior from kivy.uix.button import

我正在尝试使用kivy.uix.behaviors中的touchripple.py创建一个按钮。然而,我最终失败了。有人能用Kivy lang演示一个简单的按钮触摸涟漪的例子吗?提前谢谢。 现在,只有涟漪效应还没有显现出来。请给我一些建议。谢谢

在rippleexample2.py中:

from kivy.app import App
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 RippleButton(TouchRippleBehavior, Button):
    isRippled = BooleanProperty(False)

    def __init__(self, **kwargs):
        super(RippleButton, self).__init__(**kwargs)

    def on_touch_down(self, touch):
        collide_point = self.collide_point(touch.x, touch.y)
        if collide_point and not self.isRippled:
            self.isRippled = True
            self.ripple_show(touch)
        return super(RippleButton, self).on_touch_down(touch)

    def on_touch_up(self, touch):
        collide_point = self.collide_point(touch.x, touch.y)
        if collide_point and self.isRippled:
            self.isRippled = False
            self.ripple_fade()
        return super(RippleButton, self).on_touch_up(touch)

    def doit(self, *args):
        print('in doit')

 class Login(Screen):
    pass

class MainScreen(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

MainScreen = Builder.load_file("rippleexample2.kv")

class SimpleKivy4(App):
    def build(self):
        return MainScreen

if __name__ == "__main__":
    SimpleKivy4().run()
在rippleexample2.kv中:

ScreenManager:
    Login:
    MainScreen:

<Login>:
    name:"login"
    RippleButton:
        text:'Login'
        font_size: 24
        on_release: app.root.current = "main"


<MainScreen>:
    name: "main"
    RippleButton:
        text: 'back'
        on_release: app.root.current = "login"
屏幕管理器:
登录:
主屏幕:
:
名称:“登录”
RippleButton:
文本:'Login'
字体大小:24
发布时:app.root.current=“main”
:
名称:“主要”
RippleButton:
文本:“返回”
发布时:app.root.current=“登录”

仔细查看示例后,我注意到他们在
RippleButton
中做了一些奇怪的事情。不知什么原因,他们正在停止发送触摸事件。我修改了代码以继续发送(因此现在发布的
应该可以工作了)。我还添加了一个
BooleanProperty
,以跟踪
TouchRipple行为是否有效

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import BooleanProperty
from kivy.uix.behaviors import TouchRippleBehavior
from kivy.uix.button import Button


class RippleButton(TouchRippleBehavior, Button):
    isRippled = BooleanProperty(False)

    def __init__(self, **kwargs):
        super(RippleButton, self).__init__(**kwargs)

    def on_touch_down(self, touch):
        collide_point = self.collide_point(touch.x, touch.y)
        if collide_point and not self.isRippled:
            self.isRippled = True
            self.ripple_show(touch)
        return super(RippleButton, self).on_touch_down(touch)

    def on_touch_up(self, touch):
        collide_point = self.collide_point(touch.x, touch.y)
        if collide_point and self.isRippled:
            self.isRippled = False
            self.ripple_fade()
        return super(RippleButton, self).on_touch_up(touch)

    def doit(self, *args):
        print('in doit')

theRoot = Builder.load_string('''
RippleButton:
    text: 'Click Here'
    on_release: self.doit()
''')

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

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

下面是一个用于创建按钮的片段,该按钮在交互时渲染触动涟漪动画:

一小条 例子 main.py 主电压(千伏)
#:kivy 1.11.0
屏幕管理器:
登录:
主屏幕:
:
名称:“登录”
RippleButton:
文本:'Login'
字体大小:24
发布时:root.manager.current=“main”
:
名称:“主要”
RippleButton:
文本:“返回”
发布时:root.manager.current=“登录”
输出

欢迎来到SO。你到底有什么问题?什么“最终不成功”?为你的问题提供更多细节,并展示一些工作证明,这样其他人可能会更好地帮助你。大家好,我仍然有连锁反应的麻烦。有人能帮忙吗?嗨,约翰·安德森,非常感谢。但是,嗯,连锁反应仍然不起作用,但是on_版本现在起作用了。不知道该怎么解决。嗨,伊科利姆,非常感谢你的帮助,真的很鼓舞人心。
class RippleButton(TouchRippleBehavior, Button):

    def on_touch_down(self, touch):
        collide_point = self.collide_point(touch.x, touch.y)
        if collide_point:
            touch.grab(self)

            # The background_color (r, g, b, a) of button widget defaults to [1, 1, 1, 1]
            # where 'a' (alpha compositing or transparency) is 1 i.e. not transparent

            self.transparency = self.background_color[3]    # backup original transparency / alpha compositing
            self.background_color[3] = 0.5  # set transparency to half (0.5)

            self.ripple_show(touch)

            # dispatch on_press event because we have consumed on_touch_down
            self.dispatch('on_press')

            # consumed touch down and don’t want it to propagate any further.
            return True
        return False

    def on_touch_up(self, touch):
        if touch.grab_current is self:
            touch.ungrab(self)
            self.ripple_fade()

            # defer on_release until ripple_fade has completed
            def defer_release(dt):
                self.background_color[3] = self.transparency  # restore transparency / alpha compositing
                self.dispatch('on_release')

            Clock.schedule_once(defer_release, self.ripple_duration_out)

            # consumed touch up and don’t want it to propagate any further.
            return True
        return False
from kivy.app import App
from kivy.uix.behaviors.touchripple import TouchRippleBehavior
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.clock import Clock


class RippleButton(TouchRippleBehavior, Button):

    def on_touch_down(self, touch):
        collide_point = self.collide_point(touch.x, touch.y)
        if collide_point:
            touch.grab(self)

            # The background_color (r, g, b, a) of button widget defaults to [1, 1, 1, 1]
            # where 'a' (alpha compositing or transparency) is 1 i.e. not transparent

            self.transparency = self.background_color[3]    # backup original transparency / alpha compositing
            self.background_color[3] = 0.5  # set transparency to half (0.5)

            self.ripple_show(touch)

            # dispatch on_press event because we have consumed on_touch_down
            self.dispatch('on_press')

            # consumed touch down and don’t want it to propagate any further.
            return True
        return False

    def on_touch_up(self, touch):
        if touch.grab_current is self:
            touch.ungrab(self)
            self.ripple_fade()

            # defer on_release until ripple_fade has completed
            def defer_release(dt):
                self.background_color[3] = self.transparency  # restore transparency / alpha compositing
                self.dispatch('on_release')

            Clock.schedule_once(defer_release, self.ripple_duration_out)

            # consumed touch up and don’t want it to propagate any further.
            return True
        return False

    def doit(self, *args):
        print('in doit')


class Login(Screen):
    pass


class MainScreen(Screen):
    pass


class SimpleKivy4(App):
    def build(self):
        return Builder.load_file("main.kv")


if __name__ == "__main__":
    SimpleKivy4().run()
#:kivy 1.11.0

ScreenManager:
    Login:
    MainScreen:

<Login>:
    name:"login"
    RippleButton:
        text:'Login'
        font_size: 24
        on_release: root.manager.current = "main"


<MainScreen>:
    name: "main"
    RippleButton:
        text: 'back'
        on_release: root.manager.current = "login"