refresh_view_attrs a zmienna globalna. Kivy, ScreenManager.

Odpowiedz Nowy wątek
2019-03-04 15:26
0

Jak w poniższym kodzie doprowadzić do wywołania metody refresh_view_attrs? To działa gdy zrezygnuję ze zmiennej globalnej i ScreenManagera i w build zwrócę RequestRecycleView ale chcę aby apka była zbudowana w oparciu właśnie o ScreenManager. Podsumowując - nie wiem jak przekazać tablicę z klasy RequestRecycleView do RequestRow tak aby wyzwolić odświeżenie.

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from random import sample
from string import ascii_lowercase
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior

kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager:
    transition: FadeTransition()
    RequestsScreen:

<[email protected]>

    size_hint_y: None
    orientation: 'vertical'
    row_index: id_row_index.text
    row_index:'123'
    pos: self.pos
    size: self.size
    Label:
        id: id_row_index 
        text: root.row_index

<[email protected]>:               
    #id: rv
    viewclass: 'RequestRow'
    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

<RequestsScreen>
    name: 'RequestsScreen'
    BoxLayout:         
        orientation: 'vertical'
        Label: 
            text: 'recycle'
        RequestRecycleView:

"""
reqdata = []

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

class RequestRow(RecycleDataViewBehavior):
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    data = reqdata
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Clock.schedule_interval(refresh_view_attrs, 1)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        self.row_index = str(index)
        self.row_content = data['text']
        return super(RequestRow, self).refresh_view_attrs(
            rv, index, data)

class ScreenManagement(ScreenManager):
    pass

class RequestRecycleView(RecycleView):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        global reqdata
        self.data = []
        for r in range(30):
            row = {'text': ''.join(sample(ascii_lowercase, 6))}
            self.data.append(row)
            reqdata = self.data
        print(self.data)
        print(type(reqdata))

class RequestsScreen(Screen):
    pass
Builder.load_string(kv)

sm = ScreenManagement()
sm.add_widget(RequestsScreen(name = 'requests'))

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

Pozostało 580 znaków

2019-03-05 07:21
0

Ok udało mi się i przy okazji zrozumiałem (chyba) przepływ danych. Dodałem też drugą kolumnę w recycle view.

from kivy.config import Config
Config.set('graphics', 'multisamples', '0')
from random import sample
from string import ascii_lowercase
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.clock import Clock

kv = """
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManager:
    transition: FadeTransition()
    RequestsScreen:

<RequestRow>    
    size_hint_y: None
    orientation: 'horizontal'
    pos: self.pos
    size: self.size
    Label:
        id: id_row_index         
    Label:
        id: id_row_task

<RequestRecycleView>:               
    viewclass: 'RequestRow'
    data: root.tab
    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

<RequestsScreen>
    name: 'RequestsScreen'
    BoxLayout:         
        orientation: 'vertical'
        Label: 
            text: 'recycle'
        RequestRecycleView:

"""

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

class RequestRow(RecycleDataViewBehavior, BoxLayout):
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    def refresh_view_attrs(self, rv, index, data):
        self.index = index
        self.ids['id_row_index'].text = str(index)
        self.ids['id_row_task'].text = data['text']
        return super(RequestRow, self).refresh_view_attrs(
            rv, index, data)

class ScreenManagement(ScreenManager):
    pass

class RequestRecycleView(RecycleView):
    def __init__(self, **kwargs):
        print('2')
        super().__init__(**kwargs)
        self.tab = []
        for r in range(30):
            row = {'text': ''.join(sample(ascii_lowercase, 6))}
            self.tab.append(row)

class RequestsScreen(Screen):
    pass

sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0