0

I want to reduce my code via general function for changing screens. my screens have names as numbers e.g name: "2".. when you click on certain list in bottomsheet I want to changing certain screen.

in .py

    def show_bottom_sheet(self):
        bs = MDListBottomSheet()
        bs.add_item("Models", lambda x: x,icon='account-group-outline')

        for y in 1,2,3,4,12,13,14,21,23,24,31,32,34,41,42,43,123,124,134,234:
            bs.add_item(f"example {y} ", lambda x= y: self.behavior(x), icon='account-group-outline')
            bs.open()

    def behavior(self, x):
        self.manager.current = str(x)

before my changes it was like this, but for every single number and it was too useless

def behavior2(self):
    self.manager.current = "2"

I tried it like this but it gives me this error

 File "C:\Users\Stanko\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\uix\screenmanager.py", line 1064, in get_screen
     raise ScreenManagerException('No Screen with name "%s".' % name)
 kivy.uix.screenmanager.ScreenManagerException: No Screen with name "<kivymd.uix.list.OneLineIconListItem object at 0x0AADB088>".

Panakova1
  • 11
  • 5
  • The error tells you that you are trying to set the current screen using a `OneLineIconListItem` where it should be a string name. – John Anderson Feb 05 '21 at 14:02
  • instead of `f"{x}"` you can use more readable `str(x)` – furas Feb 05 '21 at 16:30
  • if you run `lambada` in loop then you may nee `x=y` in `lambda x=y: self.behavior(x)` to assign correctly value. If you use `lambda y: self.behavior(x)` then it will use reference to `y` instead of value from `y` and later all lambdas will use last value from loop - `234` – furas Feb 05 '21 at 16:32
  • code `'account-group-outline'.format(y)` makes no sense - you don't have `{}` in text. – furas Feb 05 '21 at 16:33
  • thank you, i changed the code above, but it still referring to the List object. could you please give me a certain line which can solve this problem? – Panakova1 Feb 07 '21 at 09:20

0 Answers0