Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在特定坐标/小部件上添加Python Kivy Touch Ripple?_Python_Python 3.x_Kivy - Fatal编程技术网

如何在特定坐标/小部件上添加Python Kivy Touch Ripple?

如何在特定坐标/小部件上添加Python Kivy Touch Ripple?,python,python-3.x,kivy,Python,Python 3.x,Kivy,我当时正在使用按钮,希望添加触摸涟漪效果。我能够在整个屏幕上产生涟漪效应,但不能在预先指定的坐标或仅限于小部件。任何帮助都将不胜感激,提前感谢,这真的意义重大。 注意-backg.png只是一个普通的蓝色背景图像 from kivy.app import App from kivy.config import Config Config.set('input', 'mouse', 'mouse,multitouch_on_demand') from kivy.uix.screenmanager

我当时正在使用按钮,希望添加触摸涟漪效果。我能够在整个屏幕上产生涟漪效应,但不能在预先指定的坐标或仅限于小部件。任何帮助都将不胜感激,提前感谢,这真的意义重大。 注意-backg.png只是一个普通的蓝色背景图像

from kivy.app import App
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.graphics.vertex_instructions import RoundedRectangle
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.behaviors.touchripple import TouchRippleBehavior

Builder.load_string("""
<MainScreen>:
    BoxLayout:
        FloatLayout:
            Image:
                size_hint: 1, 1
                source: "backg.png"
                pos_hint: {'x':.0, 'y':.0}
                keep_ratio: False
                allow_stretch: True
            Widget:
                canvas.before:
                    Color:
                        rgba: 0, 0, 255, 1
                    RoundedRectangle:
                        size: 230, 50
                        segments: 10
                        radius: [(10.0, 10.0), (10.0, 10.0), (10.0, 10.0), (10.0, 10.0)]
                        pos: 20, 500
            Widget:
                canvas.before:
                    Color:
                        rgba: 0, 0, 255, 1
                    RoundedRectangle:
                        size: 50, 50
                        segments: 10
                        radius: [(25.0, 25.0), (25.0, 25.0), (25.0, 25.0), (25.0, 25.0)]
                        pos: 20, 100
            RipFunction:
            Button:
                text: ''
                size_hint: 0.2, 0.1
                pos_hint: {'x':.4, 'y':.5}
                background_color: 1.0, 1.0, 1.0, 0.5
                on_press:
""")

Window.size = (270, 570)


class RipFunction(TouchRippleBehavior, Widget):

    def on_touch_down(self, touch):
        # print(touch)
        collide_point = self.collide_point(touch.x, touch.y)
        # collide_point = self.collide_point(touch[0], touch[1])
        if collide_point:
            touch.grab(self)
            self.ripple_duration_in = 0.7
            self.ripple_scale = 0.1
            self.ripple_show(touch)
            return True
        return False

    def on_touch_up(self, touch):
        if touch.grab_current is self:
            touch.ungrab(self)
            self.ripple_duration_out = 0.4
            self.ripple_fade()
            return True
        return False

class MainScreen(Screen):
    pass


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

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


您的代码工作正常,涟漪效应仅限于RipFunction小部件。问题是您没有设置RIP函数的大小或位置,因此它使用默认位置0,0和默认大小提示1,1。因此,RIP函数将填充整个显示。尝试修改您的kv:


谢谢约翰,这件事困扰了我两天。我现在拿到了。干杯
        RipFunction:
            size_hint: None, None
            size: 50, 50
            pos: 20, 150
            canvas.before:
                Color:
                    rgba: 1,0,0,1
                Rectangle:
                    pos: self.pos
                    size: self.size