【问题标题】:Cycling though popup windows in Kivy通过 Kivy 中的弹出窗口循环
【发布时间】:2017-09-18 02:15:37
【问题描述】:

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

弹出窗口已定义,并具有“下一个”和“上一个”按钮,可从一个设置页面循环到下一个设置页面。

如何避免在 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()

【问题讨论】:

  • 您是否要为其余五个按钮重复 MyPopup1 和 MyPopup2?
  • 是的,正确的。我喜欢为六个花园浇水阀设置 6 个相同的设置弹出窗口,并且我希望有“下一个”和“上一个”按钮从一个弹出窗口转到另一个,而无需关闭并通过主屏幕。

标签: python kivy popupwindow


【解决方案1】:

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

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

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

我在 MyPopup 中使用 eval 来“评估”这些属于根目录的语句。请参阅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()

【讨论】:

  • 谢谢。我希望有一种更通用的方法可以为弹出窗口提供“上下文”并避免代码重复。
  • 上下文是什么意思?我认为每个 Popup 有 4 行非常好。我没有看到任何代码重复。您可以像我一样定义更多属性,以制作适合您需求的小部件。
  • 对不起,我想我现在明白了。完整的内容被继承到不同的弹出窗口中。
  • 我用解释更新了我的答案,希望对您有所帮助。
  • 这回答了您的问题吗?如果是这样考虑接受,或者如果它只是帮助你,你可以考虑支持它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2019-04-14
  • 2018-02-13
  • 2011-09-30
  • 2014-07-29
相关资源
最近更新 更多