Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - How to insert value of List Item into another Screen in KivyMD

In the app I'm coding on button click on the bottom right of the screen there appears a popup window. On "Done" click the popup window closes (close_dialog method), and a new List Item appears. The text of the List Item is obtained from MDTextField of the popup. On List Items click we enter another screen <GroupScreen> (goto_group method).

So I have two questions:

  1. As far as I understand if we make several List Items, they all lead to one instance of <Group Screen>. Am I right?
  2. I want each of created List Items to lead to its unique <GroupScreen> instance. For example, I want List text to be copied to MDLabel (instead of "Welcome" text). How can I do that?

Code .py:

from kivy.lang import Builder
from kivy.core.window import Window
from kivymd.app import MDApp
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.list import TwoLineAvatarListItem

Window.size = (288, 511)

sm = ScreenManager()

class GroupScreen(Screen):
    pass

class DialogContent(BoxLayout):
    pass

class MainScreen(Screen):
    dialog = None

    def show_dialog(self, *args):
        '''
        Create group creation popup
        '''
        if not self.dialog:
            self.dialog = MDDialog(
                title="Create new group",
                type="custom",
                content_cls=DialogContent(),
            )
        self.dialog.open()

    def close_dialog(self, *args):
        '''
        Close popup on Done click
        '''
        self.dialog.dismiss()
        self.new_window()

    def new_window(self, *args):
        '''
        Create new group button
        '''
        mylist = TwoLineAvatarListItem(text = self.dialog.content_cls.textfield.text,
            on_release = self.goto_group)
        self.mdlist.add_widget(mylist)

    def goto_group(self, *args):
        sm.current = 'group'

class grudget4App(MDApp):
    def build(self):
        sm.add_widget(MainScreen(name='main'))
        sm.add_widget(GroupScreen(name='group'))
        scroll = ScrollView()
        return sm

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

Code. kv:

ScreenManager:
    MainScreen:
    GroupScreen:

<DialogContent>:
    textfield: textfield
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "120dp"

    MDTextField:
        id: textfield
        hint_text: "Group name"

    MDFlatButton:
        id: btn1
        text: "Done"
        text_color: self.theme_cls.primary_color
        on_release: app.root.get_screen('main').close_dialog()

<MainScreen>:
    name: 'main'
    mdlist: mdlist
    FloatLayout:
        size_hint: 1, 0.89
        ScrollView:
            MDList:
                id: mdlist
    MDFloatingActionButton:
        pos_hint: {'right': 0.95, 'y': 0.05}
        icon: "icon.png"
        theme_text_color: "Custom"
        text_color: app.theme_cls.primary_color
        on_release:
            root.show_dialog()

<GroupScreen>:
    name: 'group'
    MDLabel:
        text: "Welcome" #app.root.ids["textfield"].text
        halign: 'center'
    MDRectangleFlatButton:
        text: 'Back'
        pos_hint: {'center_x': 0.5, 'center_y': 0.3}
        on_release:
            root.manager.current = 'main'
question from:https://stackoverflow.com/questions/65642334/how-to-insert-value-of-list-item-into-another-screen-in-kivymd

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the documentation:

name

Name of the screen which must be unique within a ScreenManager.

I don't see any code creating new GroupScreens, but wherever you put that code, you must include a unique name="some_group_name" for it.

I my experience, in order to allow Buttons to operate in an MDDialog, you must add auto_dismiss=False to its declaration:

def show_dialog(self, *args):
    '''
    Create group creation popup
    '''
    if not self.dialog:
        self.dialog = MDDialog(
            text="Create new group",
            type="custom",
            auto_dismiss=False,  # needed to allow Buttons to operate
            content_cls=DialogContent(),
        )
    self.dialog.open()

Then, the new_window() method can create the new GroupScreen and the Button:

def new_window(self, *args):
    '''
    Create new group button
    '''
    group_name = self.dialog.content_cls.textfield.text
    mylist = TwoLineAvatarListItem(text=group_name,
        on_release = partial(self.goto_group, group_name))  # use partial to pass group name
    self.mdlist.add_widget(mylist)
    sm.add_widget(GroupScreen(name=group_name))  # actually create the new GroupScreen and add it

And the goto_group() method becomes:

def goto_group(self, group_name, *args):
    sm.current = group_name  # switch current screen to the passed in group

The App declaration can be:

class grudget4App(MDApp):
    def build(self):
        sm.add_widget(MainScreen(name='main'))
        # sm.add_widget(GroupScreen(name='group'))
        # scroll = ScrollView()
        return sm

Note the above requirement for unique name for Screens. You should take care that the user doesn't create duplicate group names.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...