简体   繁体   English

函数中的 Python 参数

[英]Python arguments in function

For context, I have a function that is used to generate a button.对于上下文,我有一个用于生成按钮的函数。 The last two arguments of the function are used to fun another function when the button is pressed.该函数的最后两个参数用于在按下按钮时使另一个函数变得有趣。

Im having problems with a button such as this, when Im trying to call the function (and arguments) createWorksheet(sheetTitle,sheetDate,sheetFilename) when the button is pressed.当我尝试在按下按钮时调用函数(和参数)createWorksheet(sheetTitle,sheetDate,sheetFilename) 时,我遇到了这样的按钮问题。

I aimed to do it using this code:我打算使用以下代码来做到这一点:

button("Create Sheet",200,500,200,50,GREEN,BRIGHTGREEN,createWorksheet,sheetTitle,sheetDate,sheetFilename) 

but this gives the error但这给出了错误

button() takes from 7 to 9 positional arguments but 11 were given

Instead I tried with the arguments in a tuple (as below)相反,我尝试使用元组中的参数(如下所示)

button("Create Sheet",200,500,200,50,GREEN,BRIGHTGREEN,createWorksheet,(sheetTitle,sheetDate,sheetFilename))

but this throws an error:但这会引发错误:

createWorksheet() missing 2 required positional arguments: 'date' and 'filename'    

Any ideas please?请问有什么想法吗?

This is the button code to generate the function这是生成函数的按钮代码

def button(text, posX, posY, width, height, inactiveColor, activeColor,action=None,actionArgs=None):
    global buttonDown

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if posX + width > mouse[0] > posX and posY + height > mouse[1] > posY:

        pygame.draw.rect(displays, activeColor, (posX,posY, width, height))

        if click[0] == 1 and not buttonDown and action!= None:

            if actionArgs is not None:
                action(actionArgs)
            else:
                action()

            buttonDown = True

        elif click[0] == 0:
            buttonDown = False
    else:
        pygame.draw.rect(displays,inactiveColor,(posX,posY,width,height))

    textSurf, textRect = text_objects(text, smallfont)
    textRect.center = ( (posX + (width/2)), (posY+(height/2)) )
    displays.blit(textSurf, textRect)

I think that the second invocation, ie using a tuple for the args, is correct.我认为第二次调用,即对 args 使用元组,是正确的。 However you need to pass the arguments to the action function as positional arguments, not as a tuple.但是,您需要将参数作为位置参数而不是元组传递给action函数。

You don't show the definition for createWorksheet() but assuming that it takes the 3 arguments from your example, you would call it like this:您没有显示createWorksheet()的定义,但假设它从您的示例中获取 3 个参数,您可以这样称呼它:

        if actionArgs is not None:
            action(*actionArgs)
        else:
            action()

This unpacks the tuple into separate values, and passes these to the function.这将元组解包为单独的值,并将这些值传递给函数。 The difference is this:区别在于:

args = (sheetTitle, sheetDate, sheetFilename)
action(args)    # passes a single argument of type tuple

vs.对比

action(*args)   # passes each element of the tuple as a positional argument.

The second case is the same as:第二种情况与以下相同:

action(sheetTitle, sheetDate, sheetFilename)

If you have only one argument to pass, you still need to pass it as a one element tuple to button() , like this:如果您只有一个参数要传递,您仍然需要将它作为单元素元组传递给button() ,如下所示:

button("Create Sheet",200,500,200,50,GREEN,BRIGHTGREEN,createWorksheet,(sheetTitle,))

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

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