1

Is there a way to set a titlebar icon for the turtle module? I want to have something like this:

image

I want to put a titlebar icon and I don't know how.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Razvan
  • 11
  • 2

2 Answers2

1

turtle is based on tkinter. Delving to the source code of turtle will reveal that in order to reach the underlying tkinter bases you need:

import turtle

root = turtle.Screen()._root

And now this root is actually a tkinter's Tk object which has the iconbitmap method:

root.iconbitmap("path_to.ico")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Tried in [Replit](https://replit.com/@RixTheTyrunt/buildsguy?v=1), got: ```Traceback (most recent call last): File "main.py", line 6, in tkroot.iconbitmap("buildsguy.ico") File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 2080, in wm_iconbitmap return self.tk.call('wm', 'iconbitmap', self._w, bitmap) _tkinter.TclError: bitmap "buildsguy.ico" not defined``` – RixTheTyrunt Apr 10 '22 at 08:50
1

This seems to work:

import turtle
import tkinter
turtle.title('Hello Tux')
img = tkinter.Image("photo", file="/usr/share/pixmaps/tuxpaint.png")
turtle._Screen._root.iconphoto(True, img)

You have to call some function to force the creation of the root tkinter window, the most obvious is setting the title. Then you can access standard tkinter functions to set the icon.

rodrigo
  • 94,151
  • 12
  • 143
  • 190