如何在GridLayout中添加小部件?(kivy+python)

dced5bon  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(140)

我如何通过python添加一个小部件?我的错误是什么?我想使用按钮添加一个小部件。如何正确应用?
python格式:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.core.window import Window
Window.size = (320, 720)

Builder.load_file('my.kv')
class Container(FloatLayout):
    def minus(self):
        self.fp.add_widget(TextInput(text="1103"))
        print("ok")

class MyApp(App):
    def build(self):
        return Container()

if __name__ == "__main__":
    MyApp().run()

kiwy格式:

<Container>
    fp: fp
    FloatLayout:
        orientation: "vertical"
        Button:
            text : "Системы"
            size_hint : (0.3, 0.1)
            pos_hint : {'x':0.05, 'y':0.9}
            font_size : "20sp"
        Button:
            text : "Локодром"
            size_hint : (0.3, 0.1)
            pos_hint : {'x':0.35, 'y':0.9}
            font_size : "20sp"
        Button:
            text : "Задачи"
            size_hint : (0.3, 0.1)
            pos_hint : {'x':0.65, 'y':0.9}
            font_size : "20sp"
        BoxLayout:
            orientation: "horizontal"
            cols: 3
            size_hint: 0.9,0.975
            pos_hint: {'center_x':.5}
            Button:
                text : "<-"
                size_hint : (0.05, 0.05)
                font_size : "20sp"
                pos_hint: {'top': 0.91}
            Label:
                text : "19.11.2022"
                size_hint : (0.2, 0.05)
                font_size : "20sp"
                pos_hint: {'top': 0.91}
            Button:
                text : "->"
                size_hint : (0.05, 0.05)
                font_size : "20sp"
                pos_hint: {'top': 0.91}
        TextInput:
            text : ""
            size_hint:(0.47, 0.05)
            pos_hint:{'x': 0.05, 'y': 0.78}
            font_size : "18sp"
        Button:
            text : "Поиск"
            size_hint:(0.2, 0.05)
            pos_hint:{'x': 0.536, 'y': 0.78}
            font_size : "15sp"
        Button:
            text : "Убрать"
            size_hint : (0.2, 0.05)
            pos_hint : {'x':0.75, 'y':0.78}
            font_size : "15sp"
        Label:
            text : "S"
            size_hint : (0.1, 0.05)
            pos_hint : {'x': 0.03, 'y': 0.72}
            font_size : "20sp"
        Label:
            text : "№"
            size_hint : (0.15, 0.05)
            pos_hint : {'x': 0.12, 'y': 0.72}
            font_size : "20sp"
        Label:
            text : "Описание"
            size_hint : (0.53, 0.05)
            pos_hint : {'x': 0.25, 'y': 0.72}
            font_size : "20sp"
        Label:
            text : "Прим."
            size_hint : (0.18, 0.05)
            pos_hint : {'x': 0.77, 'y': 0.72}
            font_size : "20sp"
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: ""
                size_hint : (0.18, 0.53)
            ScrollView:
                size_hint: 0.9,1
                pos_hint: {'center_x':.5}
                GridLayout:
                    id:fp
                    size_hint_y:None
                    height: self.minimum_height
                    cols: 4
                    cols_minimum: {0: 10, 1: 30, 2: 140, 3: 50}
                    TextInput:
                        text: 'Filler'
                        size_hint:1,None
                        height:40
            Label:
                text: ''
                size_hint : (0.18, 0.2)
        BoxLayout:
            orientation: "horizontal"
            cols: 3
            padding: 20
            Button:
                text : "Удалить"
                size_hint:(0.29, 0.07)
                pos_hint: {'top': 0.07}
                font_size : "15sp"
                on_press: root.minus()
            Button:
                text : "Добавить"
                size_hint : (0.29, 0.07)
                pos_hint: {'top': 0.07}
                font_size : "15sp"
            Button:
                text : "Сохранить"
                size_hint : (0.29, 0.07)
                pos_hint: {'top': 0.07}
                font_size : "15sp"

我想,通过引用id,我会得到一个新的小部件self.fp.add_widget(TextInput(text=“1103”))

nbnkbykc

nbnkbykc1#

你可以这样做,参见kivy doc(https://kivy.org/doc/stable/guide/widgets.html):

layout = BoxLayout(padding=10)
button = Button(text='My first button')
layout.add_widget(button)

相关问题