kivymd / KivyMD

KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphical applications. https://youtube.com/c/KivyMD https://twitter.com/KivyMD https://habr.com/ru/users/kivymd https://stackoverflow.com/tags/kivymd
https://kivymd.readthedocs.io
MIT License
2.22k stars 670 forks source link

MDTabs on_text event not working properly #602

Closed ElLoko233 closed 3 years ago

ElLoko233 commented 3 years ago

Description of the Bug

When I use the on_text event to print text to my console the tab text on the app disappears and a new empty tab is addded.

Code and Logs

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.tab import MDTabsBase

from kivy.properties import StringProperty, ListProperty, ObjectProperty, DictProperty

class EasyMixCalculatorApp(MDApp):

    def __init__(self, **kwargs):
        super(EasyMixCalculatorApp, self).__init__(**kwargs)
        self.grey = [226 / 255, 226 / 255, 226 / 255, 1]
        self.yellow = [255 / 255, 218 / 255, 6 / 255, 1]
        self.blue = [0 / 255, 35 / 255, 234 / 255, 1]
        self.red = [226 / 255, 19 / 255, 19 / 255, 1]
        self.products = {'Muffins': {'one':1, 'two':2, 'three':3}}

    def build(self):
        # self.theme_cls.theme_style = 'Dark'
        return Builder.load_string(kv)

    @staticmethod
    def change_screen(manager: ScreenManager, page, direction='left'):
        manager.transition.direction = direction
        manager.current = page

class ProductScreener(Screen, MDTabsBase):

    orgDisplay = ObjectProperty()

    title = StringProperty(name="title")
    original_ingredients = DictProperty()

    def on_text(self, *args):
        print('hello', )

kv = """
ScreenManager:
    name: srn_mgr
    id: srn_mgr

    MDScreen:
        name: "HomePage"

        BoxLayout:
            orientation: 'vertical'

            MDToolbar:
                title: "EM Calculator"
                right_action_items: [["help-circle-outline", lambda x: app.change_screen(srn_mgr, 'HelpPage')], ["comment-question", lambda x: app.change_screen(srn_mgr, "AboutPage")]]
                md_bg_color: app.yellow
                specific_text_color: app.blue
                elevation: 10

            MDTabs:
                id: TopNav
                background_color: app.yellow
                text_color_normal: app.blue
                text_color_active: app.red
                color_indicator: app.blue

                tab_bar_height: '30dp'
                tab_indicator_anim: True

                ProductScreener:
                    id: coffee
                    title: 'Coffee'
                    text: self.title
                    original_ingredients: app.products['Muffins']

                ProductScreener:
                    id:  muffins
                    title: 'Muffins'
                    text: self.title

<ProductScreener>:

    ScrollView:
        do_scroll_y: True
        do_scroll_x: False

        MDBoxLayout:
            orientation: 'vertical'
            adaptive_height: True
            padding: '20dp'
            spacing: '20dp'

            MDCard:
                id: orgCard
                orientation: "vertical"
                size_hint: [0.9, None]
                height: self.minimum_height
                pos_hint: {"center_x": .5, "center_y": .5}
                spacing: '10dp'
                elevation: '0.5dp'

                MDBoxLayout:
                    adaptive_height: True
                    padding: '10dp'
                    md_bg_color: app.yellow

                    MDLabel:
                        font_style: 'H5'
                        text: root.title
                        theme_text_color: "Custom"
                        text_color: app.blue
                        size_hint_y: None
                        height: self.texture_size[1]

                MDGridLayout:
                    id: orgDisplay
                    cols: 2
                    rows: len(root.original_ingredients)
                    adaptive_height: True
                    padding: '10dp'
                    spacing: '10dp'

            MDGridLayout:
                cols: 2
                rows: 1
                adaptive_height: True

                AnchorLayout:
                    MDFlatButton:
                        text: '#Ingredients'
                        text_color: app.yellow
                        md_bg_color: app.blue

                AnchorLayout:
                    MDFlatButton:
                        text: '#Products'
                        text_color: app.yellow
                        md_bg_color: app.blue
"""

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

Screenshots

kivyproblem

Versions

HeaTTheatR commented 3 years ago

@ElLoko233 You override the internal method, removing all code from it. Naturally this won't work. You need to do it like this:

class ProductScreener(Screen, MDTabsBase):

    orgDisplay = ObjectProperty()

    title = StringProperty(name="title")
    original_ingredients = DictProperty()

    def on_text(self, widget, text):
        print('hello', )
        # Set the icon
        if text in md_icons:
            self.tab_label.font_name = (
                fonts_path + "materialdesignicons-webfont.ttf"
            )
            self.tab_label.text = md_icons[self.text]
            self.tab_label.font_size = "24sp"
        # Set the label text
        else:
            self.tab_label.text = self.text