如何在python中使用click只传递一个参数?

2024-04-24 07:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新使用click package。我有一个有两个参数的函数,我只想通过click传递其中一个参数,参数i在语句if中传递。你知道吗

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

@click.command()
@click.argument('filename')
def plot_graph(i, filename):
    ...

if __name__ == '__main__':
    graph_animated = animation.FuncAnimation(fig, plot_graph, interval=1000)

谢谢你!你知道吗


Tags: 函数addpackage参数ifplotfigplt
1条回答
网友
1楼 · 发布于 2024-04-24 07:38:24

只需将相关代码包装在“command”函数中,并在filename变量上使用闭包:

import click
import matplotlib.animation as animation
import matplotlib.pyplot as plt


@click.command()
@click.argument('filename')
def main(filename):
    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)

    def plot_graph(i):
        # Closure over `filename` here.
        # Alternatively you can use additional kwarg `filename=filename`.

    graph_animated = animation.FuncAnimation(fig, plot_graph, interval=1000)
    plt.show()


if __name__ == '__main__':
    main()

相关问题 更多 >