Tuesday, January 3, 2023

Done

 




Now showing RGB values 'onscreenclick'. This is turtle as

a stand alone application, rather than as a window in a tkinter app.

The code works because this version of Turtle is on top of tkinter.


All this made me appreciate certain features of turtle. Set world coordinates 

allows one to define the bottom left and top right corners of the screen to one's

convenience. One can show turtle, here the three are called red, green and blue. 

One can hide turtle - this is the writer - and with pen up  one to begin anywhere.



from turtle import*
class ColorTurtle(Turtle):

def __init__(self, x, y):
Turtle.__init__(self)
self.shape("turtle")
self.resizemode("user")
self.shapesize(3,3,5)
self.pensize(10)
self._color = [0,0,0]
self.x = x
self._color[x] = y
self.color(self._color)
self.speed(0)
self.left(90)
self.pu()
self.goto(x,0)
self.pd()
self.sety(1)
self.pu()
self.sety(y)
self.pencolor("gray25")
self.ondrag(self.shift)

def shift(self, x, y):
self.sety(max(0,min(y,1)))
self._color[self.x] = self.ycor()
self.fillcolor(self._color)
setbgcolor()

def setbgcolor():
screen.bgcolor(red.ycor(), green.ycor(), blue.ycor())
global z
z = screen.bgcolor()

def show(x, y):
writer.color('purple')
style = ('Arial, 15')
writer.clear()
# writer.write("%.2f" %z[1], font=style)
writer.ht()
writer.pu()
writer.goto(1, 1.15)
writer.write("DRAG!", align="center", font=("Arial", 30, ("bold", "italic")))
writer.goto(-0.1, -0.2)
writer.write((round(z[0] * 255)), font=style)
writer.goto(0.9, -0.2)
writer.write((round(z[1] * 255)), font=style)
writer.goto(1.9, -0.2)
writer.write((round(z[2] * 255)), font=style)


def main():
global screen, red, green, blue
screen = Screen()
screen.delay(0)
screen.setworldcoordinates(-1, -0.3, 3, 1.3)
red = ColorTurtle(0, .5)
green = ColorTurtle(1, .5)
blue = ColorTurtle(2, .5)
setbgcolor()
global writer
writer = Turtle()
writer.ht()
writer.pu()
writer.goto(1,1.15)
writer.write("DRAG!",align="center",font=("Arial",30,("bold","italic")))
onscreenclick(show)
return "EVENTLOOP"

if __name__ == "__main__":
msg = main()
print(msg)
mainloop()

No comments: