Kivy:在重新进入的屏幕上将切换按钮重置为“正常”

2024-06-26 17:02:39 发布

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

我正在开发一个kivy应用程序,其中包括一个带有切换按钮的屏幕。我想知道如何重新设置这些按钮的状态,这些按钮碰巧是“向下”到“正常”,每次用户进入那个屏幕。在

为了使GUI代码与应用程序的其余部分分开,我希望能够从屏幕.kv文件。有办法吗?在

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

提前感谢你的时间和智慧。在


Tags: textselfid应用程序if屏幕oncode
1条回答
网友
1楼 · 发布于 2024-06-26 17:02:39

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

你也可以循环做这个,但我觉得不太好。在

相关问题 更多 >