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

时间:2015-02-18 07:00:47

标签: python dynamic widget kivy

我能够使用静态kivy语言处理我的布局,但我需要能够通过python将项目添加到我的列表中。我尝试了几件事,但似乎无法正常工作。这是我静态工作的。

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()

selectfruit.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\nPicked On: 12/26/2014, 2:01 PM"

    CustomButton:
        wid: "1"
        image: "ico/banana.png"
        title: "banana"
        label: "Banana: Want a bunch?\nPicked On: 2/18/2014, 2:01 PM"

    CustomButton:
        wid: "2"
        image: "ico/strawberry.png"
        title: "strawberry"
        label: "Strawberry: Yummy Yummy\nPicked On: 5/6/2014, 2:01 PM"

    CustomButton:
        wid: "3"
        image: "ico/orange.png"
        title: "orange"
        label: "Orange: Florida's Best\nPicked On: 4/21/2014, 2:01 PM"

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

2 个答案:

答案 0 :(得分:3)

这是工作代码,显示了以kivy语言添加的一些项目,然后以编程方式添加了一些其他项目。我还添加了ScrollView,一个配置设置,以防止调整窗口大小,并添加代码以突出显示所选项目。

我希望这对将来有所帮助。 :)

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\nPicked 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?\nPicked 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\nPicked 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\nPicked 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()

selectfruit.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\nPicked On: 12/26/2014, 2:01 PM"

    ButtonListItem:
        wid: "1"
        image: "ico/banana.png"
        title: "xbanana"
        label: "Banana: Want a bunch?\nPicked On: 2/18/2014, 2:01 PM"

    ButtonListItem:
        wid: "2"
        image: "ico/strawberry.png"
        title: "xstrawberry"
        label: "Strawberry: Yummy Yummy\nPicked On: 5/6/2014, 2:01 PM"

    ButtonListItem:
        wid: "3"
        image: "ico/orange.png"
        title: "xorange"
        label: "Orange: Florida's Best\nPicked On: 4/21/2014, 2:01 PM"

答案 1 :(得分:2)

def click(button):

调用方法self的第一个参数是正常的,指的是调用其方法的实例。

class SelectFruit(App, BoxLayout):

这似乎是一个灾难的秘诀,没有必要让应用程序也成为一个小工具,它有可能出现奇怪的错误,因为这意味着你将同时运行两个应用程序。您应该将其分成单独的小部件和应用程序类。

  

我需要能够通过python

将项目添加到我的列表中

我不清楚问题的确切部分,但您应该能够使用App.get_running_app().root.add_widget(your_widget)添加(例如)根小部件,其中your_widget例如是CustomButton的新实例。