首页 > 解决方案 > 触摸并影响任何物体

问题描述

...我怎样才能给出函数路径?我找不到如何解决寻址问题。有人知道如何解决这个问题吗?当我只点击绿色区域时,我想做白色区域按钮的操作。...

'''python main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput



class Area(RelativeLayout):
    pass

class Sec(RelativeLayout):
    """ This is white area """
    def chn(self):
        self.ids.secbut.disabled = True

class Vec(RelativeLayout):
    """ This is green area
    I want the button to disable when the green area is clicked.
    """
    def on_touch_down(self,touch):
        if self.collide_point(*touch.pos):
            Sec.ids.secbut.disabled = True

class runner(App):

    def build(self):
        self.area = Area()
        return self.area

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

''' ...

亚军.kv

...

'''基维

<Area>:
    size_hint: 1, 1
    pos_hint: {"x":0,"y":0}
    canvas:
        Color:
            rgba:0.9,0.9,0.9,1
        Rectangle:
            size:self.size    
    Sec:
    Vec:


<Sec>:
    size_hint: 1, 0.5
    pos_hint: {"x":0,"y":0}
    canvas:
        Color:
            rgba:0.9,0.8,0.7,1
        Rectangle:
            size:self.size    
    Button:
        id:secbut
        text:"click"
        size_hint:0.5,0.5

<Vec>:
    size_hint: 1, 0.5
    pos_hint: {"x":0,"y":0.5}
    canvas:
        Color:
            rgba:0.2,0.8,0.1,1
        Rectangle:
            size:self.size 

'''

我找不到如何解决寻址问题。

标签: pythonkivy

解决方案


有点尴尬,但你可以通过添加一个这样的来做到这id一点Sec

<Area>:
    size_hint: 1, 1
    pos_hint: {"x":0,"y":0}
    canvas:
        Color:
            rgba:0.9,0.9,0.9,1
        Rectangle:
            size:self.size    
    Sec:
        id: sec
    Vec:

然后在你的on_touch_down()in 中使用它Vec

class Vec(RelativeLayout):
    """ This is green area
    I want the button to disable when the green area is clicked.
    """
    def on_touch_down(self,touch):
        if self.collide_point(*touch.pos):
            App.get_running_app().root.ids.sec.ids.secbut.disabled = True

推荐阅读