Try writing a minimal runnable app. You can use the on_tab_switch() method to make your tab specific sql queries.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1f1de5eb-09c4-4056-b039-d94582ec6f1do%40googlegroups.com.
Try writing a minimal runnable app. You can use the on_tab_switch() method to make your tab specific sql queries.
From: kivy
Sent: Friday, January 22, 2021 12:29 PM
To: Kivy users support
Subject: [kivy-users] KivyMD Using Tabs
Hello,
I want to use tabs (here is link) feature on my app. But tabs should be control from one class. I will run sql query every tab.. How can I do that? Can you give me example please?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
When the user switches tabs, the on_tab_switch() method will be called. The parameters will tell you the current tab. You could use that paramerter to call a different method or execute a different sql query based on the current tab.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c07df8f-36b4-419f-af16-d8f49fb4f10ao%40googlegroups.com.
Here is an example:
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
KV = '''
BoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
on_tab_switch: app.on_tab_switch(*args)
<Tab>:
MDLabel:
id: label
text: "Tab 0"
halign: "center"
'''
class Tab(FloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
for fruit in ['Apple', 'Orange', 'Banana', 'Avocado']:
t = Tab(text=fruit)
t.ids.label.text = f'This is the label content for the {fruit} tab'
self.root.ids.tabs.add_widget(t)
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
'''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>;
:param instance_tab: <__main__.Tab object>;
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>;
:param tab_text: text or name icon of tab;
'''
print(f'Now on tab: {tab_text}')
if tab_text == 'Apple':
# make a sql query for the Apple Page
print('One a day keeps the Dr Away... ')
elif tab_text == 'Orange':
# Orange specific action
print('Make some juice')
elif tab_text == 'Banana':
print('Peel the Banana')
elif tab_text == 'Avocado':
print('Time for guacamole!')
Example().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/600b33a7.1c69fb81.9b97.1918SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c07df8f-36b4-419f-af16-d8f49fb4f10ao%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/600b33a7.1c69fb81.9b97.1918SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
<FBScreen>: BoxLayout: orientation: 'vertical'
MDTabs: id: tabs on_tab_switch: app.on_tab_switch(*args)
BoxLayout: # Headings for size_hint_y: None height: 28 Label: text: 'Pos' color: 1, 0, 0, 1 Label: text: 'Name' color: 1, 0, 0, 1 Label: text: 'W' color: 1, 0, 0, 1 Label: text: 'L' color: 1, 0, 0, 1 Label: text: 'D' color: 1, 0, 0, 1 Label: text: 'Point' color: 1, 0, 0, 1 ScrollView: do_scroll_y: True bar_width: dp(10) scroll_type: ['bars','content'] BoxLayout: orientation: 'vertical' id:scroll_box size_hint_y: None height: self.minimum_height
class FBScreen(Screen):
def on_start(self): for fruit in ['tEST', 'A', 'B', 'Avocado']:
t = Tab(text=fruit) t.ids.label.text = f'This is the label content for the {fruit} tab' self.root.ids.tabs.add_widget(t)
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text): '''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>; :param instance_tab: <__main__.Tab object>; :param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>; :param tab_text: text or name icon of tab; '''
print(f'Now on tab: {tab_text}') if tab_text == 'Apple': # make a sql query for the Apple Page print('One a day keeps the Dr Away... ')
for line in table:
pos, name, win, lose, draw, point = line w = FBData(fbPos=str(pos), fbName=name, fbW=str(win), fbL=str(lose), fbD=str(draw), fbP=str(point)) self.ids.scroll_box.add_widget(w)
elif tab_text == 'Orange': # Orange specific action print('Make some juice') elif tab_text == 'Banana': print('Peel the Banana') elif tab_text == 'Avocado': print('Time for guacamole!')
Here is an example:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c07df8f-36b4-419f-af16-d8f49fb4f10ao%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/600b33a7.1c69fb81.9b97.1918SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
The method on_start() is a method of App, it runs after build. In a widget you can us on_kv_post(), it runs after the kv code is processed so the ids have been initialized.
https://kivy.org/doc/stable/api-kivy.uix.widget.html#kivy.uix.widget.Widget
class FBScreen(Screen):
def on_kv_post(self, base_widget):
for fruit in ['tEST', 'A', 'B', 'Avocado']:
t = Tab(text=fruit)
t.ids.label.text = f'This is the label content for the {fruit} tab'
self.root.ids.tabs.add_widget(t)
In this context, self is the screen. You would add the widget as
self.ids.tabs.add_widget(t)
Each kv rule has it’s own ids dict. The class defines a screen, self is FBScreen. The id tab is in ids dict of FBScreen, in this context
self.ids is that dict. Try printing out self, and self.ids and self.ids.tab to see how this works.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/313b9be8-4419-4ef1-a2cf-d94bb50c7cd5o%40googlegroups.com.
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text): '''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>; :param instance_tab: <__main__.Tab object>; :param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>; :param tab_text: text or name icon of tab; '''
print(f'Now on tab: {tab_text}') if tab_text == 'Apple': # make a sql query for the Apple Page print('One a day keeps the Dr Away... ')
def on_kv_post(self, base_widget): some db queries...
for fruit in ['tEST', 'A', 'B', 'Avocado']: t = Tab(text=fruit) t.ids.label.text = f'This is the label content for the {fruit} tab'
self.ids.tabs.add_widget(t)
for line in table:
bla bla bla...
self.ids.scroll_box.add_widget(w) elif tab_text == 'Orange': # Orange specific action print('Make some juice') elif tab_text == 'Banana': print('Peel the Banana') elif tab_text == 'Avocado': print('Time for guacamole!')
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/313b9be8-4419-4ef1-a2cf-d94bb50c7cd5o%40googlegroups.com.
There is not enough here to tell where the issue is. Share a runnable program.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/716139c8-f0e4-4017-955d-a88e1911ea83o%40googlegroups.com.
from kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenimport sqlite3from kivymd.app import MDAppfrom kivy.properties import ListProperty, StringPropertyfrom kivy.uix.boxlayout import BoxLayoutfrom kivy.uix.floatlayout import FloatLayoutfrom kivymd.uix.tab import MDTabsBase
kv = '''
BoxLayout: orientation: 'vertical' MDToolbar: id: toolbar elevation: 10 title: "Sports App" left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationLayout: ScreenManager: id: screen_manager HomeScreen: id: home name: 'home' FBScreen: id: fb name: 'fb' BBScreen: id: bb name: 'bb' MDNavigationDrawer: id: nav_drawer ScrollView: MDList: OneLineListItem: text: "Home" on_press: nav_drawer.set_state("close") screen_manager.current = "home" icon: 'language-python' OneLineListItem: text: "Football" on_press: nav_drawer.set_state("close") screen_manager.current = "fb"
<HomeScreen>: MDLabel: text: 'Home screen' halign: 'center'
<FBData>: canvas: Color: rgb: .3, .3, .3 Line: width: 2 rectangle: (*self.pos, *self.size) size_hint_y: None height: 48 Label: text: root.fbPos
color: 1, 0, 0, 1 Label:
text: root.fbName
color: 1, 0, 0, 1 Label:
text: root.fbW
color: 1, 0, 0, 1 Label:
text: root.fbL
color: 1, 0, 0, 1 Label:
text: root.fbD
color: 1, 0, 0, 1 Label:
text: root.fbP
color: 1, 0, 0, 1
<Tab>: MDLabel: id: label text: "Tab 0" halign: "center"
'''
class HomeScreen(Screen): pass
class FBData(BoxLayout): fbName = StringProperty() fbPos = StringProperty() fbW = StringProperty() fbL = StringProperty() fbD = StringProperty() fbP = StringProperty()
class Tab(FloatLayout, MDTabsBase): '''Class implementing content for a tab.'''
class FBScreen(Screen):
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text): '''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>; :param instance_tab: <__main__.Tab object>; :param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>; :param tab_text: text or name icon of tab; '''
print(f'Now on tab: {tab_text}') if tab_text == 'Apple': # make a sql query for the Apple Page print('One a day keeps the Dr Away... ') def on_kv_post(self, base_widget):
con = sqlite3.connect('test.db') cur = con.cursor() cur.execute("SELECT * FROM Soccer ORDER BY pos ASC") table = cur.fetchall()
for fruit in ['tEST', 'A', 'B', 'Avocado']: t = Tab(text=fruit) t.ids.label.text = f'This is the label content for the {fruit} tab' self.ids.tabs.add_widget(t)
for line in table:
pos, name, win, lose, draw, point = line w = FBData(fbPos=str(pos), fbName=name, fbW=str(win), fbL=str(lose), fbD=str(draw), fbP=str(point)) self.ids.scroll_box.add_widget(w) elif tab_text == 'Orange': # Orange specific action print('Make some juice') elif tab_text == 'Banana': print('Peel the Banana') elif tab_text == 'Avocado': print('Time for guacamole!')
class ScrManage(ScreenManager): pass
class Sports(MDApp): def build(self): return Builder.load_string(kv)
Sports().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/716139c8-f0e4-4017-955d-a88e1911ea83o%40googlegroups.com.
Yoy are using BBScreen, but you have not defined it. You can define it or comment it out, as I have done below.
'''
# BBScreen:
# id: bb
# name: 'bb'
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1b4e2f1c-413c-4485-9918-161b8752d895o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/1b4e2f1c-413c-4485-9918-161b8752d895o%40googlegroups.com.
The kv code as changed as highlighted below. You had the on_kv_post method indented under another method.
'''
on_tab_switch: root.on_tab_switch(*args)
(Screen):
def on_kv_post(self, base_widget):
# con = sqlite3.connect('test.db')
# cur = con.cursor()
# cur.execute("SELECT * FROM Soccer ORDER BY pos ASC")
# table = cur.fetchall()
for fruit in ['tEST', 'A', 'B', 'Avocado']:
t = Tab(text=fruit)
t.ids.label.text = f'This is the label content for the {fruit} tab'
self.ids.tabs.add_widget(t)
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
'''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>;
:param instance_tab: <__main__.Tab object>;
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>;
:param tab_text: text or name icon of tab;
'''
print(f'Now on tab: {tab_text}')
# if tab_text == 'Apple':
# # make a sql query for the Apple Page
# print('One a day keeps the Dr Away... ')
#
# for line in table:
# pos, name, win, lose, draw, point = line
# w = FBData(fbPos=str(pos), fbName=name, fbW=str(win), fbL=str(lose), fbD=str(draw), fbP=str(point))
# self.ids.scroll_box.add_widget(w)
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3e1d88bc-9fa7-4793-bd82-bf1c8fd18832o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3e1d88bc-9fa7-4793-bd82-bf1c8fd18832o%40googlegroups.com.
I commented out the database related code. You will need to get that fixed up and in the right spots.
If you want different tables on the tabs, you need to create tabs to hold that content. In the current code the tab content is only a Label. You many need to create a number of classes derived from Tab to capture the different formats for each type of data you want to display.
<Tab>:
# You would need to create the Layout you want on a tab here…
MDLabel:
id: label
text: "Tab 0"
halign: "center"
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6ff780d3-d57a-4472-a579-8af28882552do%40googlegroups.com.
MDNavigationDrawer: id: nav_drawer ScrollView: MDList: OneLineListItem: text: "Home" on_press: nav_drawer.set_state("close") screen_manager.current = "home" icon: 'language-python' OneLineListItem: text: "Football" on_press: nav_drawer.set_state("close") screen_manager.current = "fb"
<HomeScreen>: MDLabel: text: 'Home screen' halign: 'center'
<Tab>:
class Tab(FloatLayout, MDTabsBase): '''Class implementing content for a tab.''' fbName = StringProperty() fbPos = StringProperty() fbW = StringProperty() fbL = StringProperty() fbD = StringProperty() fbP = StringProperty()
class FBScreen(Screen):
def on_kv_post(self, base_widget): con = sqlite3.connect('test.db') cur = con.cursor() cur.execute("SELECT * FROM Soccer ORDER BY pos ASC") table = cur.fetchall()
for fruit in ['Test', 'A', 'B', 'C']:
t = Tab(text=fruit) t.ids.label.text = f'This is the label content for the {fruit} tab' self.ids.tabs.add_widget(t)
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
'''Called when switching tabs.''' if tab_text == 'tEST':
con = sqlite3.connect('test.db') cur = con.cursor()
cur.execute("SELECT * FROM Futbol ORDER BY sira ASC") table = cur.fetchall()
for line in table: pos, name, win, lose, draw, point = line
w = Tab(fbPos=str(pos), fbName=name, fbW=str(win), fbL=str(lose), fbD=str(draw), fbP=str(point)) self.ids.scroll_box.add_widget(w)
class ScrManage(ScreenManager): pass
class Sports(MDApp): def build(self): return Builder.load_string(kv)
Sports().run()
AttributeError: 'super' object has no attribute '__getattr__'
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6ff780d3-d57a-4472-a579-8af28882552do%40googlegroups.com.
Look a little higher in the error message:
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\ellio\PycharmProjects\HelpKivyMD\temp.py", line 144, in on_kv_post
t.ids.label.text = f'This is the label content for the {fruit} tab'
File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Your newly defined Tab object does not have an id label.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/549bdb67-d087-43e6-a4a4-225b98cec967o%40googlegroups.com.
cur.execute("SELECT * FROM Futbol ORDER BY sira ASC") table = cur.fetchall()
for fruit in ['Test', 'A', 'B', 'C']: t = Tab(text=fruit)
self.ids.tabs.add_widget(t)
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text): '''Called when switching tabs.'''
if tab_text == 'Test':
con = sqlite3.connect('test.db') cur = con.cursor() cur.execute("SELECT * FROM Futbol ORDER BY sira ASC") table = cur.fetchall() for line in table: pos, name, win, lose, draw, point = line w = Tab(fbPos=str(pos), fbName=name, fbW=str(win), fbL=str(lose), fbD=str(draw), fbP=str(point)) self.ids.scroll_box.add_widget(w)
class ScrManage(ScreenManager): pass
class Sports(MDApp): def build(self): return Builder.load_string(kv)
Sports().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/549bdb67-d087-43e6-a4a4-225b98cec967o%40googlegroups.com.
Look at your code below. It divides the screen into 3 parts.
The Tab content, the headings and the ScrollView. If you only want the tab content, remove the other sections.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/891dafb5-7971-44e2-9ed9-3336153f6251o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/891dafb5-7971-44e2-9ed9-3336153f6251o%40googlegroups.com.
Add it to your tab definition.
Currently you have defined on Tab class. If all the Tab classes will have the same layout you only need one Tab class. If you will have tabs with different layouts, you will need to create multiple tab classes. Put the layout of the widgets you want shown in the Tab class.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0f5171f0-a2cd-43ef-96cc-06631ac3b150o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0f5171f0-a2cd-43ef-96cc-06631ac3b150o%40googlegroups.com.
Here is an example:
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
from kivy.properties import StringProperty
KV = '''
BoxLayout:
orientation: "vertical"
MDToolbar:
title: "Example Tabs"
MDTabs:
id: tabs
on_tab_switch: app.on_tab_switch(*args)
OneLabelTab:
text: 'Apple'
label_text: 'This is a one label tab instance'
TwoLabelTab:
text: 'Orange'
top_label_text: 'This is a two label tab instance'
bottom_label_text: 'The bottom label'
OneLabelTab:
text: 'Banana'
label_text: 'This is another instance of a one label tab'
TwoLabelTab:
text: 'Avocado'
top_label_text: f'This is a two label tab on tab {self.text}'
bottom_label_text: f'The bottom label of {self.text}'
<OneLabelTab>: # One Label
MDLabel:
id: label
text: root.label_text
halign: "center"
<TwoLabelTab>: # Two Labels
BoxLayout:
orientation: 'vertical'
MDLabel:
id: label
text: root.top_label_text
halign: "center"
MDLabel:
id: label
text: root.bottom_label_text
halign: "center"
'''
class Tab(FloatLayout, MDTabsBase):
'''Class implementing content for a tab.'''
pass
class OneLabelTab(Tab):
label_text = StringProperty()
class TwoLabelTab(Tab):
top_label_text = StringProperty()
bottom_label_text = StringProperty()
class Example(MDApp):
def build(self):
return
Builder.load_string(KV)
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
'''Called when switching tabs.
print(f'Now on tab: {tab_text}')
if tab_text == 'Apple':
# make a sql query for the Apple Page
print('One a day keeps the Dr Away... ')
elif tab_text == 'Orange':
# Orange specific action
print('Make some juice')
elif tab_text == 'Banana':
print('Peel the Banana')
elif tab_text == 'Avocado':
print('Time for guacamole!')
Example().run()
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fff64f43-d3a4-4e7f-a19f-7058e4297713o%40googlegroups.com.