Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将函数调用与此函数分开_Python_Function_Import - Fatal编程技术网

Python 如何将函数调用与此函数分开

Python 如何将函数调用与此函数分开,python,function,import,Python,Function,Import,我有一个现有的脚本,其中包含我想从另一个脚本调用的函数。我想修改它,使main函数不会自动调用primitives函数 全文如下: import time import datetime from luma.core.render import canvas def primitives1(device, draw): # First define some constants to allow easy resizing of shapes. padding = 2

我有一个现有的脚本,其中包含我想从另一个脚本调用的函数。我想修改它,使main函数不会自动调用primitives函数

全文如下:

import time
import datetime
from luma.core.render import canvas


def primitives1(device, draw):
    # First define some constants to allow easy resizing of shapes.
    padding = 2
    shape_width = 20 
    top = padding
    bottom = device.height - padding - 1
    # Move left to right keeping track of the current x position for drawing shapes.
    x = padding
    # Write two lines of text.
    size = draw.textsize('World!')
    x = device.width - padding - size[0]
    draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
    draw.rectangle((x, top + 16, x + size[0], top + 16 + size[1]), fill="black")
    draw.text((device.width - padding - size[0], top + 4), 'Hello', fill="cyan") 
    draw.text((device.width - padding - size[0], top + 16), 'World!', fill="purple") 
    time.sleep(5)

    def primitives2(device, draw):
    # First define some constants to allow easy resizing of shapes.
    padding = 2
    shape_width = 20 
    top = padding
    bottom = device.height - padding - 1
    # Move left to right keeping track of the current x position for drawing shapes.
    x = padding

    # Write two lines of text.
    size = draw.textsize('World!')
    x = device.width - padding - size[0]
    draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
    draw.rectangle((x, top + 16, x + size[0], top + 16 + size[1]), fill="black")
    draw.text((device.width - padding - size[0], top + 4), 'Bye', fill="cyan") 
    draw.text((device.width - padding - size[0], top + 16), 'Bye!', fill="purple") 
    time.sleep(5)    

def main():
    from luma.core.interface.serial import spi
    from luma.core.render import canvas
    from luma.oled.device import ssd1351
    serial = spi(device=0, port=0, gpio_DC=20)
    device = ssd1351(serial)
    device.width=128
    device.height=128
    print("Testing basic canvas graphics...")
    for _ in range(2):
        with canvas(device) as draw:
            primitives1(device, draw)
    time.sleep(3)
    print("Testing clear display...")
    time.sleep(1)
    device.clear()



if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass
是否有一种方法可以删除primitives1device,从def main中绘制,但仍保留canvasdevice作为draw:?如果我将canvasdevice作为draw:there离开而不调用primitives1,则如果我尝试启动脚本,终端将打印错误


我想这样做的原因是,我可以先使用另一个脚本调用main,然后选择调用primitives1或primitives2。

我会修改main函数以接受primitivesX函数定义。你想这样使用

def main(prim_func):
    ...
    for _ in range(2):
        with canvas(device) as draw:
            prim_func(device, draw)
然后你会把main叫做

main(primitives1) 
or...
main(primatives2)

请注意,您传递的是函数的声明名称,而不是它的实例。当然,请确保该名称在脚本中的某个位置定义或导入。

您可以注释掉primitives1device、draw和put in pass,但我仍然不知道为什么您不能注释掉整个for循环。感谢bivouac0,这解决了错误。然而,在我使用的另一个脚本script2.py中:从script1 import primitives1,main primitives1device,在我调用primitives1的行上绘制,我得到一个错误,说绘制没有定义。为什么我在调用main时会出现这个错误,而canvasdevice作为绘制:因为绘制不是全局的,它是在函数中定义的。我会做下面的一件事。。。1-将要使用的primativeX函数传递给main,如。。。mainprim_func,然后在with canvas或2-return设备下使用该名称,并将with canvas。。。在你的剧本里,我不太确定你说1是什么意思。我所做的是这些更改,包括def mainx:以及将canvasdevice作为draw:x修改为script1,在另一个脚本中,我做了:mainprimitives1device,draw。这就是我应该做的吗?我仍然得到了抽签未定义的错误。谢谢!这对我有用!所以这个方法允许次函数成为主函数的参数。这允许辅助函数的参数保持不变,并避免名称未定义错误。