【问题标题】:touch and effect any object触摸并影响任何物体
【发布时间】:2019-10-26 11:37:13
【问题描述】:

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

'''蟒蛇 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()

''' ...

runner.kv

...

'''kivy

<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 

'''

我找不到解决寻址的方法。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    您需要检查触摸是否真的与您关心的区域发生碰撞。

    if self.collide_point(*touch.pos):
        self.ids.secbut.disabled = True
    

    【讨论】:

    • 谢谢,但我收到了警告。 AttributeError: 'super' object has no attribute 'getattr' 在一个类中的绿色区域,按钮在另一个类中。
    • Vec 中的on_touch_down() 方法中,您必须使用Sec 的实例才能访问ids,但您使用的是类,而不是实例。
    • @JohnAnderson 我明白,但我不知道怎么做。我是这个页面的新手。我自己在学习 Pthon..
    【解决方案2】:

    有点尴尬,但您可以通过将 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:
    

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2012-10-06
      • 1970-01-01
      • 2011-02-21
      相关资源
      最近更新 更多