python kivy显示图片_python – 在Kivy中隐藏和显示一个小部件

我正在开发一个Kivy项目,当列表中没有元素时,我需要显示一个标签.否则,我需要显示一个列表视图.

这些是我描述的两个场景:

什么时候没有朋友表示:

当列表包含朋友时:

这是我的Kivy文件:

#: kivy 1.9.1

#: import ListAdapter kivy.adapters.listadapter.ListAdapter

#: import FriendItemButton gui.FriendItemButton

ChumMeRoot:

:

friend_list_view: friend_list_view

FriendList:

id: friend_list_view

:

text: self.full_name

height: "40dp"

size_hint_y: None

:

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)

height: '30dp'

size_hint_y: None

:

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`,或者有人能告诉我一个很好的方法来完成这项任务吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值