Kivy:将切换按钮重置为“正常"在重新进入屏幕上 [英] Kivy: resetting toggle buttons to "normal" on re-entering screen
问题描述
我正在开发一个 kivy 应用程序,其中包含一个带有切换按钮的屏幕.我想知道每次用户进入该屏幕时如何将这些碰巧向下"的任何按钮的状态重置为正常".
I'm working on a kivy app that includes a screen with toggle buttons in it. I'd like to know how to reset the state of any of these buttons that happen to be "down" to "normal" every time the user enters that screen.
为了将 GUI 代码与应用程序的其余部分分开,我希望能够从我的 screen.kv 文件中重置按钮.有没有办法这样做?
In order to keep the GUI code separate from the rest of the application, I'd prefer to be able to reset the buttons from within my screens.kv file. Is there a way of doing so?
这是我的screens.kv 的相关部分:
Here's the releveant section of my screens.kv:
# Solution code, based on przyczajony's answer:
<ScreenThree>:
on_enter:
button1.state = 'normal'
button2.state = 'normal'
button3.state = 'normal'
button4.state = 'normal'
button5.state = 'normal'
#End of solution code, beginning of original question code:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
Label:
id: label
text: root.explanationText
ToggleButton:
id: button1
text: root.button1Text
on_state:
if self.state == 'normal': root.button1Up()
else: root.button1Down()
ToggleButton:
id: button2
text: root.button2Text
on_state:
if self.state == 'normal': root.button2Up()
else: root.button2Down()
ToggleButton:
id: button3
text: root.button3Text
on_state:
if self.state == 'normal': root.button3Up()
else: root.button3Down()
ToggleButton:
id: button4
text: root.button4Text
on_state:
if self.state == 'normal': root.button4Up()
else: root.button4Down()
ToggleButton:
id: button5
text: root.button5Text
on_state:
if self.state == 'normal': root.button5Up()
else: root.button5Down()
Button:
id: button6
text: root.button6Text
on_release: root.manager.current = "screen4"
提前感谢您的时间和智慧.
Thanks in advance for your time and wisdom.
推荐答案
屏幕小部件 有一个名为 on_enter
(和 on_pre_enter
)的事件,它在进入屏幕时被调度.您可以在那里重置按钮的状态.示例:
Screen widget has an event called on_enter
(and on_pre_enter
), which is dispatched upon entering a screen. You can reset state of the buttons there. Example:
Screen:
on_enter:
button1.state = 'normal'
button2.state = 'normal'
GridLayout:
cols: 1
ToggleButton:
id: button1
ToggleButton:
id: button2
您也可以循环执行此操作,但我认为它看起来不太好.
You could also do this in a loop, but I think it wouldn't look good.
这篇关于Kivy:将切换按钮重置为“正常"在重新进入屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!