我正在开发一个Kivy项目,当列表为空时,我需要显示一个标签,否则,我需要显示一个列表视图。
这是我描述的两种情况:
这是我的Kivy文件:
#: kivy 1.9.1
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import FriendItemButton gui.FriendItemButton
ChumMeRoot:
<ChumMeRoot>:
friend_list_view: friend_list_view
FriendList:
id: friend_list_view
<FriendItemButton>:
text: self.full_name
height: "40dp"
size_hint_y: None
<FriendList>:
orientation: 'vertical'
friend_list: friend_list_view
Button:
height: '45dp'
size_hint_y: None
text: 'Add Friend'
on_press: app.root.show_add_friend_form()
ListView:
id: friend_list_view
adapter:
ListAdapter(
data=[],
cls=FriendItemButton,
args_converter=root.args_converter)
<AddFriendFormInput@BoxLayout>
height: '30dp'
size_hint_y: None
<AddFriendForm>:
orientation: 'vertical'
first_name_input: first_name
last_name_input: last_name
AddFriendFormInput:
Label:
text: 'First Name'
TextInput:
id: first_name
AddFriendFormInput:
Label:
text: 'Middle Name'
TextInput:
AddFriendFormInput:
Label:
text: 'Last Name'
TextInput:
id: last_name
AddFriendFormInput:
Label:
text: 'Birthdate'
TextInput:
AddFriendFormInput:
Label:
text: 'Email'
TextInput:
AddFriendFormInput:
Label:
text: 'Cell Phone'
TextInput:
BoxLayout:
BoxLayout:
height: '40dp'
size_hint_y: None
Button:
text: 'Cancel'
on_press: app.root.show_friend_list()
Button:
text: 'Add friend'
on_press: app.root.add_friend()
这是我的 Python 代码:
import os
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from friend import Friend
from friend_manager import FriendManager
def get_friend_manager():
db_path = '{}/{}'.format(
os.path.dirname(os.path.abspath(__file__)),
'chumme.db'
)
return FriendManager(db_path)
def get_friends():
return [(friend.full_name,)
for friend in get_friend_manager().get_friends()]
class ChumMeRoot(BoxLayout):
add_friend_form = ObjectProperty()
friend_list_view = ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.update_friend_list_view()
def update_friend_list_view(self):
friend_list = self.friend_list_view.friend_list
friend_list.adapter.data.clear()
friend_list.adapter.data.extend(get_friends())
friend_list._trigger_reset_populate()
def show_add_friend_form(self):
self.clear_widgets()
self.add_friend_form = AddFriendForm()
self.add_widget(self.add_friend_form)
def show_friend_list(self):
self.clear_widgets()
self.update_friend_list_view()
self.add_widget(self.friend_list_view)
def add_friend(self):
friend = Friend(first_name=self.add_friend_form.first_name_input.text,
last_name=self.add_friend_form.last_name_input.text)
get_friend_manager().add_friend(friend)
self.show_friend_list()
class AddFriendForm(BoxLayout):
first_name_input = ObjectProperty()
last_name_input = ObjectProperty()
class FriendList(BoxLayout):
friend_list = ObjectProperty()
def args_converter(self, index, data_item):
return {'full_name': (data_item[0])}
class FriendItemButton(ListItemButton):
full_name = StringProperty()
class ChumMeApp(App):
pass
def main():
ChumMeApp().run()
if __name__ == '__main__':
main()
到目前为止,我已经找到了这个解决方案。基本上它建议避免从根部删除小部件,虽然我尝试过,但我失去了引用并且我的应用程序崩溃了。我也不想把元素放在可见屏幕之外的位置,所以我想知道是否有人知道一种将窗口小部件隐藏和显示的方法,例如' self.widget_name.hide() 或
self.widget_name.hide = True',或者有人可以告诉我一个好的实现此任务的方法吗?