You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python脚本分离函数调用,实现跨脚本调用指定绘图函数

解决方案:调整脚本结构以支持外部调用绘图函数

你的需求完全可以实现!我们需要拆分设备初始化与绘图逻辑,同时给外部脚本留出灵活调用的入口。下面是修改后的完整代码:

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 init_device():
    # 单独抽离设备初始化逻辑,方便外部获取已配置的设备实例
    from luma.core.interface.serial import spi
    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("Device initialized successfully!")
    return device

def main():
    # 原main函数改为初始化设备的入口,不再自动循环调用primitives1
    device = init_device()
    print("Testing basic canvas graphics...")
    # 若需要直接运行本脚本时做测试,可保留以下调用;不需要则删除
    with canvas(device) as draw:
        primitives1(device, draw)
    time.sleep(3)
    print("Testing clear display...")
    time.sleep(1)
    device.clear()

# 可选:给绘图函数包装一层带canvas上下文的调用,简化外部使用
def draw_primitives1(device):
    with canvas(device) as draw:
        primitives1(device, draw)

def draw_primitives2(device):
    with canvas(device) as draw:
        primitives2(device, draw)

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass

关键修改说明

  • 拆分设备初始化:新增init_device()函数,把原来main里的设备创建逻辑单独抽离,返回设备实例。外部脚本可以通过这个函数快速获取已初始化好的设备。
  • 移除自动调用逻辑:原main函数中循环调用primitives1的代码已删除,现在直接运行脚本只会初始化设备并执行单次测试(可按需去掉)。
  • 支持外部灵活调用:其他脚本可以这样使用:
    import your_script_name as draw_utils
    
    # 第一步:初始化设备
    device = draw_utils.init_device()
    
    # 方式1:自己管理canvas上下文调用绘图函数
    with draw_utils.canvas(device) as draw:
        draw_utils.primitives1(device, draw)
    
    # 方式2:用封装好的函数直接调用(更简洁)
    draw_utils.draw_primitives2(device)
    

关于空with canvas(device) as draw:的说明

直接保留空的上下文块虽然不会报错,但完全没有实际意义——canvas的设计是为了在上下文内完成绘图操作,所以我们要么把上下文管理交给外部调用者,要么在本脚本里给绘图函数封装好上下文逻辑,这样才符合工具类脚本的设计逻辑。

内容的提问来源于stack exchange,提问作者Craver2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:15:00