Python Kivy - не может передать значение TextInput экрана (ScreenManager) в просмотр списка другого экрана - PullRequest
0 голосов
/ 17 мая 2018

Я новичок в kivy и мне нужна помощь с ListView и ScreenManager.Мне удалось использовать ListView на одном экране, но я пытаюсь использовать его с двумя экранами через ScreenManager: у меня есть ListView в MainScreen и сделал кнопку, чтобы перейти к ProfileScreen, где я буду вводить значения в TextInput «abc»и "def" и хотите, чтобы они были представлены в ListView.Когда я запускаю файл .py с этим кодом, он работает, но когда я ввожу значения в ProfileScreen и нажимаю «Окей», он вылетает и говорит, что «ProfileScreen» не имеет атрибута «student_list».Если я изменяю параметр ProfileScreen (Screen) на ProfileScreen (MainScreen), он работает, но содержимое страницы ProfileScreen наследуется от MainScreen, чего я не хочу.

Как решить эту проблему?Буду признателен за любую помощь, заранее спасибо.

Это мой код:

studentdb.py, studentdb.kv

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton
from kivy.uix.screenmanager import ScreenManager, Screen

class StudentListButton(ListItemButton):
    pass

class Manager(ScreenManager):
    main_screen = ObjectProperty()
    profile_screen = ObjectProperty()

class MainScreen(Screen):
    first_name_text_input= ObjectProperty()
    last_name_text_input= ObjectProperty()
    student_list = ObjectProperty()

    def submit_student(self):
        # Get the student name from the TextInputs
        student_name = self.first_name_text_input.text + " " + self.last_name_text_input.text

        # Add the student to the ListView
        self.student_list.adapter.data.extend([student_name])

        # Reset the ListView
        self.student_list._trigger_reset_populate()

    def delete_student(self, *args):
        if self.student_list.adapter.selection:

            # Get the text from the item selected
            selection = self.student_list.adapter.selection[0].text

            # Remove the matching item
            self.student_list.adapter.data.remove(selection)

            # Reset the ListView
            self.student_list._trigger_reset_populate()

    def replace_student(self, *args):
        # If a list item is selected
        if self.student_list.adapter.selection:

            # Get the text from the item selected
            selection = self.student_list.adapter.selection[0].text

            # Remove the matching item
            self.student_list.adapter.data.remove(selection)

            # Get the student name from the TextInputs
            student_name = self.first_name_text_input.text + " " + self.last_name_text_input.text

            # Add the updated data to the list
            self.student_list.adapter.data.extend([student_name])

            # Reset the ListView
            self.student_list._trigger_reset_populate()

class ProfileScreen(Screen):
    abc_text_input=ObjectProperty()
    def_text_input=ObjectProperty()

    def okay(self):
        abc_name = self.abc_text_input.text + " " + self.def_text_input.text

        # Add the student to the ListView
        self.student_list.adapter.data.extend([abc_name])

        # Reset the ListView
        self.student_list._trigger_reset_populate()

class StudentDBApp(App):
    def build(self):
        return Manager()

if __name__=="__main__":
    StudentDBApp().run()
------------------------------------------------------------------
#: import main studentdb
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton

<MainScreen>:
    first_name_text_input: first_name
    last_name_text_input: last_name
    student_list: student_list
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            Label:
                text:"First Name:"
            TextInput:
                id: first_name
            Label:
                text:"Last Name:"
            TextInput:
                id: last_name
        BoxLayout:
            Button:
                text:"Submit"
                on_release: root.submit_student()
            Button:
                text:"Delete"
                on_release: root.delete_student()
            Button:
                text:"Replace"
                on_release: root.replace_student()
            Button:
                text: "New"
                on_release: root.manager.current="profile_screen"
        ListView:
            id: student_list
            adapter:
                ListAdapter(data=['Doug Smith'], cls=main.StudentListButton)

<ProfileScreen>:
    abc_text_input: abc
    def_text_input: defe
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            Button:
                text:"Back"
                on_release: root.manager.current="main_screen"
        BoxLayout:
            Label:
                text:"abc"
            TextInput:
                id: abc
            Label:
                text: "def"
            TextInput:
                id: defe
        BoxLayout:
            Button:
                text: "Okay"
                on_release: root.okay()

<Manager>:
    id: screen_manager
    main_screen: main_screen
    profile_screen: profile_screen

    MainScreen:
        id: main_screen
        name:"main_screen"
        manager: screen_manager

    ProfileScreen:
        id: profile_screen
        name: "profile_screen"
        manager: screen_manager

Ответы [ 2 ]

0 голосов
/ 17 мая 2018

Поскольку у вас уже есть экраны, подключенные к ObjectProperty в ScreenManager, решение -

self.manager.main_screen...

Примечание

Возможно, вы захотите заменить ListView на RecycleView, поскольку ListView устарел.

Список

Устаревший с версии 1.10.0: ListView устарел, используйте Вместо RecycleView .

Отрывки

def okay(self):
    abc_name = self.abc_text_input.text + " " + self.def_text_input.text

    # Add the student to the ListView
    self.manager.main_screen.student_list.adapter.data.extend([abc_name])

    # Reset the ListView
    self.manager.main_screen.student_list._trigger_reset_populate()

выход

Img01

0 голосов
/ 17 мая 2018

Ошибка в вашем случае заключается в том, что student_list является атрибутом MainScreen, а не ProfileScreen, поэтому вы не можете получить к нему доступ самостоятельно, я рекомендую прочитать следующее: Какова цель себя?

В вашем случае решение состоит в том, чтобы получить доступ к нужному экрану через ScreenManager:

def okay(self):
    abc_name = self.abc_text_input.text + " " + self.def_text_input.text
    main_screen = self.manager.get_screen('main_screen')

    # Add the student to the ListView
    main_screen.student_list.adapter.data.extend([abc_name])

    # Reset the ListView
    main_screen.student_list._trigger_reset_populate()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...