Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy:可选择的回收查看内部屏幕管理器_Python_Kivy - Fatal编程技术网

Python Kivy:可选择的回收查看内部屏幕管理器

Python Kivy:可选择的回收查看内部屏幕管理器,python,kivy,Python,Kivy,我是Kivy的新手,我试图在screenmanager中添加一个可选的recycleview,但我想不出来。 我也找不到关于它的例子 有谁能给我一个例子来说明如何做到这一点吗?如果您共享您的代码,它将更容易帮助您。 但这是一个从kivy的内置示例构建的基本示例 我希望这会有帮助 from kivy.app import App from kivy.lang import Builder from kivy.properties import BooleanProperty from kivy.p

我是Kivy的新手,我试图在screenmanager中添加一个可选的recycleview,但我想不出来。 我也找不到关于它的例子


有谁能给我一个例子来说明如何做到这一点吗?

如果您共享您的代码,它将更容易帮助您。 但这是一个从kivy的内置示例构建的基本示例

我希望这会有帮助

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import BooleanProperty
from kivy.properties import NumericProperty
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.label import Label
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string('''
#:import random random.random

<CustomScreen>:
    hue: random()
    canvas:
        Color:
            hsv: self.hue, .5, .3
        Rectangle:
            size: self.size

    Label:
        font_size: 42
        text: root.name

    Button:
        text: 'Next screen'
        size_hint: None, None
        pos_hint: {'right': 1}
        size: 150, 50
        on_release: root.manager.current = root.manager.next()

<SelectableLabel>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size
<RV>:
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True
        touch_multiselect: True

<RVScreen>:
    BoxLayout:
        orientation: "vertical"
        RV:
        Button:
            text: 'Previous screen'
            size_hint: None, None
            size: 150, 50
            on_release: root.manager.current = root.manager.previous()

''')


class CustomScreen(Screen):
    hue = NumericProperty(0)


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(10)]


class RVScreen(Screen):
    pass


class ScreenManagerApp(App):

    def build(self):
        root = ScreenManager()
        root.add_widget(CustomScreen(name='CustomScreen'))
        root.add_widget(RVScreen(name='RVScreen'))
        return root


if __name__ == '__main__':
    ScreenManagerApp().run()
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.properties导入布尔属性
从kivy.properties导入NumericProperty
从kivy.uix.behaviors导入焦点行为
从kivy.uix.label导入标签
从kivy.uix.recycleboxlayout导入recycleboxlayout
从kivy.uix.recycleview导入recycleview
从kivy.uix.recycleview.layout导入LayoutSelectionBehavior
从kivy.uix.recycleview.views导入RecycleDataViewBehavior
从kivy.uix.screenmanager导入screenmanager,屏幕
Builder.load_字符串(“”)
#:导入random.random
:
色调:随机
画布:
颜色:
hsv:self.hue、.5、.3
矩形:
大小:self.size
标签:
字号:42
text:root.name
按钮:
文本:“下一屏幕”
大小提示:无,无
位置提示:{'right':1}
尺寸:150,50
发布时:root.manager.current=root.manager.next()
:
#绘制背景以指示选择
在以下情况之前:
颜色:
rgba:(.0,0.9,1,3)如果自选,则为其他(0,0,0,1)
矩形:
pos:self.pos
大小:self.size
:
viewclass:“SelectableLabel”
可选择的可循环利用布局:
默认大小:无,dp(56)
默认大小提示:1,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
多选:正确
触摸多选:真
:
盒子布局:
方向:“垂直”
房车:
按钮:
文本:“上一屏幕”
大小提示:无,无
尺寸:150,50
发布时:root.manager.current=root.manager.previous()
''')
类自定义屏幕(屏幕):
色调=数值属性(0)
类SelectableRecycleBoxLayout(焦点行为、布局选择行为、,
可回收性布局):
“将选择和焦点行为添加到视图中。”
类SelectableLabel(RecycleDataViewBehavior,Label):
''将选择支持添加到标签''
索引=无
selected=布尔属性(False)
可选=布尔属性(真)
def刷新\视图\属性(自身、rv、索引、数据):
''捕获并处理视图更改''
self.index=索引
返回超级(SelectableLabel,self)。刷新\u视图\u属性(
rv、索引、数据)
def on_触控向下(自身,触控):
''在触地时添加选择''
如果是超级(SelectableLabel,self)。打开时触摸下(触摸):
返回真值
如果自碰撞点(*touch.pos)和自选择:
返回self.parent。使用触摸键选择(self.index,touch)
def应用选项(选择了自身、rv、索引):
''响应视图中的项目选择''
self.selected=是否选中
如果选择了以下选项:
打印(“选择更改为{0}”。格式(rv.data[index]))
其他:
打印(“删除了对{0}的选择”。格式(rv.data[index]))
RV级(回收利用审查):
定义初始(自我,**kwargs):
超级(RV,自我)。\uuuuu初始值(**kwargs)
self.data=[{'text':str(x)}表示范围(10)内的x
类RVScreen(屏幕):
通过
类屏幕管理器RAPP(应用程序):
def生成(自):
root=ScreenManager()
root.add_小部件(CustomScreen(name='CustomScreen'))
root.add_小部件(RVScreen(name='RVScreen'))
返回根
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
ScreenManagerApp().run()

这真的很有帮助,我的朋友。感谢一个lot,我可以将
self.data=[{'text':str(x)}用于范围(10)]
中的x的部分代码放在一个函数中,以便在循环定义之外调用它吗?是的,您可以在python代码中创建RV小部件的实例,并将其添加到init中的RVScreen中。因此,您可以访问对象的数据属性并对其进行修改,或者在RVScreen的kv定义中向RV小部件添加一个id,并通过python代码中的id访问对象。