I do some art both analog and digital. This time I did some color test digitally. To work on my programming skill so that it's shareable I'm asking for help to get some code review.

  • How's the readability?
  • Is it easy to follow line of thought?
  • Suggestion as to how to improve?

I'm using John Zelles graphics.py for this

#A program for fun to see how red, blue and purple appears at resolution 

from math import *
from graphics import *

def hole(win, centerx, centery, radius): #draws an inverted sphere
    for circle in range(radius, 0, -1):
        c = Circle(Point(centerx, centery), circle)
        coloratangle = int(sin(circle/radius*pi/2)*255) #max resolution from black to white is 255 steps
        c.setFill(color_rgb(coloratangle * (circle % 2), 0, coloratangle * ((circle + 1) % 2))) # Modulus instead of Boolean? Modulus in c.setFill or in another line?

windowheight = 1350
windowwidth = 730
win = GraphWin("My Circle", windowheight, windowwidth)
win.setBackground(color_rgb(255, 100, 100))

centerx = int(windowheight / 2)
centery = int(windowwidth / 2) #intetger
radius = int(sqrt(centerx**2 + centery**2)) #pythagoras

hole(win, centerx, centery, radius)

win.getMouse() # Pause to view result
win.close()    # Close window when done
  • \$\begingroup\$ graphics doesn't seem to be in the standard library. It's probably "Zelle's graphics", available via pypi.org/project/graphics.py - seem right? \$\endgroup\$ – Reinderien Sep 22 '19 at 23:55
  • \$\begingroup\$ Yes its John Zelles graphics, sorry i forgot to write it in original post. Since you added this information is it no longer new information or should i ad it to question as new information? \$\endgroup\$ – Progrmming is fun Sep 23 '19 at 8:14
  • \$\begingroup\$ It's totally fine to add more explanatory information to your existing question. \$\endgroup\$ – Reinderien Sep 23 '19 at 13:29

I actually ran this. Good thing I don't have epilepsy, because wow. The flashing is intense.

There are many ways to improve this, depending on how technical you want to get. Move to a more advanced graphics library that's able to draw circles in an off-screen memory buffer before showing them in tkinter, your current windowing framework. This will speed up the rendering significantly and won't create that flashing (unless that's part of your art?).

There's a bug where moving your window creates even more flashing - this time, instead of being due to the concentric circles being rendered from the outside in, I suspect it's from the circles being re-rendered by graphics.py rather than flattening the representation of the image to a rasterized buffer. Rendering to an off-screen buffer will fix this, too.

There's another bug: closing the window produces this stack trace -

  File "/opt/pycharm-community-2019.2.1/helpers/pydev/pydevd.py", line 2060, in <module>
  File "/opt/pycharm-community-2019.2.1/helpers/pydev/pydevd.py", line 2054, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/opt/pycharm-community-2019.2.1/helpers/pydev/pydevd.py", line 1405, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "/opt/pycharm-community-2019.2.1/helpers/pydev/pydevd.py", line 1412, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/opt/pycharm-community-2019.2.1/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/gtoombs/PycharmProjects/zelles/zelles.py", line 25, in <module>
    win.getMouse() # Pause to view result
  File "/home/gtoombs/.virtualenvs/zelles/lib/python3.7/site-packages/graphics/__init__.py", line 316, in getMouse
    if self.isClosed(): raise GraphicsError("getMouse in closed window")
graphics.GraphicsError: getMouse in closed window

which means that you're not handling shut-down gracefully. See if you can change your exit condition to be either when the mouse event is received or the window is closed.

  • \$\begingroup\$ Thank you for running the program and lending your insights wich are eye opening to me. When i look into the ways to improve the program and probably run into hard to solve obstacles should i post as a comment to make a thread or state a new post question? For example the computer im using run out of memory for some fractals rendering. I was thinking about saving it to image every number of iterations to skip layers behind top circle. Is flattening to skip layers behind top circle? \$\endgroup\$ – Progrmming is fun Sep 23 '19 at 8:40
  • 1
    \$\begingroup\$ If you're asking clarifying questions to my answer, you can do so in the comments. If you're running out of memory, that's a question for StackOverflow. Yes, flattening forgets all of the layers and all of the information about circle sizes, only saving the image. \$\endgroup\$ – Reinderien Sep 23 '19 at 13:31
  • \$\begingroup\$ Hi:) After trying to get a more gracefull shut down I thought the program might be better off without the last two lines? \$\endgroup\$ – Progrmming is fun Sep 24 '19 at 14:55
  • \$\begingroup\$ You still need something to pause the program. Replace those two lines with win.wait_window(). \$\endgroup\$ – Reinderien Sep 24 '19 at 15:14
  • \$\begingroup\$ when i do so and exit without clicking in window i get "error" message. Without those three lines the window hovers paused even though the program is stopped in idle and every way ive closed the window no "error" message. Is there an error message im not seeing with default idle? \$\endgroup\$ – Progrmming is fun Sep 24 '19 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.