Disable fade in and out animation of KivyMD toast

565 Views Asked by At

I am trying to create an app and use a toast to show a message, but when the toast is displayed the background changes to a darker color (see image below). Is there a way to disable this, so just the toast is displayed without a fade animation.

Note that I installed kivy with pip, I typed: pip install https://github.com/kivy/kivy/archive/master.zip

enter image description here

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.toast import toast

KV = '''
BoxLayout:
    orientation:'vertical'

    MDToolbar:
        id: toolbar
        title: 'Test Toast'
        md_bg_color: app.theme_cls.primary_color
        left_action_items: [['menu', lambda x: '']]

    FloatLayout:

        MDRaisedButton:
            text: 'TEST KIVY TOAST'
            on_release: app.show_toast()
            pos_hint: {'center_x': .5, 'center_y': .5}

'''

class Test(MDApp):
    def show_toast(self):
        '''Displays a toast on the screen.'''

        toast('Test Kivy Toast')

    def build(self):
        return Builder.load_string(KV)

Test().run()
1

There are 1 best solutions below

0
On

The fade in and fade out are hard wired characteristics of Toast. There is no way to change that. However, you can create your own version of that class by extending Toast, something like this:

from kivymd.toast.kivytoast.kivytoast import Toast


class MyToast(Toast):

    def fade_in(self):
        self.label_toast.opacity = 1
        self.opacity = 1

    def fade_out(self, interval):
        self.label_toast.opacity = 0
        self.opacity = 0
        self.dismiss()

I haven't tested this code, but this should give you the idea of how to approach this. Also, note that if this app ends up as an Android App, then the above code will not be used, but the actual Android Toast would be used.