KivyMD Using Tabs

993 views
Skip to first unread message

kivy

unread,
Jan 22, 2021, 2:28:56 PM1/22/21
to Kivy users support
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?

Elliot Garbus

unread,
Jan 22, 2021, 2:58:03 PM1/22/21
to kivy-...@googlegroups.com

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.

 

kivy

unread,
Jan 22, 2021, 3:07:27 PM1/22/21
to Kivy users support
Can I use three times same method (on_tab_switch()) under same class?

22 Ocak 2021 Cuma 22:58:03 UTC+3 tarihinde ElliotG yazdı:

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.

Elliot Garbus

unread,
Jan 22, 2021, 3:20:59 PM1/22/21
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.

 

Elliot Garbus

unread,
Jan 22, 2021, 3:38:55 PM1/22/21
to kivy-...@googlegroups.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()

kivy

unread,
Jan 22, 2021, 4:08:12 PM1/22/21
to Kivy users support
Thank you so much! I'll try it now!

22 Ocak 2021 Cuma 23:38:55 UTC+3 tarihinde ElliotG yazdı:

--
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.

kivy

unread,
Jan 25, 2021, 8:42:26 AM1/25/21
to Kivy users support
I make changes on my app but I can't see any tab name.

Here is my screen's code: 

<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

I can't see anything.

And class code:

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!')


22 Ocak 2021 Cuma 23:38:55 UTC+3 tarihinde ElliotG yazdı:

Here is an example:

--
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.

Elliot Garbus

unread,
Jan 25, 2021, 12:41:50 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 2:21:33 PM1/25/21
to Kivy users support
Now, my code is like that:

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!')

But still doesn't appear anything on screen. Where is the problem?


25 Ocak 2021 Pazartesi 20:41:50 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 2:27:48 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 2:33:37 PM1/25/21
to Kivy users support
Sure, sorry about that. I shared missing code.

Here it is:

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import sqlite3
from kivymd.app import MDApp
from kivy.properties import ListProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from 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()




25 Ocak 2021 Pazartesi 22:27:48 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 3:22:22 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 3:26:05 PM1/25/21
to Kivy users support
I commented this section but still doesn't appears tabs on Football screen.

25 Ocak 2021 Pazartesi 23:22:22 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 3:38:24 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 3:56:38 PM1/25/21
to Kivy users support
It's working, thank you so much! But now, problem is I can't get table from database and display it to screen. And one more problem. Screen is double now for football area. I just want to get different tables from every tabs. How can I configure my code?

25 Ocak 2021 Pazartesi 23:38:24 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 4:04:31 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 4:22:55 PM1/25/21
to Kivy users support
If I try to run like this:

       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()


I'm getting this error:

 AttributeError: 'super' object has no attribute '__getattr__'

Where is the problem?


26 Ocak 2021 Salı 00:04:31 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 4:30:49 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 4:50:30 PM1/25/21
to Kivy users support
Here is the problem (click the link please): https://prnt.sc/xmdwyy

And my code is like that now: 

       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()



26 Ocak 2021 Salı 00:30:49 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 5:19:48 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 25, 2021, 5:30:18 PM1/25/21
to Kivy users support
How can I move table to tab screen?

26 Ocak 2021 Salı 01:19:48 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 25, 2021, 5:52:29 PM1/25/21
to kivy-...@googlegroups.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.

 

kivy

unread,
Jan 26, 2021, 2:02:38 PM1/26/21
to Kivy users support
Can you explain it more please?

26 Ocak 2021 Salı 01:52:29 UTC+3 tarihinde ElliotG yazdı:

Elliot Garbus

unread,
Jan 26, 2021, 3:42:56 PM1/26/21
to kivy-...@googlegroups.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.

 

Reply all
Reply to author
Forward
0 new messages