Reputation: 453
I have an existing script that contains functions that I would like to call from another script. I would like to modify it such that the main
function does not automatically call the primitives
function.
The full script reads:
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
Is there a way that I can remove primitives1(device, draw)
from def main
but still retain the with canvas(device) as draw:
? If I leave with canvas(device) as draw:
there without calling primitives1
, terminal will print an error if I try to launch the script.
The reason that I want to do this is so that I can use another script to call main()
first and then choose to call either primitives1
or primitives2
.
Upvotes: 0
Views: 50
Reputation: 2560
I would modify the main function to take in the primitivesX function definition. You want to use like this...
def main(prim_func):
...
for _ in range(2):
with canvas(device) as draw:
prim_func(device, draw)
You would then call main something like...
main(primitives1)
or...
main(primatives2)
Note that your passing in the declared name of the function, not an instance of it. Of course, be sure that the name is defined somewhere in your script or imported.
Upvotes: 1