0

I'm quite new to python and still learning. I am currently have some difficulties using the import function in python. script2.py contains functions that enable me to use my OLED display. I currently have problems as when def primitives is called, I get an error that says it has not been defined.

The functions from this script (script2.py) are what I want to import to script1.py (primitives, main):

import time
import datetime
from luma.core.render import canvas
def primitives(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), 'World1!', 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:
 primitives(device, draw)
 print("Drawing stuff 1")
 time.sleep(3)
 print("Testing clear display...")
 time.sleep(1)
 device.clear()
if __name__ == "__main__":
 try:
 main()
 except KeyboardInterrupt:
 pass

The script that is importing these functions (script1.py1) is:

import time 
import otherscript
variable_intermediate=1
while True:
 if variable_intermediate==1:
 from script2 import primitives, main #does not seem to work
 main()
 primitives (device, draw) #error printed in terminal, it says draw not defined
 time.sleep(0.01)
asked Jan 25, 2018 at 13:37
2
  • 1
    "does not seem to work" how? Obviously just importing the functions won't do anything, you actually need to call them - which you only do in the third snippet. Commented Jan 25, 2018 at 13:40
  • When you import from script2 import primitives, main in your script you allow the first script to access theses function but if you want to run them you have to call them Commented Jan 25, 2018 at 13:41

1 Answer 1

1

Try this : Script1.py

import time 
import otherscript
variable_intermediate=1
while True:
 if variable_intermediate==1:
 from script2 import primitives, main 
 main() # this is the line you're missing
 time.sleep(0.01)
answered Jan 25, 2018 at 13:42
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, I got what you mean. I now added main() and primitives (device,draw)
But I got another error: NameError: name 'draw' is not defined
Isn't draw already defined when I imported and call main()?
The line with primitives(device, draw)
That's normal, draw is not defined in script1.py, you should start primitive() from the script2.py But that is already what you are doing in your definition of main(), you call primitives(device, draw). Why do you call it twice ?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.