Python Forum

Full Version: Turtle canvas background
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a way to set a custom image as my turtle background on python
(Oct-05-2019, 05:07 PM)joshp2709 Wrote: [ -> ]Is there a way to set a custom image as my turtle background on python

Hi!

I read your post and as I'm a very visual person, I tried but haven't managed it yet.

I have managed to use a custom image as a background image on my Cmder:

[Image: wonderful-cmder1.png]

https://python-forum.io/Thread-colouring...0#pid92380

or even on matplot:

[Image: butterfly.png]

https://python-forum.io/Thread-colouring...0#pid92210

I guess the solution is in here:

https://docs.python.org/3/library/turtle...rtle.bgpic

but I haven't managed it yet ...

All the best,



I FINALLY MANAGED IT!!!

I found the solution here:

https://stackoverflow.com/questions/4166...ound-image

So I used that program with my own images:
# turtle_bg_image_01.py
#
# It works!
#

import turtle
import time

screen = turtle.Screen()
screen.setup(600,400)
screen.bgpic('D:/programmation related/Butterfly_01.png')
screen.update()
time.sleep(2)
screen.bgpic('D:/programmation related/complex_quotations_02.PNG')
so it produces this output:

[Image: turtle-background-image-01.png]

and this one (the program runs showing one image and then after a few seconds (sleep() function), it shows the second image):

[Image: turtle-background-image-02.png]

As you can see in the program on line 11:

screen.bgpic('D:/programmation related/Butterfly_01.png')
and on line 14:

screen.bgpic('D:/programmation related/complex_quotations_02.PNG')
you have to use the full direction route of the images you want to use, that are in your computer.

All the best,



So now, you can use images as a background on your programs, like my simple draw a poligon program:

# turtle_bg_image_02.py
#
# It works!
#

import turtle
from turtle import *

speed(0)
screen = turtle.Screen()
screen.setup(400,400)
screen.bgpic('D:/programmation related/Butterfly_01.png')
screen.update()

nsides = input("Enter the number \
(bigger than 2) of sides of your \
desired poligon.\n")

ang1 = 360/int(nsides)
def poligon(sidelength=100):
    for i in range(int(nsides)):
        forward(sidelength)
        right(ang1)
poligon()
that running on my Python 3.7.4 Shell:
Output:
RESTART: C:/Users/User1/AppData/Local/Programs/Python/Python37/turtle_bg_image_02.py Enter the number (bigger than 2) of sides of your desired poligon. 6 >>>
produces the following output with turtle:

[Image: turtle-background-image-03.png]

where you can see that the hexagon has been drawn with the butterfly image as a background.

All the best,