首页 > 解决方案 > Python:换屏的简单函数

问题描述

我想通过更改屏幕的通用功能来减少我的代码。我的屏幕名称为数字,例如名称:“2”..当您单击底部表格中的某个列表时,我想更改某个屏幕。

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

在我改变之前是这样的,但是对于每一个数字,它太长了

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

我该如何解决?

标签: pythonkivy

解决方案


这是一种方法:

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} ", partial(self.behavior, y), icon='account-group-outline')
        bs.open()


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

推荐阅读