2017-02-01 70 views
2

我正在開發一個Kivy項目,當列表中沒有元素時,我需要顯示一個標籤。否則,我需要顯示一個列表視圖。在Kivy中隱藏和顯示小部件

這是兩個場景我描述:

如果沒有朋友顯示: No friends

當列表中包含的朋友: List with friends

這是我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` ,或者有人可以告訴我一個完成這個任務的好方法嗎?

+0

也許隱藏一個小部件透明是一個選項。如果是這樣,請查看[opacity kwarg](https://kivy.org/docs/api-kivy.uix.widget.html#kivy.uix.widget.Widget.opacity)。 另一個簡單的方法是將小部件的大小設置爲0. – yogabonito

+0

@yogabonito,將大小設置爲0似乎不是一個壞主意。 – lmiguelvargasf

+0

很高興我的評論對你有用:) – yogabonito

回答

1

由於@yogabonito建議我所做的就是在這種情況下,設置widget的高度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) 

因爲它可以看到,我已經添加了一種稱爲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文件中有相應的引用。然後,我根據用戶的朋友「隱藏」或顯示標籤或列表視圖。