py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

Bug in `py5_tools.screenshot` when used in class mode #171

Closed hx2A closed 2 years ago

hx2A commented 2 years ago
  I'm currently trying to use `py5_tools.screenshot` in class mode, but I'm having trouble getting it to work, so any advice would be great!
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 s, got error output:

❯ 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

Originally posted by @zztkm in https://github.com/py5coding/py5generator/discussions/170

hx2A commented 2 years ago

Need to do two things:

hx2A commented 2 years ago

Fixed by #172