向循环中的按钮添加释放动作

2024-10-01 00:16:50 发布

您现在位置:Python中文网/ 问答频道 /正文

class WidgetFiscal(Screen):
    box = ObjectProperty(None)

    def on_box(self, *args):
        fiscal = ['Elzab Mera TE FV', 'Posnet Thermal XL', 'Posnet HD', 'Elzab Sigma', 'Novitus Delio Prime E', 'Elzab D10', 'Posnet Trio', 'Epson TM-T801FV']
        for i in fiscal:
            self.box.add_widget(Button(text=str(i), background_color=[1,2,1,1]))

我的.kv文件:

<FiscalPrinter>:
    name: 'fiscal_printer'

    BoxLayout:
        size: root.size
        spacing: 20
        padding: 10,10,10,10
        orientation: 'vertical'

        Label:
            text: 'Choose fiscal printer which you want to rent'
            size: root.width, root.height / 10
            size_hint: None, None

        WidgetFiscal:

        Button:
            text: 'GO BACK'
            size: root.width, root.height / 10
            size_hint: None, None
            on_release: app.root.current = "rent_device"

<WidgetFiscal>:
    box: box

    GridLayout:
        background_color: 1,2,1,1
        cols: 3
        id: box

Tags: textselfboxnonesizeonbuttonroot
1条回答
网友
1楼 · 发布于 2024-10-01 00:16:50

正在将on_release事件添加到Button小部件

self.box.add_widget(Button(..., on_release=self.mycallback))

注意事项

Kivy » Touch event basics

By default, touch events are dispatched to all currently displayed widgets. This means widgets receive the touch event whether it occurs within their physical area or not.

...

In order to provide the maximum flexibility, Kivy dispatches the events to all the widgets and lets them decide how to react to them. If you only want to respond to touch events inside the widget, you simply check:

def on_touch_down(self, touch):
    if self.collide_point(*touch.pos):
        # The touch has occurred inside the widgets area. Do stuff!
        pass

解决方案

因此,您希望使用Button小部件的继承来定义class PrinterButton,并实现on_touch_down方法以仅响应Button触摸事件

片段

class PrinterButton(Button):
    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print(f"\nPrinterButton.on_touch_down: text={self.text}")
            self.dispatch('on_release')
            return True    # consumed on_touch_down & stop propagation / bubbling
        return super(PrinterButton, self).on_touch_down(touch)

class WidgetFiscal(Screen): 
    box = ObjectProperty(None)

    def on_box(self, *args):
        fiscal = ['Elzab Mera TE FV', 'Posnet Thermal XL', 'Posnet HD', 'Elzab Sigma', 'Novitus Delio Prime E', 'Elzab D10', 'Posnet Trio', 'Epson TM-T801FV']
        for i in fiscal:
            self.box.add_widget(PrinterButton(text=str(i), background_color=[1,2,1,1], on_release=self.mycallback))

    def mycallback(self, instance):
        print(f"mycallback: Button.text={instance.text}")

输出

Result

相关问题 更多 >