如何使用kivy python中的用户输入在网格布局中添加标签和按钮
我是kivy的新手…我想制作一个待办事项列表应用程序…并想添加一个来自用户的任务名称和一个按钮,该按钮可以在按下时得到记号 我是如何在屏幕上得到一个标签的,但我无法得到它 这是main.py如何使用kivy python中的用户输入在网格布局中添加标签和按钮,python,mobile,kivy,android-gridlayout,Python,Mobile,Kivy,Android Gridlayout,我是kivy的新手…我想制作一个待办事项列表应用程序…并想添加一个来自用户的任务名称和一个按钮,该按钮可以在按下时得到记号 我是如何在屏幕上得到一个标签的,但我无法得到它 这是main.py class ListWidget(RecycleView): def update(self): self.data = [{'text': str(item)}for item in self.item] def __init__(self, **kwargs):
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'
inputbutton:inputbutton
inputcontent:inputcontent
outputcontent:outputcontent
方向:“垂直”
盒子布局:
方向:“垂直”
尺寸提示:1,0.25
标签:
文本:“待办事项”
字体大小:32
尺寸提示:1,0.3
盒子布局:
方向:“水平”
按钮:
id:inputbutton
尺寸提示:0.25,1
文本:“添加”
按:root.add\u item()
文本输入:
id:输入内容
多行:False
ListWidget:
id:outputcontent
viewclass:“标签”
方向:“垂直”
循环利用布局:
默认大小:无,dp(56)
默认大小提示:0.4,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
这是输出
您可以使用自定义的
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
ListWidget:
id:outputcontent
viewclass:“LabelAndButton”#使用新类
方向:“垂直”
循环利用布局:
默认大小:无,dp(56)
默认大小提示:0.4,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
:
科尔斯:2
标签:
text:root.text
按钮:
text:root.text