简体   繁体   English

如何使用 f“{}” Python 简化代码

[英]How can I simplify code with f“{}” Python

I want to simplify my code, I did it via f"{}".我想简化我的代码,我是通过 f"{}" 完成的。 But now I need when you click on certainly item, to switch a screen with certain number.但是现在我需要当你点击确定项目时,切换一个具有特定数字的屏幕。 But I don't know how.但我不知道怎么做。 Are there any possibilities?有没有可能?

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

    for y in 1,2,3,4,12,13:

        bs.add_item(f"number {y} ", lambda x: self.behavior(), icon='account-group-outline'.format(y))
        bs.open()

instead of代替

    #bs.add_item("2", lambda x: self.behavior2(), icon='account-group-outline')
    #bs.add_item("3", lambda x: self.behavior3(),icon='account-group-outline' )
    #bs.add_item("4", lambda x: self.behavior4(),icon='account-group-outline' )
    #bs.add_item("12", lambda x: self.behavior12(),icon='account-group-outline' )
    #bs.add_item("13", lambda x: self.behavior13(), icon='account-group-outline')

I tried something like this but unsuccessful:我尝试过这样的事情但没有成功:

def behavior(self):
    for y in 1, 2, 3, 4, 12, 13:
        self.manager.current = f"{y}"

I had it before like this我以前有过这样的

def behavior2(self):
    self.manager.current = "2"
def behavior3(self):
    self.manager.current = "3"
def behavior4(self):
    self.manager.current = "4"
def behavior12(self):
    self.manager.current = "12"
def behavior13(self):
    self.manager.current = "13"
def behavior14(self):

I think that your behavior() method needs to take a parameter:我认为您的behavior()方法需要带一个参数:

bs.add_item(f"number {y} ", lambda x: self.behavior(x), icon='account-group-outline'.format(y))

...

def behavior(self, x):
    self.manager.current = f"{x}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM