python - 触摸并影响任何物体
问题描述
...我怎样才能给出函数路径?我找不到如何解决寻址问题。有人知道如何解决这个问题吗?当我只点击绿色区域时,我想做白色区域按钮的操作。...
'''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
'''
我找不到如何解决寻址问题。
解决方案
有点尴尬,但你可以通过添加一个这样的来做到这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
推荐阅读
- javascript - js async await 函数执行顺序
- python - 如何从函数返回嵌套字典
- javascript - 如何使 Shopify 集合按选项排序显示所选值?加载后返回默认的“排序依据”
- mongodb - 当有人从 db 目录中删除一个数据库目录时,如何修复 MongoDb?
- javascript - 无法推送列表的数据
- bash - 检查指定名称的数组是否包含指定值的函数
- vue.js - Vue-Router 在 beforeEach 中使用 next() 时我应该使用 return 吗?
- arrays - 修改数组中逗号分隔值的最简单方法?
- python - 如何解决:值错误:时间数据“1901 年 1 月”与格式“%Y-%m”不匹配
- xamarin - Xamarin Forms:如何更改 Collectionview SelectedItem 的文本颜色?