Kivy RecycleView SelectableButton Не переключается на страницу динамического макета - PullRequest
0 голосов
/ 12 мая 2019

Я работаю над проектом, в котором есть RecycleView, который содержит информацию о пациентах. Моя цель состоит в том, чтобы, когда я щелкаю по конкретному пациенту в строке recycleView, он приводил меня к определенному динамическому макету страницы, содержащему конкретные сведения о выбранном пациенте.в RecycleView без использования диспетчера экрана, переключение динамических страниц. как я могу это сделать?

Я создал метод change_dynamic_Layout () для переключения динамических страниц, и он работает нормально при использовании обычной кнопкиназывать этоЯ использовал оператор print в методе, чтобы показать, выполняется метод или нет.Но когда я использую SelectableButton, оператор print выполняется, а оператор для изменения макета страницы - нет.Ошибки не отображаются и динамическая страница не изменяется.Вот код, попробуйте запустить его и посмотреть, о чем идет речь?

Demo.py

from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, ListProperty, 
NumericProperty
from kivy.uix.behaviors import ButtonBehavior
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.factory import Factory
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior



lost = Builder.load_file('Demo.kv')

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


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' 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(SelectableButton, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, 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

    #selectablebutton to call the change_dynamic_Layout() method in patient class    

    def on_enter(self):
        layout=Patient()
        layout.change_dynamic_Layout()



class victor(BoxLayout):
    pass
class Patient(Screen):
    manage_prescription: ObjectProperty(None)

    #Method to change the dynamic pagelayout

    def change_dynamic_Layout(self): 
        layout = Factory.victor()
        self.manage_prescription.clear_widgets()
        self.manage_prescription.add_widget(layout)
        print ('pressed')


class DemoApp(App):
    title = 'Hospital Management System'
    def build(self):
        n= Patient()
        return n 

if __name__ == "__main__":
    DemoApp().run()

demo.kv

<Patient>:
    manage_prescription:manage_prescription

    BoxLayout:

        GridLayout :
            cols:1
            BoxLayout:
                id:manage_prescription
                orientation:'vertical'

                BoxLayout:
                    size_hint_y:None
                    height:40

                    Button:
                        text:"NO."
                        font_size: 25

                    Button:
                        text:"Date"
                        font_size: 25


                    Button:
                        text:"Patient"
                        font_size: 25


                    Button:
                        text:"Doctor"
                        font_size: 25
                        on_press: root.change_dynamic_Layout()


                BoxLayout:

                    RecycleView:
                        bar_width: 10
                        bar_color: 1, 0, 0, 1   # red
                        bar_inactive_color: 0, 0, 1, 1   # blue
                        #effect_cls: "ScrollEffect"
                        scroll_type: ['bars']
                        viewclass: 'SelectableButton'
                        data:[{'text': str(x)} for x in range(20)]
                        SelectableRecycleGridLayout:
                            cols:4
                            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





<victor>:
    Label:
        text:" Switched to specific page for patient's details"


<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (1, 1, 1, 1)
        Rectangle:
            pos: self.pos
            size: self.size
    on_press: root.on_enter()

Любое понимание или помощь высоко ценится, заранее спасибо.

1 Ответ

0 голосов
/ 13 мая 2019

Проблема в том, что ваш on_enter метод SelectableButton создает новый макет Patient (Screen) и вызывает метод change_dynamic_Layout этого нового Patient Screen.Этот недавно созданный Patient Screen не отображается в вашем приложении, поэтому он не влияет на то, что вы видите.Вы действительно хотите позвонить change_dynamic_Layout на отображаемом Patient Screen.Один из способов сделать это - получить к нему доступ через App.get_running_app().root.Так что ваш on_enter метод может быть изменен на:

def on_enter(self):
    #layout=Patient()
    layout = App.get_running_app().root
    layout.change_dynamic_Layout()

Вот весь файл Python:

from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, ListProperty, NumericProperty
from kivy.uix.behaviors import ButtonBehavior
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.factory import Factory
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior



lost = Builder.load_file('Demo.kv')

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


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' 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(SelectableButton, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, 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

    #selectablebutton to call the change_dynamic_Layout() method in patient class

    def on_enter(self):
        #layout=Patient()
        layout = App.get_running_app().root
        layout.change_dynamic_Layout()



class victor(BoxLayout):
    pass
class Patient(Screen):
    manage_prescription: ObjectProperty(None)

    #Method to change the dynamic pagelayout

    def change_dynamic_Layout(self):
        layout = Factory.victor()
        self.manage_prescription.clear_widgets()
        self.manage_prescription.add_widget(layout)
        print ('pressed')


class DemoApp(App):
    title = 'Hospital Management System'
    def build(self):
        n= Patient()
        return n

if __name__ == "__main__":
    DemoApp().run()

А вот Demo.kv:

<Patient>:
    manage_prescription:manage_prescription

    BoxLayout:

        GridLayout :
            cols:1
            BoxLayout:
                id:manage_prescription
                orientation:'vertical'

                BoxLayout:
                    size_hint_y:None
                    height:40

                    Button:
                        text:"NO."
                        font_size: 25

                    Button:
                        text:"Date"
                        font_size: 25


                    Button:
                        text:"Patient"
                        font_size: 25


                    Button:
                        text:"Doctor"
                        font_size: 25
                        on_press: root.change_dynamic_Layout()


                BoxLayout:

                    RecycleView:
                        bar_width: 10
                        bar_color: 1, 0, 0, 1   # red
                        bar_inactive_color: 0, 0, 1, 1   # blue
                        #effect_cls: "ScrollEffect"
                        scroll_type: ['bars']
                        viewclass: 'SelectableButton'
                        data:[{'text': str(x)} for x in range(20)]
                        SelectableRecycleGridLayout:
                            cols:4
                            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





<victor>:
    Label:
        text:" Switched to specific page for patient's details"


<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (1, 1, 1, 1)
        Rectangle:
            pos: self.pos
            size: self.size
    on_press: root.on_enter()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...