gpt4 book ai didi

python - 隐藏和显示小部件

转载 作者:太空狗 更新时间:2023-10-29 20:57:58 27 4
gpt4 key购买 nike

我正在做一个 Kivy 项目,我需要在列表中没有元素时显示一个标签。否则,我需要显示 ListView 。

这是我描述的两个场景:

当没有好友显示时: 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)


<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()orself .widget_name.hide = True`,或者有人可以告诉我完成此任务的好方法吗?

最佳答案

应该适用于每个小部件和用例:

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

关于python - 隐藏和显示小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com