我几天前开始制作我的第一个kivy应用程序,一切都很好,直到一点 .
我有一个内部有GridLayout的Label和256个按钮( PaletteColorButton
),它们应该代表调色板 . 我为这个类创建了一个 on_touch_down
方法并尝试点击任何一个按钮,它们都执行 on_touch_down
中的内容 .
这是我的代码中最重要的部分:
class PaletteLabel(ToolBarLabel):
def make_palette(self, layout):
for i in range(0, 256):
palette_color_button = PaletteColorButton()
with palette_color_button.canvas:
Color(i/255, i/255, i/255, 1)
layout.add_widget(palette_color_button)
class PaletteColorButton(Button):
def on_touch_down(self, touch):
if touch.is_double_tap:
print(self.pos)
class BamEditor(App):
Config.set('kivy', 'window_icon', r'.\static\program_icon\BamEditor-icon.png')
def build(self):
main_label = MainLabel()
main_label.ids['palettelabel'].make_palette(main_label.ids['palettelayout'])
return main_label
这是来自.kv文件的数据:
<PaletteLabel>:
height: 160
width: 640
<PaletteColorButton>:
size:(20,20)
canvas.after:
Rectangle:
pos: self.x + 1 , self.y + 1
size: self.width - 2, self.height - 2
<MainLabel>:
PaletteLabel:
id: palettelabel
pos: (self.parent.x + 120, self.parent.y + 20)
GridLayout:
id: palettelayout
cols: 32
rows: 8
pos: self.parent.x , self.parent.y
size: self.parent.width, self.parent.height
我想只打印单击按钮 pos
,但我得到所有256个按钮的位置,有没有人知道如何实现这一点? Ofc,我可以使用 on_press
而且它可以工作,但我希望我的按钮在点击一次时有不同的行为,而在点击两次时则不同 . 谢谢你的帮助 .
1 回答
来自kivy编程指南:
https://kivy.org/docs/guide/inputs.html#touch-events
当您不想在此按钮中管理触摸时(因此当碰撞测试失败时),您应该将事件调度到小部件树的其余部分