本文介绍了Kivy如何在GridLayout中创建下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件,其中一个 py 和另一个 kv ,正如标题所述,我不知道如何添加下拉列表.

I have 2 files one of the py and the other kv, well as the title says, I do not know how to add a dropdown.

---> main.py

---> main.py

from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout

class MyGridLayout(GridLayout):
    pass

class LayoutsApp(App):
    def build(self):
        return MyGridLayout()


if __name__ == '__main__':
    LayoutsApp().run()

---> layouts.kv

---> layouts.kv

<MyGridLayout>:
    rows: 2
    id: main_win
    Dropdown:
        id: dropdown
        Button:
            id: btn1
            text: 'option 1'
        Button:
            id: btn2
            text: 'option 2'
    BoxLayout:

    BoxLayout:

编译时,它会为该部分生成错误.打电话给下拉列表的正确方法是什么?

when compiling, it generates error for that part. How is the correct way to make the call for dropdown-list?

推荐答案

问题3

解决方案3

Python脚本

  • 添加导入语句:from kivy.uix.dropdown import DropDown; from kivy.properties import ObjectProperty
  • 实施class CustomDropDown(DropDown):
  • 在MyGridLayout类中,在类级别声明一个ObjectProperty(例如,dropdown = ObjectProperty(None);实现方法dropdown_open(self):;实例化CustomDropDown类并将其分配给self.dropdown,依此类推.有关详细信息,请参阅代码片段.)
  • Solution 3

    Python script

    • Add import statements: from kivy.uix.dropdown import DropDown; from kivy.properties import ObjectProperty
    • Implement class CustomDropDown(DropDown):
    • In class MyGridLayout, declare an ObjectProperty at class level (e.g. dropdown = ObjectProperty(None); implement a method dropdown_open(self):; instantiate the class CustomDropDown and assigned it to self.dropdown, etc. Please refer to snippets for detail.
      • 删除self.dismiss()
      • 将动态类<CustomDropdown@DropDown>:替换为类规则<CustomDropDown>:
      • rows: 2替换为rows: 3
      • Factory.CustomDropdown().open(self)替换为root.dropdown_open(self)
      • Remove self.dismiss()
      • Replace dynamic class, <CustomDropdown@DropDown>: with class rule, <CustomDropDown>:
      • Replace rows: 2 with rows: 3
      • Replace Factory.CustomDropdown().open(self) with root.dropdown_open(self)
      from kivy.uix.dropdown import DropDown
      from kivy.properties import ObjectProperty
      
      
      class CustomDropDown(DropDown):
          pass
      
      
      class MyGridLayout(GridLayout):
          dropdown = ObjectProperty(None)
      
          def dropdown_open(self, instance):
              self.dropdown = CustomDropDown()
              self.dropdown.open(instance)
              print(self.dropdown.ids.btn2.text)
      

      layouts.kv-片段

      <CustomDropDown>:
          id: dropdown
          on_select:
              app.root.ids.btn.text = '{}'.format(args[1])
      ...
      <MyGridLayout>:
          rows: 3
          id: main_win
      
          Button:
              id: btn
              text: 'Press'
              size_hint_y: None
              height: '48dp'
              on_release:
                  root.dropdown_open(self)
      

      输出3

      注意

      您可能需要将行从2增加到3.

      • 为两个BoxLayout添加了配置
      • 为说明起见,为两个BoxLayout的画布添加了颜色
      #:kivy 1.10.1
      #:import Factory kivy.factory.Factory
      
      <CustomDropdown@DropDown>:
          id: dropdown
          on_select:
              app.root.ids.btn.text = '{}'.format(args[1])
              self.dismiss()
      
          Button:
              id: btn1
              text: 'option 1'
              size_hint_y: None
              height: '48dp'
              on_release:
                  dropdown.select(btn1.text)
      
          Button:
              id: btn2
              text: 'option 2'
              size_hint_y: None
              height: '48dp'
              on_release:
                  dropdown.select(btn2.text)
      
      <MyGridLayout>:
          rows: 2
          id: main_win
      
          Button:
              id: btn
              text: 'Press'
              on_release: Factory.CustomDropdown().open(self)
              size_hint_y: None
              height: '48dp'
      
          BoxLayout:
              orientation: 'vertical'
              size_hint: (.9,.9)
              canvas.before:
                  Color:
                      rgba: 1, 0, 0, 1    # red
                  Rectangle:
                      pos: self.pos
                      size: self.size
      
          BoxLayout:
              orientation: 'vertical'
              size_hint_y: 0.5
              canvas.before:
                  Color:
                      rgba: 0, 0, 1, 1    # blue
                  Rectangle:
                      pos: self.pos
                      size: self.size
      

      输出#2

      Output #2

      DropDown小部件类似于Popup小部件,即它们是特殊的小部件.

      DropDown widget is like Popup widget i.e. they are special widget.

      因此,创建一个具有DropDown小部件继承的动态类,并使用Factory实例化该类.

      Therefore, create a dynamic class with inheritance of DropDown widget, and use Factory to instantiate the class.

      from kivy.app import App
      from kivy.uix.gridlayout import GridLayout
      
      
      class MyGridLayout(GridLayout):
          pass
      
      
      class LayoutsApp(App):
          def build(self):
              return MyGridLayout()
      
      
      if __name__ == '__main__':
          LayoutsApp().run()
      

      layouts.kv

      #:kivy 1.10.1
      #:import Factory kivy.factory.Factory
      
      <CustomDropdown@DropDown>:
          id: dropdown
          on_select:
              app.root.ids.btn.text = '{}'.format(args[1])
              self.dismiss()
      
          Button:
              id: btn1
              text: 'option 1'
              size_hint_y: None
              height: '48dp'
              on_release:
                  dropdown.select(btn1.text)
      
          Button:
              id: btn2
              text: 'option 2'
              size_hint_y: None
              height: '48dp'
              on_release:
                  dropdown.select(btn2.text)
      
      <MyGridLayout>:
          rows: 2
          id: main_win
      
          Button:
              id: btn
              text: 'Press'
              on_release: Factory.CustomDropdown().open(self)
              size_hint_y: None
              height: '48dp'
      
          BoxLayout:
      
          BoxLayout:
      

      输出

      Output

      这篇关于Kivy如何在GridLayout中创建下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:27
查看更多