Reputation: 43
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()
Upvotes: 1
Views: 395
Reputation: 270
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)
Upvotes: 1