我正在开发一个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()
到目前为止,我发现this solution基本上是为了避免从根目录中删除小部件,虽然我已经尝试过,但我丢失了引用并且我的应用程序崩溃了。我也不想将元素放在可见屏幕之外的位置,所以我想知道是否有人知道隐藏和显示小部件的方法为self.widget_name.hide(){{1} } self.widget_name.hide = True`,或者有人能告诉我一个很好的方法来完成这项任务吗?
答案 0 :(得分:5)
正如@yogabonito建议我做的是将小部件的高度设置为0dp
(显然属性size_hint_y
。
所以这是我在我的kivy文件中所做的更改:
<FriendList>:
orientation: 'vertical'
friend_list: friend_list_view
no_friends_label: no_friends_label
Button:
height: '45dp'
size_hint_y: None
text: 'Add Friend'
on_press: app.root.show_add_friend_form()
Label:
id: no_friends_label
ListView:
id: friend_list_view
adapter:
ListAdapter(
data=[],
cls=FriendItemButton,
args_converter=root.args_converter)
可以看出,我添加了一个Label小部件,其根目录中有一个名为no_friends_label
的引用。
这是我在Python文件中所做的更改:
class FriendList(BoxLayout):
friend_list = ObjectProperty()
no_friends_label = ObjectProperty()
def args_converter(self, index, data_item):
return {'full_name': (data_item[0])}
class ChumMeRoot(BoxLayout):
add_friend_form = ObjectProperty()
friend_list_view = ObjectProperty()
# __init__ method
def update_friend_list_view(self):
friends = get_friends()
no_friends_label = self.friend_list_view.no_friends_label
friend_list = self.friend_list_view.friend_list
if friends:
friend_list.size_hint_y = 1
friend_list.adapter.data.clear()
friend_list.adapter.data.extend(friends)
friend_list._trigger_reset_populate()
no_friends_label.size_hint_y = None
no_friends_label.height = '0dp'
no_friends_label.text = ''
else:
no_friends_label = self.friend_list_view.no_friends_label
no_friends_label.size_hint_y = 1
no_friends_label.text = 'There are no friends to show.'
friend_list.size_hint_y = None
friend_list.height = '0dp'
# remaining methods
首先,我添加了kivy属性friend_list
,它在kivy文件中有相应的引用。然后我“隐藏”或显示标签或列表视图,具体取决于用户的朋友。
答案 1 :(得分:1)
应该适用于每个小部件和用例:
def hide_widget(wid, dohide=True):
if hasattr(wid, 'saved_attrs'):
if not dohide:
wid.height, wid.size_hint_y, wid.opacity, wid.disabled = wid.saved_attrs
del wid.saved_attrs
elif dohide:
wid.saved_attrs = wid.height, wid.size_hint_y, wid.opacity, wid.disabled
wid.height, wid.size_hint_y, wid.opacity, wid.disabled = 0, None, 0, True