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?