【问题标题】:how to add a Label and button in grid layout with user input in kivy python如何在 kivy python 中使用用户输入在网格布局中添加标签和按钮
【发布时间】:2021-07-25 19:35:08
【问题描述】:

我是 kivy 的新手……我想制作一个待办事项列表应用程序……并想添加一个来自用户的任务名称和一个可以在按下时获得刻度标记的按钮……

我是如何在屏幕上获得标签的,但我无法获得它。

这个 main.py

class ListWidget(RecycleView):

    def update(self):
        self.data = [{'text': str(item)}for item in self.item]


    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.item = []

class RootWidget(BoxLayout):

    inputbutton = ObjectProperty(None).

    inputcontent = ObjectProperty(None).

    outputcontent = ObjectProperty(None).

    def add_item(self):
        if self.inputcontent.text != " ":
            formatted = f'\n*{self.inputcontent.text}'
            self.outputcontent.item.append(formatted)
            self.outputcontent.update()
            self.inputcontent.text = ""

class MyApp(App):

    def build(self):

        return RootWidget()

MyApp().run()

这是我的.kv 文件

<RootWidget>

    inputbutton: inputbutton

    inputcontent: inputcontent

    outputcontent: outputcontent

    orientation: 'vertical'

    BoxLayout:
        orientation: 'vertical'
        size_hint: 1, 0.25

        Label:
            text: 'TO-DO'
            font_size: 32
            size_hint: 1,0.3

        BoxLayout:

            orientation: 'horizontal'

            Button:
                id: inputbutton
                size_hint: 0.25, 1
                text: 'add'
                on_press:root.add_item()

            TextInput:
                id: inputcontent
                multiline: False



    ListWidget:
        id: outputcontent
        viewclass: 'Label'
        orientation: 'vertical'


        RecycleBoxLayout:
            default_size: None,dp(56)
            default_size_hint: 0.4,None
            size_hint_y: None
            height:self.minimum_height
            orientation: 'vertical'

这是输出 This is the output

【问题讨论】:

    标签: python mobile kivy android-gridlayout


    【解决方案1】:

    您可以使用自定义viewclass 来满足您的需求。像这样的:

    class LabelAndButton(GridLayout):
        text = StringProperty()  # must have a text property (used in ListWidget.data)
    

    然后在kv 中你可以使用这个类作为viewclass 并定义它的外观:

        ListWidget:
            id: outputcontent
            viewclass: 'LabelAndButton'  # use the new class
            orientation: 'vertical'
    
    
            RecycleBoxLayout:
                default_size: None,dp(56)
                default_size_hint: 0.4,None
                size_hint_y: None
                height:self.minimum_height
                orientation: 'vertical'
                
    <LabelAndButton>:
        cols:2
        Label:
            text: root.text
        Button:
            text: root.text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多