我正在開發一個Kivy項目,當列表中沒有元素時,我需要顯示一個標籤。否則,我需要顯示一個列表視圖。在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)
<[email protected]>
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()or
self.widget_name.hide = True` ,或者有人可以告訴我一個完成這個任務的好方法嗎?
也許隱藏一個小部件透明是一個選項。如果是這樣,請查看[opacity kwarg](https://kivy.org/docs/api-kivy.uix.widget.html#kivy.uix.widget.Widget.opacity)。 另一個簡單的方法是將小部件的大小設置爲0. – yogabonito
@yogabonito,將大小設置爲0似乎不是一個壞主意。 – lmiguelvargasf
很高興我的評論對你有用:) – yogabonito