python - Kivy buttons not working as intended -
i've started making first kivy app few days ago , went fine till 1 point.
i have label has gridlayout inside , 256 buttons (palettecolorbutton
) supposed represent palette. created on_touch_down
method class , try click on of buttons, execute what's inside of on_touch_down
.
here's important part of code:
class palettelabel(toolbarlabel): def make_palette(self, layout): in range(0, 256): palette_color_button = palettecolorbutton() 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
and here's data .kv file:
<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
i want have clicked button's pos
printed, instead positions of 256 buttons, know how achieve that? ofc, can use on_press
instead , works, i'd buttons have different behavior when tapped once , different when tapped twice. thank help.
from kivy programming guide:
by default, touch events dispatched displayed widgets. means widgets receive touch event whether occurs within physical area or not.
[…]
in order provide maximum flexibility, kivy dispatches events widgets , lets them decide how react them. if want respond touch events inside widget, check:
def on_touch_down(self, touch): if self.collide_point(*touch.pos): # touch has occurred inside widgets area. stuff! pass
https://kivy.org/docs/guide/inputs.html#touch-events
when don't want manage touch in button, (so when collision test fails), should let event dispatch rest of widget tree
return super(widgetclass, self).on_touch_down(touch)
Comments
Post a Comment