Kivy:将切换按钮重置为" normal"在重新进入屏幕

时间:2016-02-13 18:17:08

标签: python python-2.7 kivy

我正在开发一款kivy应用,其中包含一个带切换按钮的屏幕。我想知道如何重置这些按钮的状态,这些按钮恰好是" down"到"正常"每次用户进入该屏幕。

为了使GUI代码与应用程序的其余部分分开,我更喜欢能够从screens.kv文件中重置按钮。有办法吗?

这是我的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"

提前感谢您的时间和智慧。

1 个答案:

答案 0 :(得分:3)

Screen widget有一个名为on_enter(和on_pre_enter)的事件,在进入屏幕时会被调度。您可以重置那里按钮的状态。例如:

Screen:
    on_enter:
        button1.state = 'normal'
        button2.state = 'normal'

    GridLayout:
        cols: 1

        ToggleButton:
            id: button1

        ToggleButton:
            id: button2

您也可以循环执行此操作,但我认为它看起来不太好。