在 Kivy 中循环浏览弹出窗口 windows

Cycling though popup windows in Kivy

我在 Kivy 中创建了一个带有 6 个切换按钮的主要 windows。 我喜欢通过长按每个切换按钮上的事件来访问具有相关设置的弹出窗口 window。

弹出窗口 window 已定义,具有 "next" 和 "previous" 按钮,可从一个设置页面循环到下一个设置页面。

如何避免在 Kivy 中手动创建这些弹出窗口定义?

虚拟 .kv 代码:

#:import Factory kivy.factory.Factory
<MyPopup2@Popup>:
    auto_dismiss: False
    title: "Popup Window No. 2"
    Button:
        text: 'Close me, too!'
        on_release: root.dismiss()


MyPopup1@Popup:
    auto_dismiss: False
    size_hint: None,None
    size: 400,300
    title: "Popup Window No. 1"
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            orientation: "vertical"
            BoxLayout:
                Label:
                    text: 'Circuit Active:'
                Switch:
                    id: "switch1"
            BoxLayout:
                Label:
                    text: 'Default Watering Time: [min]'
                TextInput:    
                    text: '30'
            BoxLayout:
                Label:
                    text: 'Watering Group'
                TextInput:    
                    text: '3'
        BoxLayout:
            Button:
                text: 'Previous'
            Button:
                text: 'Cancel'
                on_release: root.dismiss()
            Button:
                text: 'Save + Exit'
            Button:
                text: 'Next'
                on_release: root.dismiss()
                on_release: Factory.MyPopup2().open()



BoxLayout:
    orientation: "vertical"
    padding: 5

    BoxLayout:
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 1"
#               disabled: True
                on_release: Factory.MyPopup1().open()
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 2"
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 3"

    BoxLayout:
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 4"
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 5"
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 6"

    BoxLayout:
        BoxLayout:
            padding: 5
            Label:
                text: 'Drei Zeilen\nmit\nStatusmeldungen'
        BoxLayout:
            size_hint_x: 0.5
            padding: 5
            ToggleButton:
                text: "Automatik-\nBetrieb"
                on_press: app.testfunktion()

以同样的方式,当您扩展 Popup 时,您也可以扩展您创建的 MyPopup。我为前两个弹出窗口实现了它。扩展它很简单。如果您还有问题,请在评论中提问。

MyPopup 的所有功能也都在 MyPopup1 中,因为它扩展了 MyPopup。标题应该不同,每个 Popup 的上一个和下一个都不同。因此您需要指定它们。

<MyPopup1@MyPopup>:
        title: "Popup Window No. 1"
        call_previous:
        call_next: "Factory.MyPopup2().open()"

我在 MyPopup 中使用 eval 来 "evaluate" 这些语句是根的一部分。请参阅 MyPopup 中的摘录:

on_release: eval(root.call_next)

或者你也可以只给他们数字并使用类似的东西,但我会把它留给你。

#:import Factory kivy.factory.Factory
<MyPopup1@MyPopup>:
    title: "Popup Window No. 1"
    call_previous:
    call_next: "Factory.MyPopup2().open()"

<MyPopup2@MyPopup>:
    title: "Popup Window No. 2"
    call_previous: "Factory.MyPopup1().open()"
    call_next: "Factory.MyPopup2().open()"

<MyPopup@Popup>:
    auto_dismiss: False
    size_hint: None,None
    size: 400,300
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            orientation: "vertical"
            BoxLayout:
                Label:
                    text: 'Circuit Active:'
                Switch:
                    id: "switch1"
            BoxLayout:
                Label:
                    text: 'Default Watering Time: [min]'
                TextInput:    
                    text: '30'
            BoxLayout:
                Label:
                    text: 'Watering Group'
                TextInput:    
                    text: '3'
        BoxLayout:
            Button:
                text: 'Previous'
                on_release: root.dismiss()
                on_release: eval(root.call_previous)
            Button:
                text: 'Cancel'
                on_release: root.dismiss()
            Button:
                text: 'Save + Exit'
            Button:
                text: 'Next'
                on_release: root.dismiss()
                on_release: eval(root.call_next)



BoxLayout:
    orientation: "vertical"
    padding: 5

    BoxLayout:
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 1"
#               disabled: True
                on_release: Factory.MyPopup1().open()
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 2"
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 3"

    BoxLayout:
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 4"
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 5"
        BoxLayout:
            padding: 5
            ToggleButton:
                text: "Wasserkreis 6"

    BoxLayout:
        BoxLayout:
            padding: 5
            Label:
                text: 'Drei Zeilen\nmit\nStatusmeldungen'
        BoxLayout:
            size_hint_x: 0.5
            padding: 5
            ToggleButton:
                text: "Automatik-\nBetrieb"
                on_press: app.testfunktion()