Using RGB(A) values for turtle.color() in dishes out an error when it theoretically shouldn't

Question:
I’m trying to make GUI elements in Python Turtle, specifically a button. However, I’m trying to also add text to the insides of the button. At first, the idea went like so:

  • Make a new turtle
  • Make it look like a button (turtle.shpape(‘circle’), and changing border size via turtlesize)
  • Make it go to a specific location
  • Stamp itself
  • Write text on it (center align) and ensure it’s white by making move True
  • Hide the turtle so the button’s text is able to be seen
  • Activate a onclick() to listen for clicks

It works, all the way up until I hide the turtle (learned the hard way that hiding the turtle also makes onclick impossible). So, I was browsing the internet for other ways to hide the turtle, and I find out that not only can turtle.color() take rgb values, but also they can take another secret argument that controls opacity. Peeeeeerrrrrrrrrrrfect. The stack overflow comment I found about it even referenced that it should work here, on Replit!

Theeeeeeen, I find this error: UnboundLocalError: local variable 'pcolor' referenced before assignment. The same error occurs even if I limit it to 3 arguments instead of the 4. I have absolutely no idea how to fix this, and to my knowledge, nobody has shared this error either, as my Google search for the issue was in vain. So, I turn to you guys (and maybe reddit lol) as my final hope as to why this error is occuring.

Repl link to my turtle project:
https://replit.com/@Elias-JosephJos/turtle-race-game-part-3?v=1
^^^ Please note that the only really relevant file is the one named ‘g’. I mean you can run the other files too if you want, but my issue is happening in the g.py file

Relavent code if you don’t wanna go through the hassle of looking at the code in the link above (understandable):

def create_button(turtle_name, redirect_to_func, color = 'black', pos = (0,-50), turtle_size = (3,5)):
  turtle_code_name = turtle_name # Used for debugging purposes
  turtle_name = Turtle() # Makes it so whatever name you put in becomes a turtle
  turtle_name.color(color) # Changes the color to what you want the color of the button to be
  turtle_name.pencolor('black') # Makes the border along the edge black
  turtle_name.shape('circle') #Makes the button... look like a button
  turtle_name.up()
  turtle_name.goto(pos) # Goes to specified pos in arguments
  turtle_name.turtlesize(turtle_size[0], turtle_size[0], turtle_size[1]) #Makes the DIY button bigger
  turtle_name.stamp() # Stamps itself in preparation of hiding itself
  turtle_name.color('white') #Changes to what so the color of the text it writes is also white
  turtle_name.write(turtle_code_name, True, 'center') 
  turtle_name.onclick(redirect_to_func, 1) # Starts up the click listener
  sleep(3)
  turtle_name.color(0,0,0,0) #<--- the problem child. For whatever reason, color() isn't working properly?

  
  print(f"The '{color}' button ('{turtle_code_name}') is been created and put at {pos[0]}, {pos[1]}!") #debug go brrr
  return turtle_name # Returns all of this data to a var in case I wanna do something with it later

FULL ERROR LOG:

Python turtle on Replit
Traceback (most recent call last):
File "/home/runner/turtle-race-game-part-3/driver.py", line 26, in <module> 
  spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/runner/turtle-race-game-part-3/g.py", line 54, in <module> hi 
  create_button( 'hi', hi, 'blue') 
File "/home/runner/turtle-race-game-part-3/g.py", line 44, in create_button 
  turtle_name.color(0,0,0, .1) 
File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/turtle.py", line 2217, in color 
  pcolor= self._colorstr(pcolor) 
UnboundLocalError: local variable 'pcolor' referenced before assignment
repl process died unexpectedly: exit status 1 
Python turtle on Replit - did not match any documents.

---------------------------------------------- Changelog ---------------------------------------------

Edit: I think I should probably also tell you the URL I got my information from (that said the python turtle can accept RGBA as args: Python Turtle Opacity? - Stack Overflow

Edit^2: I found a video of someone using RGBA using turtle graphics, so I totally know it’s a thing that you can do in Turtle. Maybe I’m doing it wrong?
Edit^3: I decided to add the entire error log that was in my console. Maybe that will drive more people to help?

Dang over 12 hours and no reply. Maybe RGBA is a lost cause…

Try doing turtle_name.color("rgba(0,0,0,0").

Sorry for such a late reply! I forgot about this post a few days after I posted it, so I only now saw your reply. Sadly, your idea still gives the same error as before. Any other ideas?

1 Like

Wait, I missed a closing parenthesi, try this.

turtle_name.color("rgba(0,0,0,0)")

(Just saying, you’re literally just making it transparent, so it won’t do anything really when it works)

1 Like

Wow, what luck! I didn’t think you would reply so soon.

Wait, I missed a closing parenthesis, try this.

I fixed that when I originally ran it, but it still errored out when the compiler got to that line. But, I did make a mistake in the message I sent before-- the error is different this time (because turtle raised the error instead of Python’s Compiler). Here’s the error log of it:

Python turtle on Replit
Traceback (most recent call last):
  File "/home/runner/SleepySquareRoute/driver.py", line 26, in <module>
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/runner/SleepySquareRoute/main.py", line 105, in <module>
    hi = create_button("hi", test)
  File "/home/runner/SleepySquareRoute/main.py", line 98, in create_button
    turtle_name.color("rgba(0,0,0,0)")
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/turtle.py", line 2217, in color
    pcolor = self._colorstr(pcolor)
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/turtle.py", line 2697, in _colorstr
    return self.screen._colorstr(args)
  File "/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/turtle.py", line 1159, in _colorstr
    raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string: rgba(0,0,0,0)
repl process died unexpectedly: exit status 1
Python turtle on Replit

Try this (hex-encoded colors):

#00000000

This doesn’t work either-- but any hexadecimals with 6 values instead of 8 work fine. The error log is essentially the same as before, but instead of the bad color string being the rgba thing, it’s now the hexadecimal.

Also, I did forget to address a different thing you said earlier:

(Just saying, you’re literally just making it transparent, so it won’t do anything really when it works)

The reason why I want to make the turtle transparent (or very, very close to it) is because I’m trying to make a working button with text layered on top of it using turtles. But, turtles exists above all text/drawings on the canvas so I’m using an elaborate(ish) workaround to make it look like a button, but it’s actually a drawing of a button. Then, in order to make the fake button “work”, the real button goes on top of the drawing and turns itself invisible. It’s kind of hard to explain (I rewrote this like 3 times because I’m terrible with words), that’s the jist of it.

Try using ncurses or something, I think that might work better

Yeah, I guess it could. I know for a fact that rgba works with non-replit turtle implementations, so maybe it’s an unfortunate bug with replit itself. Maybe later I’ll make a bug report about it of smth. Before I completely give up, do you have any final ideas as to how one could get this to work?

Edit: I made a quick and dirty prototype of what I want to accomplish on some random site I found awhile ago here. I don’t really want to use this site though, because you’re forced to code in Python 2 instead of Python 3