RippleButton类在一个python脚本中工作,但在使用屏幕时不工作

2024-10-03 21:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

当我用rippleexample2.kv运行这个脚本rippleexample2.py时,按钮应该在按下时产生涟漪效果,但它不起作用。我知道RippleButton类在ctmbtn.py中运行良好,当按下此处的按钮时,会产生涟漪效应。我不知道这里怎么了。也许是绑定功能?提前谢谢

涟漪示例2.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)


class Login(Screen):
    pass

class MainScreen(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

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

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

if __name__ == "__main__":
    SimpleKivy4().run()

波纹示例2.kv

ScreenManager:
    Login:
    MainScreen:

<Login>:
    name:"login"
    RippleButton:
        text:'Login'
        font_size: 24
        size_hint: (.4,.25)
        on_release: app.root.current = "main"

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

ctmbtn.py公司

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import (StringProperty, NumericProperty, ObjectProperty,
ListProperty, DictProperty, BooleanProperty)
from kivy.uix.touchripple import TouchRippleBehavior

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)


class RootWidget(BoxLayout):
    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.add_widget(RippleButton(text='btn 1'))
        cb = CustomBtn()
        cb.bind(pressed=self.btn_pressed)
        self.add_widget(cb)
        self.add_widget(RippleButton(text='btn 2'))

    def btn_pressed(self, instance, pos):
        print ('pos: printed from root widget: {pos}'.format(pos=pos))

class CustomBtn(Widget):
    pressed = ListProperty([0, 0])

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            self.pressed = touch.pos
            # we consumed the touch. return False here to propagate
            # the touch further to the children.
            return True
        return super(CustomBtn, self).on_touch_down(touch)

    def on_pressed(self, instance, pos):
        print ('pressed at {pos}'.format(pos=pos))

class TestApp(App):
    def build(self):
        return RootWidget()

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

Tags: fromposimportselfreturnondefclass
1条回答
网友
1楼 · 发布于 2024-10-03 21:27:06

Touch-Ripple代码仍然是实验性的,在按钮小部件的kv语言中使用它时是不可见的

实际上,当使用kv语言时,交互上的按钮触动涟漪动画是有效的。它只是不可见,因为Button widget的background_color(r,g,b,a)默认为[1,1,1,1]或灰色,没有透明度。其中“a”(α合成或透明度)为1,即不透明

波纹颜色默认为[1.,1.,1.,.5]或半透明的灰色

解决方案

临时解决办法是将按钮小部件的alpha合成或透明度更改为0.5

请参考我的例子Kivy RippleButton

相关问题 更多 >