【问题标题】:Python / Kivy: conditional design in .kv filePython / Kivy:.kv 文件中的条件设计
【发布时间】:2017-05-03 05:55:15
【问题描述】:

在 Kivy 中是否可以使用类似于以下示例的方法?
发布的代码显然不起作用,这只是一个示例:我需要根据某个属性绘制不同的布局。

您建议如何解决这个问题?

BoxLayout:
    number: 0
    if self.number > 3:
        Label:
            text: 'number is bigger than 3'
        Button:
            text: 'click here to decrease'
            on_press: root.number -= 1
    else:
        Label:
            text: 'number is smaller than 3'
        Button:
            text: 'click here to increase'
            on_press: root.number += 1

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    我会选择 ScreenManager 或 Carousel,一个简单的例子可能是:

    Carousel: 
        index: 1# or a "certain property" :)
        scroll_timeout: 0.0 # disable the user ability to mess with the widgets layout 
        BoxLayout: #first option
            Label:
            Button:
        BoxLayout: #2nd option
            Button:
            Label:
    

    如果您将索引绑定到您选择的属性,它将自动切换布局:)...

    基于 ScreenManager 的方法将非常相似,主要变化只是绑定 current_screen 属性而不是 index

    【讨论】:

      【解决方案2】:

      KV 语言只有有限的功能,所以如果你想要更多的控制,你应该把你的逻辑放在 Python 代码中。例如,您可以将布局移动到单独的小部件中,然后使用 add_widget()remove_widget() 从 Python 代码中动态选择合适的小部件。

      from kivy.app import App
      from kivy.uix.boxlayout import BoxLayout
      from kivy.properties import NumericProperty
      from kivy.lang import Builder
      
      Builder.load_string('''
      <SubWidget1>:
          Label:
              text: 'number is bigger than 3'
          Button:
              text: 'click here to decrease'
              on_press: root.parent.number -= 1
      
      <SubWidget2>:
          Label:
              text: 'number is smaller than 3'
          Button:
              text: 'click here to increase'
              on_press: root.parent.number += 1
      
      <MyWidget>
          number: 0
      ''')
      
      class SubWidget1(BoxLayout):
          pass
      
      class SubWidget2(BoxLayout):
          pass
      
      class MyWidget(BoxLayout):
          number = NumericProperty()
      
          def __init__(self, *args):
              super(MyWidget, self).__init__(*args)
              self.widget = None
              self._create_widget()
      
          def _create_widget(self):
              print(self.number)
              if self.widget is not None:
                  self.remove_widget(self.widget)
              if self.number > 3:
                  self.widget = SubWidget1()
              else:
                  self.widget = SubWidget2()
              self.add_widget(self.widget)
      
          def on_number(self, obj, value):
              self._create_widget()
      
      class MyApp(App):
          def build(self):
              return MyWidget()
      
      if __name__ == '__main__':
          MyApp().run()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-06
        相关资源
        最近更新 更多