'Disable and enable MDRectangleFlatButton after another button is clicked in kivymd?

I am making an app that takes two files and then does some operation using the uploaded files. I have used kivy's MDFileManager to get the files when the buttons are pressed. I want to disable the "Upload .csv" button until the other file is uploaded. How to disable and enable "Upload .csv" - a MDRectangleFlatButton after another button(Upload .sb3) is clicked and a path is chosen from FileManager in kivymd? [![app][1]][1] Here is the code:

import os
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.filemanager import MDFileManager
from kivymd.toast import toast

screen_helper ="""
<UploadScreen>:
    name: 'upload'
    MDLabel:
        text: 'Upload CSV file'
        halign: 'center'
        font_style: "H4"
    MDRectangleFlatButton:
        id: but_two
        text: 'Upload .csv'
        pos_hint: {'center_x':0.75,'center_y':0.35}
        on_release: app.file_manager_open()
        
    MDRectangleFlatButton:
        id: but_one
        text: 'Upload .sb3'
        pos_hint: {'center_x':0.5,'center_y':0.35}
        on_release: app.file_manager_open()
        
    MDRectangleFlatButton:
        text: 'Back'
        pos_hint: {'center_x':0.25,'center_y':0.35}
        on_release: root.manager.current = 'menu'
<MDFileManager>:
    preview: True
"""

class UploadScreen(Screen):
    pass


def get_download_path():
    """Returns the default downloads path for linux or windows"""
    if os.name == 'nt':
        import winreg
        sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
        downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
            location = winreg.QueryValueEx(key, downloads_guid)[0]
        return location
    else:
        return os.path.join(os.path.expanduser('~'), 'downloads')


# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(ProfileScreen(name='profile'))
sm.add_widget(UploadScreen(name='upload'))


class DemoApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.manager_open = False
        self.file_manager = MDFileManager(
            exit_manager=self.exit_manager,
            select_path=self.select_path,
            preview=True,
        )
        self.file_manager.ext = [".sb3",".csv"]
    def select_path(self,path):
        '''It will be called when you click on the file name
        or the catalog selection button.

        :type path: str;
        :param path: path to the selected directory or file;
        '''
        self.exit_manager()
        toast(path)

    def file_manager_open(self):
        self.file_manager.show(get_download_path())  # output manager to the screen
        self.manager_open = True

    def exit_manager(self):
        '''Called when the user reaches the root of the directory tree.'''
        self.manager_open = False
        self.file_manager.close()

    
    def build(self):
        self.theme_cls.material_style = "M3"
        self.theme_cls.primary_palette = "Purple"
        self.theme_cls.theme_style = "Dark"
        screen = Builder.load_string(screen_helper)
        return screen


DemoApp().run()

I am a newbie in Kivy environment so other suggestions regarding the code are also welcomed. [1]: https://i.stack.imgur.com/hMdRQ.png



Solution 1:[1]

You can set a flag and bind that to the disabled attr. of MDRectangleFlatButton as,

class DemoApp(MDApp):
    in_progress = BooleanProperty(True)

Then in kvlang,

    ...
    MDRectangleFlatButton:
        id: but_two
        text: 'Upload .csv'
        disabled: app.in_progress
    ...

Now when you call exit_manager,

    def exit_manager(self, *args):
        '''Called when the user reaches the root of the directory tree.'''
        self.manager_open = False
        self.in_progress = False # The button will be enabled before closing file manager.
        self.file_manager.close()
        #self.in_progress = False # The button will be enabled after closing file manager.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ApuCoder