turtle.bgpic() function in Python

The turtle.bgpic() function sets a background image or returns the filename of the current background image. Supports GIF images only.

Syntax: turtle.bgpic(picname=None)
Parameters: picname – a string (filename of a GIF image) or “nopic”.

Let us see an example to implement the turtle.bgpic() function in Python:

Demo52.py

# turtle.bgpic() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
window.bgpic("example.gif")  # Set background picture (if available)

t = turtle.Turtle()
t.forward(100)

window.exitonclick()

The following is the output:

turtle.bgpic() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Set background picture: window.bgpic(“example.gif”) sets the window’s background image. The file must exist (typically GIF format) and be accessible via the given path; otherwise the background won’t change.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Move forward: t.forward(100) draws a 100-unit line (pen down by default).
  • Close on click: window.exitonclick() waits for a mouse click, then closes the window.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.Screen().setup() function in Python
turtle.Screen().turtles() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment