简体   繁体   English

隐藏和显示小部件

[英]Hiding and showing a widget

I am working on a Kivy project, and I need to display a label when there is no elements in a list.我正在处理 Kivy 项目,当列表中没有元素时,我需要显示 label。 Otherwise, I need to display a list view.否则,我需要显示一个列表视图。

These are the two scenarios I am describing:这是我描述的两种情况:

When no friends to show:当没有朋友可以显示时: 没朋友

When list contains friends:当列表包含朋友时: 与朋友一起列出

This is my Kivy file:这是我的 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()

and this is my Python code:这是我的 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()

So far, I have found this solution which basically says to avoid removing widgets from the root, and though I have tried it, I lost references and my app crashes.到目前为止,我已经找到了这个解决方案,它基本上说要避免从根目录中删除小部件,虽然我已经尝试过了,但我丢失了引用并且我的应用程序崩溃了。 I don't also want to put the element in a position out of the visible screen, so I wonder if somebody knows a way to hide and show widgets as 'self.widget_name.hide() or self.widget_name.hide = True`, or can somebody tell me a good way to achieve this task?我也不想将元素放在可见屏幕之外的 position 中,所以我想知道是否有人知道将小部件隐藏和显示为'self.widget_name.hide() or self.widget_name.hide = True`的方法,或者有人可以告诉我完成这项任务的好方法吗?

As @yogabonito suggested what I did was to set the widget's height in this case to 0dp (and obviously the attribute size_hint_y . 正如@yogabonito建议我做的是将小部件的高度设置为0dp (显然属性size_hint_y

So this is the changes I made in my kivy file: 所以这是我在我的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)

As it can be seen, I have added a Label widget which has a reference in its root called no_friends_label . 可以看出,我添加了一个Label小部件,其根目录中有一个名为no_friends_label的引用。

This are the changes I made in my Python file: 这是我在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

First, I added the kivy property friend_list which has the corresponding reference in the kivy file. 首先,我添加了kivy属性friend_list ,它在kivy文件中有相应的引用。 Then I am 'hiding' or showing the label or the list view depending on the user's having friends. 然后我“隐藏”或显示标签或列表视图,具体取决于用户的朋友。

Should work for every widget & use case: 应该适用于每个小部件和用例:

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

In Matteljay's comment,在 Matteljay 的评论中,

def hide_widget(wid, dohide=True):

should be应该

def hide_widget(self, wid, dohide=True):

Interestingly, without self , hide_widget hides the entire app.有趣的是,没有self , hide_widget 隐藏整个应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM