-
I'm currently trying to use from py5 import Sketch
from py5_tools import screenshot
from utils import get_timestamp
class FirstSketch(Sketch):
def settings(self):
self.size(600, 600)
def setup(self):
self.rect_mode(self.CENTER)
def draw(self):
self.rect(self.mouse_x, self.mouse_y, 30, 30)
def key_typed(self):
if self.key == "s":
time_stamp = get_timestamp()
img = screenshot(sketch=self, hook_post_draw=True)
img.save(f"{time_stamp}.png")
def main():
sketch = FirstSketch()
sketch.run_sketch()
if __name__ == "__main__":
main() When typing ❯ python main.py
py5 encountered an error in your code:File "C:\Users\zztkm\dev\sandbox\pydev\py5-example\main.py", line 20, in key_typed
17 def key_typed(self):
18 if self.key == "s":
19 time_stamp = get_timestamp()
--> 20 img = screenshot(sketch=self, hook_post_draw=True)
21 img.save(f"{time_stamp}.png")
..................................................
self = <__main__.FirstSketch object at 0x000002882B98BA90>
..................................................
UnboundLocalError: local variable 'py5' referenced before assignment |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hi, @zztkm! It seems like you've found a bug. Thank you for reporting this. There are few things you can do differently. First, I recommend you use https://py5coding.org/reference/sketch_save_frame.html I created Still, the code you wrote should work if Another solution is to remove |
Beta Was this translation helpful? Give feedback.
-
Hi @hx2A! Thanks for the reply! Oh, it was a bug. I thought I had written some crazy code! There was one mistake in the code I posted. supplement: |
Beta Was this translation helpful? Give feedback.
-
I took your advice and used Here is the code that worked correctly in case others encounter similar problems. from datetime import datetime
from py5 import Sketch
def get_timestamp() -> str:
now = datetime.now()
return now.strftime("%Y-%m-%d-%H-%M-%S")
class FirstSketch(Sketch):
def settings(self):
self.size(600, 600)
def setup(self):
self.rect_mode(self.CENTER)
def draw(self):
self.rect(self.mouse_x, self.mouse_y, 30, 30)
def key_typed(self):
if self.key == "s":
time_stamp = get_timestamp()
self.save_frame(f"{time_stamp}.png")
print(f"save to {time_stamp}.png")
def main():
sketch = FirstSketch()
sketch.run_sketch()
if __name__ == "__main__":
main() output: > python main.py
save to 2022-10-04-09-40-56.png |
Beta Was this translation helpful? Give feedback.
Hi, @zztkm! It seems like you've found a bug. Thank you for reporting this.
There are few things you can do differently. First, I recommend you use
save_frame()
instead ofpy5_tools.screenshot()
here.https://py5coding.org/reference/sketch_save_frame.html
I created
py5_tools.screenshot()
with the goal of providing an easy way for you to grab an arbitrary screenshot of a running sketch and have the image embedded in a Jupyter notebook. I should make it more clear in the documentation forpy5_tools.screenshot()
thatsave_frame()
is also available and is a better choice for saving a frame from within a running Sketch.Still, the code you wrote should work if
hook_post_draw=False
. Thehook_po…