【问题标题】:Kivy - Using the "on_release" event in parent WidgetKivy - 在父 Widget 中使用“on_release”事件
【发布时间】:2019-05-09 13:12:44
【问题描述】:

我目前正在尝试触发嵌套在动态类中的自定义按钮的“on_release”事件。更加具体。这是我当前实现的示例 kv 文件:

#:kivy 1.10.1
#:include gui/components.kv

<MiddleSectionMain@AnchorLayout>:
    anchor_x: 'center'
    anchor_y: 'center'

    lb_text: ''
    text: spinner_1.text
    stateb1: button_1.state 

    BoxLayout:
        orientation: 'horizontal'
        Label:
            text: root.lb_text
            size_hint_x: 0.3
        Spinner:
            id: spinner_1
            text: 'Select'
        IconButtonSmall:
            id: button_1
            icon_source: 'icons/add_32.png'
        IconButtonSmall:
            id: button_2
            icon_source: 'icons/edit_32.png'


<MainScreen>:
    orientation: 'vertical'

    Label:
        font_size: 25
        text: "Headline"

    GridLayout:
        cols: 1

        MiddleSectionMain:
            lb_text: "Label"
            on_stateb1: 
                if self.stateb1 == 'normal': app.root.current = 'Screen2'

        MiddleSectionMain:
            lb_text: "Label"

IconButtonSmall 在另一个 .kv 文件 (gui/components.kv) 中定义。

这个实现是可行的,但我对它并不满意,因为按钮也提供了“on_release”事件。因此,如果有办法使用“on_release”事件而不是使用“on_state”以及检查当前状态的我的版本,我会徘徊。

最好, 朱兹

【问题讨论】:

  • 您是在问如何使MiddleSectionMain 具有类似按钮的行为?或者如何从您的IconButtonSmalls 更改为Screen2?
  • 嗨,我想根据按下的两个 IconButtonSmall 中的哪一个更改为不同的屏幕(一个“添加”屏幕,我们将使用 Screen2 和一个“编辑”屏幕(此处未显示))。我认为 ikolims 的回答将完全符合我的要求。

标签: python python-3.x kivy kivy-language


【解决方案1】:

假设-使用on_release或on_press事件切换屏幕

以下示例说明如何在动态类中使用Button的事件on_release进行屏幕切换。

注意: 如果要使用on_press 事件,请将on_release 替换为on_press

  1. 为每个按钮声明 StringProperty,例如btn1_screen_name: ''
  2. 为每个按钮实现on_release: 事件
  3. 初始化btn1_screen_name: 'Screen2'

片段

<MiddleSectionMain@AnchorLayout>:
    btn1_screen_name: ''
    btn2_screen_name: ''
    ...
    BoxLayout:
        IconButtonSmall:
            id: button_1
            icon_source: 'icons/add_32.png'
            on_release: app.root.current = root.btn1_screen_name
        IconButtonSmall:
            id: button_2
            icon_source: 'icons/edit_32.png'
            on_release: app.root.current = root.btn2_screen_name


<MainScreen>:
    BoxLayout:
        ...
        GridLayout:
            cols: 1

            MiddleSectionMain:
                lb_text: "Label"
                btn1_screen_name: 'Screen2'
                btn2_screen_name: 'MainScreen'

            MiddleSectionMain:
                lb_text: "Label"
                btn1_screen_name: 'Screen3'
                btn2_screen_name: 'MainScreen'

【讨论】:

  • 再次感谢您:)
猜你喜欢
  • 2015-11-14
  • 2022-01-22
  • 1970-01-01
  • 2018-07-13
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多