我如何将小部件添加到FloatLayout内ScrollView内的StackLayout中?

时间:2019-02-06 04:29:47

标签: python kivy kivy-language

对奇异鸟来说是新手,只接受过python的高中教育,现在已经坚持了一周。我想要一个可以在StackLayout中添加和删除按钮的系统,我该怎么做?

我可以添加和删除按钮,现在只需要弄清楚如何将它们放置在正确的区域即可。

我知道这个问题有点儿混乱,我对此深有感触,但我认为最好的方法是学习就是直接研究它。

pythton代码:

# import kivy & functions/widgets.
import kivy
from kivy.app import App
from kivy.uix.button import Button

# import kivy layouts.
from kivy.uix.floatlayout import FloatLayout

# Specify version of kivy needed.
kivy.require("1.10.1")

# define buttons and widgets
testButton = Button(width=177, height=254, size_hint=(None, None), background_normal="pics/32864.jpg")


class Page(FloatLayout):
    def __init__(self):
        super().__init__()

    def add_button(self):
        Page.add_widget(self, widget=testButton)


class YuGiOhApp(App):
    pass


YuGiOhApp().run()

我的.kv代码:

#:kivy 1.10.1

<TestButton@Button>:
    width: 177
    height: 254
    size_hint: None, None
    background_normal: "pics/32864.jpg"

FloatLayout:

    Button:
        size_hint: 0.20, 0.10
        pos_hint: {"x": 0.60, "top": 1}
        text: "Search"
        on_press: root.add_button()

    Button:
        size_hint: 0.20, 0.10
        pos_hint: {"x": 0.80, "top": 1}
        text: "collection"

    TextInput:
        multiline: False
        font_size: 48
        size_hint: 0.60, 0.10
        pos_hint: {"x": 0, "top": 1}

    ScrollView:
        size_hint: 0.60, 0.90

        StackLayout:
            orientation: "lr-tb"
            pos_hint: {"x": 0, "top": 0.88}
            size_hint: 1, None

            height: self.minimum_height

            padding: 5
            spacing: 5

            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:

编辑4:尝试在按下标有“搜索”的按钮时尝试运行add_button()函数时,出现错误“ AttributeError:'FloatLayout'对象没有属性'add_button'”

2 个答案:

答案 0 :(得分:1)

您可以为StackLayout小部件赋予唯一的id

StackLayout:
    id: your_stacklayout_id
    ...

然后访问您的StackLayout小部件,您可以在id小部件内使用其root

#: import Factory kivy.factory.Factory

FloatLayout:

    ...

    Button:
        text: "Add new Button"
        on_press: root.ids["your_stacklayout_id"].add_widget(Factory.TestButton())

    ...

这是一个基本示例:

main.py

from kivy.app import App
from kivy.uix.boxlayout import *
from kivy.uix.button import *

class Main(BoxLayout):
    pass

class TestApp(App):
    def build(self):
        return Main()

TestApp().run()

test.kv

#: import Factory kivy.factory.Factory

<MyButton@Button>:
    text: "Added Button"

<Main>:
    BoxLayout:
        orientation: "vertical"

        Button:
            text: "Add New Button"
            on_press: root.ids["my_boxlayout_id"].add_widget(Factory.MyButton())

        BoxLayout:
            id: my_boxlayout_id
            orientation: "vertical"

答案 1 :(得分:1)

您的代码有一些问题。

首先,当您似乎确实想引用您的kv类时,您的FloatLayout引用了Page。 (您引用了root.add_button(),但是该方法在Page类中。)

第二,您的testbuttonTestButton的单个实例,因此,第二次单击Search按钮时,您会收到有关该按钮已经具有父级的错误。因此,我在python代码中创建了一个TestButton类,并从<TestButton@Button>中删除了kv规则。

这是我想为您的代码提供的内容:

# import kivy & functions/widgets.
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button

# import kivy layouts.
from kivy.uix.floatlayout import FloatLayout

# Specify version of kivy needed.
kivy.require("1.10.1")

# define buttons and widgets
class TestButton(Button):
    def __init__(self, **kwargs):
        super(TestButton, self).__init__(**kwargs)
        self.width = 177
        self.height = 254
        self.size_hint = (None, None)
        self.background_normal = "pics/32864.jpg"


class Page(FloatLayout):
    def __init__(self):
        super().__init__()

    def add_button(self):
        self.ids.stack.add_widget(TestButton())



class YuGiOhApp(App):
    def build(self):
        return Page()


YuGiOhApp().run()

并在id中为您的StackLayout添加一个kv

#:kivy 1.10.1
<Page>:

    Button:
        size_hint: 0.20, 0.10
        pos_hint: {"x": 0.60, "top": 1}
        text: "Search"
        on_press: root.add_button()

    Button:
        size_hint: 0.20, 0.10
        pos_hint: {"x": 0.80, "top": 1}
        text: "collection"

    TextInput:
        multiline: False
        font_size: 48
        size_hint: 0.60, 0.10
        pos_hint: {"x": 0, "top": 1}

    ScrollView:
        size_hint: 0.60, 0.90

        StackLayout:
            id: stack
            orientation: "lr-tb"
            pos_hint: {"x": 0, "top": 0.88}
            size_hint: 1, None

            height: self.minimum_height

            padding: 5
            spacing: 5

            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:
            TestButton:

新的add_button()方法引用新的stack ID来访问StackLayout,并在每次调用时创建一个新的TestButton