[英]how to add a Label and button in grid layout with user input in kivy python
我是 kivy 的新手……我想制作一個待辦事項列表應用程序……並想從用戶那里添加一個任務名稱和一個按鈕,該按鈕可以在按下時打勾……
一些我如何在屏幕上獲得 label 但我無法獲得它。
這個 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'
這是 output這是 output
您可以使用自定義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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.