<i id='XZtf3'><tr id='XZtf3'><dt id='XZtf3'><q id='XZtf3'><span id='XZtf3'><b id='XZtf3'><form id='XZtf3'><ins id='XZtf3'></ins><ul id='XZtf3'></ul><sub id='XZtf3'></sub></form><legend id='XZtf3'></legend><bdo id='XZtf3'><pre id='XZtf3'><center id='XZtf3'></center></pre></bdo></b><th id='XZtf3'></th></span></q></dt></tr></i><div id='XZtf3'><tfoot id='XZtf3'></tfoot><dl id='XZtf3'><fieldset id='XZtf3'></fieldset></dl></div>

<tfoot id='XZtf3'></tfoot>
      • <bdo id='XZtf3'></bdo><ul id='XZtf3'></ul>
    1. <small id='XZtf3'></small><noframes id='XZtf3'>

        <legend id='XZtf3'><style id='XZtf3'><dir id='XZtf3'><q id='XZtf3'></q></dir></style></legend>

      1. kivy通过python动态添加自定义小部件到布局

        时间:2023-10-09
        <tfoot id='ZYSoE'></tfoot>

        • <small id='ZYSoE'></small><noframes id='ZYSoE'>

            <bdo id='ZYSoE'></bdo><ul id='ZYSoE'></ul>

          • <legend id='ZYSoE'><style id='ZYSoE'><dir id='ZYSoE'><q id='ZYSoE'></q></dir></style></legend>

              <tbody id='ZYSoE'></tbody>
              1. <i id='ZYSoE'><tr id='ZYSoE'><dt id='ZYSoE'><q id='ZYSoE'><span id='ZYSoE'><b id='ZYSoE'><form id='ZYSoE'><ins id='ZYSoE'></ins><ul id='ZYSoE'></ul><sub id='ZYSoE'></sub></form><legend id='ZYSoE'></legend><bdo id='ZYSoE'><pre id='ZYSoE'><center id='ZYSoE'></center></pre></bdo></b><th id='ZYSoE'></th></span></q></dt></tr></i><div id='ZYSoE'><tfoot id='ZYSoE'></tfoot><dl id='ZYSoE'><fieldset id='ZYSoE'></fieldset></dl></div>

                  本文介绍了kivy通过python动态添加自定义小部件到布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我能够让我的布局使用静态 kivy 语言,但我需要能够通过 python 将项目添加到我的列表中.我已经尝试了几件事,但似乎无法让任何东西正常工作.这是我静态工作的内容.

                  I was able to get my layout working with static kivy language but I need to be able to add items to my list via python. I've tried several things but can't seem to get anything working correctly. Here's what I have working statically.

                  main.py

                  #!/usr/bin/python
                  
                  import os
                  import kivy
                  kivy.require('1.8.0')
                  
                  from kivy.app import App
                  from kivy.core.window import Window
                  from kivy.logger import Logger
                  
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.button import Button
                  
                  class CustomButton(Button):
                      pass
                  
                      def click(button):
                          Logger.info(button.title + ": wid=" + button.wid)
                  
                  
                  class SelectFruit(App, BoxLayout):
                      icon = 'ico/fruit.png'
                      title = 'Awesome Fruit Picker'
                  
                      def build(self):
                          Window.size = 400, (4 * 78)     
                          return SelectFruit()
                  
                  if __name__ in ('__main__'):
                      SelectFruit().run()
                  

                  选择水果.kv

                  #:kivy 1.8.0
                  
                  <CustomButton@Button>:
                      wid: ""
                      image: ''
                      title: ''
                      label: ''
                      on_press: self.click()
                      BoxLayout:
                          orientation: "horizontal"
                          size: self.parent.size      # match the button's size
                          pos: self.parent.pos        # match the button's position
                          padding: 5   
                          spacing: 10
                  
                          Image:
                              size_hint: None, 1
                              source: root.image
                              size: 64, 64
                              valign: "middle"
                  
                  
                          Label:
                              size_hint: None, 1
                              text: root.label
                              valign: "middle"
                              size: 400, 64
                              text_size: self.size
                  
                  
                  <SelectFruit>
                      orientation: "vertical"
                      padding: 2
                  
                      CustomButton:
                          wid: "0"
                          image: "ico/apple.png"
                          title: "apple"
                          label: "Apple: Super Sweet
                  Picked On: 12/26/2014, 2:01 PM"
                  
                      CustomButton:
                          wid: "1"
                          image: "ico/banana.png"
                          title: "banana"
                          label: "Banana: Want a bunch?
                  Picked On: 2/18/2014, 2:01 PM"
                  
                      CustomButton:
                          wid: "2"
                          image: "ico/strawberry.png"
                          title: "strawberry"
                          label: "Strawberry: Yummy Yummy
                  Picked On: 5/6/2014, 2:01 PM"
                  
                      CustomButton:
                          wid: "3"
                          image: "ico/orange.png"
                          title: "orange"
                          label: "Orange: Florida's Best
                  Picked On: 4/21/2014, 2:01 PM"
                  

                  我只需要能够以编程方式将每个 CustomButton 添加到我的布局中,而不是通过 kivy 语言文件.非常感谢任何帮助.

                  I just need to be able to add each CustomButton programmatically to my layout rather than via the kivy language file. Any help is greatly appreciated.

                  推荐答案

                  下面是工作代码,显示了一些以 kivy 语言添加的项目,然后以编程方式添加了一些附加项目.我还添加了 ScrollView,这是一个配置设置以防止窗口被调整大小和代码以突出显示选定的项目.

                  Here's the working code showing some items added in kivy language and then some additional items added programmatically. I also added the ScrollView, a configuration setting to keep the window from being resized and code to highlight the selected item.

                  我希望这对将来的某人有所帮助.:)

                  I hope this is helpful to someone in the future. :)

                  main.py

                  #!/usr/bin/python
                  
                  from kivy.config import Config
                  Config.set('graphics','resizable',0)
                  
                  import kivy
                  kivy.require('1.8.0')
                  
                  from kivy.app import App
                  from kivy.core.window import Window
                  from kivy.properties import ObjectProperty, StringProperty
                  from kivy.logger import Logger
                  
                  from kivy.uix.scrollview import ScrollView
                  from kivy.uix.gridlayout import GridLayout
                  from kivy.uix.button import Button
                  
                  
                  class ButtonListItem(Button):
                      wid = StringProperty('')
                      image = StringProperty('')
                      title = StringProperty('')
                      label = StringProperty('')
                      pass
                  
                      def click(button):
                          global app
                          app.clearSelection()
                          button.background_color = (0,160,66,.9)
                          Logger.info(button.title + ": wid=" + button.wid)
                  
                  class ButtonList(GridLayout):
                      pass
                  
                  class SelectFruit(App):
                      icon = 'ico/fruit.png'
                      title = 'Awesome Fruit Picker'
                  
                      def build(self):
                          Window.size = 400, (4 * 90)
                  
                          self.layout = ButtonList()
                          self.layout.size = 400, (8 * 78)
                  
                          self.root = ScrollView(
                                          size_hint=(None, None), 
                                          size=Window.size,
                                          scroll_type=['bars', 'content']
                                      )
                          self.root.add_widget(self.layout)
                  
                          ib = ButtonListItem(
                                  wid="0", 
                                  image="ico/apple.png", 
                                  title="apple", 
                                  label="Apple: Super Sweet
                  Picked On: 12/26/2014, 2:01 PM"
                              )
                          self.layout.add_widget(ib)
                  
                          ib = ButtonListItem(
                                  wid="1", 
                                  image="ico/banana.png", 
                                  title="banana", 
                                  label="Banana: Want a bunch?
                  Picked On: 2/18/2014, 2:01 PM"
                              )
                          self.layout.add_widget(ib)
                  
                          ib = ButtonListItem(
                                  wid="2", 
                                  image="ico/strawberry.png", 
                                  title="strawberry", 
                                  label="Strawberry: Yummy Yummy
                  Picked On: 5/6/2014, 2:01 PM"
                              )
                          self.layout.add_widget(ib)
                  
                          ib = ButtonListItem(
                                  wid="3", 
                                  image="ico/orange.png", 
                                  title="orange", 
                                  label="Orange: Florida's Best
                  Picked On: 4/21/2014, 2:01 PM"
                              )
                          self.layout.add_widget(ib)
                  
                          return self.root
                  
                      def clearSelection(self):
                          for child in self.layout.children:
                              child.background_color = (1,1,1,1)
                  
                  if __name__ == "__main__":
                      app = SelectFruit()
                      app.run()
                  

                  选择水果.kv

                  #:kivy 1.8.0
                  
                  <ButtonListItem@Button>:
                      wid: self.wid
                      image: self.image
                      title: self.title
                      label: self.label
                      on_press: self.click()
                      BoxLayout:
                          orientation: "horizontal"
                          size: self.parent.size      # match the button's size
                          pos: self.parent.pos        # match the button's position
                          padding: 5   
                          spacing: 10
                  
                          Image:
                              size_hint: None, 1
                              source: root.image
                              size: 64, 64
                              valign: "middle"
                  
                  
                          Label:
                              size_hint: None, 1
                              text: root.label
                              valign: "middle"
                              size: 400, 64
                              text_size: self.size
                  
                  
                  <ButtonList@GridLayout>
                      id: output
                      cols: 1
                      size_hint_y: None
                      height: self.minimum_height
                  
                      ButtonListItem:
                          wid: "0"
                          image: "ico/apple.png"
                          title: "xapple"
                          label: "Apple: Super Sweet
                  Picked On: 12/26/2014, 2:01 PM"
                  
                      ButtonListItem:
                          wid: "1"
                          image: "ico/banana.png"
                          title: "xbanana"
                          label: "Banana: Want a bunch?
                  Picked On: 2/18/2014, 2:01 PM"
                  
                      ButtonListItem:
                          wid: "2"
                          image: "ico/strawberry.png"
                          title: "xstrawberry"
                          label: "Strawberry: Yummy Yummy
                  Picked On: 5/6/2014, 2:01 PM"
                  
                      ButtonListItem:
                          wid: "3"
                          image: "ico/orange.png"
                          title: "xorange"
                          label: "Orange: Florida's Best
                  Picked On: 4/21/2014, 2:01 PM"
                  

                  这篇关于kivy通过python动态添加自定义小部件到布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 kivy 中,如何控制 TextInput 使用的 VKeyboard 的高 下一篇:在 Kivy 中创建全局变量

                  相关文章

                  最新文章

                  <tfoot id='DGHLb'></tfoot>

                  <small id='DGHLb'></small><noframes id='DGHLb'>

                • <i id='DGHLb'><tr id='DGHLb'><dt id='DGHLb'><q id='DGHLb'><span id='DGHLb'><b id='DGHLb'><form id='DGHLb'><ins id='DGHLb'></ins><ul id='DGHLb'></ul><sub id='DGHLb'></sub></form><legend id='DGHLb'></legend><bdo id='DGHLb'><pre id='DGHLb'><center id='DGHLb'></center></pre></bdo></b><th id='DGHLb'></th></span></q></dt></tr></i><div id='DGHLb'><tfoot id='DGHLb'></tfoot><dl id='DGHLb'><fieldset id='DGHLb'></fieldset></dl></div>
                • <legend id='DGHLb'><style id='DGHLb'><dir id='DGHLb'><q id='DGHLb'></q></dir></style></legend>
                      <bdo id='DGHLb'></bdo><ul id='DGHLb'></ul>