Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

kivy - How can I make an MDDataTable in another screen?

I can't find a way to put an MDDataTable into a second screen after I press a button. I want to be able to display my table in the second screen when I press a button from the first screen. I don't know what to write to have the view_account function to run in my the account screen. There seems to be no way that the table can get to the second screen. The error I get is this

self.table.open()
 AttributeError: 'DemoApp' object has no attribute 'table'
Window.size = (300, 500)

screen_helper = """
ScreenManager:
    MenuScreen:
    AccountScreen:
<MenuScreen>:
    name: 'menu'
    MDLabel:
        text: "Login Page Test"
        pos_hint: {'center_x':0.8, 'center_y':0.9}
    MDRectangleFlatButton:
        text: 'Proceed To Next Page'
        pos_hint: {'center_x':0.5, 'center_y':0.4}
        font_size : 20
        on_press:
            root.manager.current = 'account'   
            app.view_account()
<AccountScreen>:
    name: 'account'
    MDLabel:
        text: 'Accounts Page Test'
        halign: 'center'
    
"""


class MenuScreen(Screen):
    pass

class AccountScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(AccountScreen(name='account'))


class DemoApp(MDApp):

    def build(self):

        screen = Builder.load_string(screen_helper)
        return screen

    def view_account(self):
        rows = []
        table = MDDataTable(pos_hint={'center_x': .5, 'center_y': .6},
                            size_hint=(.9, 0.7),
                            check=True,
                            rows_num=30,
                            column_data=[
                                ("Type.", dp(38)),
                                ("Balance", dp(20)),
                                ("Date", dp(20)),
                                ("id", dp(20))
                            ],
                            row_data=rows
                            )
        table.bind(on_check_press=self.check_press)
        table.bind(on_row_press=self.row_press)
        close_button1 = MDFlatButton(text='Close', on_release=self.close_view)
        self.table.open()

    def row_press(self, instance_table, instance_row):
        print(instance_table, instance_row)


DemoApp().run()
question from:https://stackoverflow.com/questions/65651903/how-can-i-make-an-mddatatable-in-another-screen

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

1 Reply

0 votes
by (71.8m points)

Instead of doing it in the app, create a function in the screen which has the button, then access the other screen and then add to it.

Kivy modification (Add the method and create a holder/container for the table)

     MDRectangleFlatButton:
        text: 'Proceed To Next Page'
        pos_hint: {'center_x':0.5, 'center_y':0.4}
        font_size : 20
        on_press:
<AccountScreen>:
    name: 'account'
    FloatLayout:
        MDLabel:
            text: 'Accounts Page Test'
            halign: 'center'
                root.manager.current = 'account'   
                root.add_table()
        BoxLayout:
            id: table_holder

Python modification (create the method in the MenuScreen)

class MenuScreen(Screen):
    
    def add_table(self):
        account_screen_ref = self.manager.get_screen('account')
        account_screen_ref.ids['table_holder'].add_widget(account_screen_ref.table)


class AccountScreen(Screen):
    def on_kv_post(self):
        rows = []
        self.table = MDDataTable(pos_hint={'center_x': .5, 'center_y': .6},
                            size_hint=(.9, 0.7),
                            check=True,
                            rows_num=30,
                            column_data=[
                                ("Type.", dp(38)),
                                ("Balance", dp(20)),
                                ("Date", dp(20)),
                                ("id", dp(20))
                            ],
                            row_data=rows
                            )
        self.table.bind(on_check_press=self.check_press)
        self.table.bind(on_row_press=self.row_press)
        close_button1 = MDFlatButton(text='Close', on_release=self.close_view)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...