我当前正在尝试触发嵌套在动态类中的自定义按钮的“ 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”的版本以及检查当前状态的情况下。
最好, 朱尔兹
答案 0 :(得分:0)
on_release
或on_press
事件切换屏幕以下示例说明了如何在动态类中使用Button
的事件on_release
来切换屏幕。
注意:
如果要使用on_press
事件,请将on_release
替换为on_press
btn1_screen_name: ''
on_release:
事件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'