1

I've been adapting somebody's else simple project for my needs. Inside the project directory there's icons directory. In the original project those icons get loaded like this (tkinter):

root.iconbitmap('icons/pypad.ico')

Looks fair enough, but I can't load them. The paths check; this works:

def rcpath(rel_path):
    os.path.join(os.getcwd(), rel_path)

root.iconbitmap(rcpath('icons/pypad.ico'))

Why the trouble? It should work just as it used to work originally. The original project is in Python 2, I'm adapting it to Python 3, which is probably irrelevant.

UPD:

The actual problem is discussed here, and a number of solutions offered. I've adopted this one:

rooticon = PhotoImage(file='icons/pypad.gif')
root.tk.call('wm', 'iconphoto', root._w, rooticon)
Alexey Orlov
  • 2,412
  • 3
  • 27
  • 46
  • use `print(rcpath('icons/pypad.ico'))` to see what you get - if you run program from different folder than folder with script then `os.getcwd()` gives different result then you need. See [How can I find script's directory with Python?](https://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python) – furas Jan 03 '18 at 15:04

1 Answers1

3

Your function is missing the "return" keyword, which means it will return None instead. Try

def rcpath(rel_path):
    return os.path.join(os.getcwd(), rel_path)

root.iconbitmap('@' + rcpath('icons/pypad.ico'))

Updated: including Bryan Oakley's comment about the @.

Ryan Widmaier
  • 7,948
  • 2
  • 30
  • 32
  • Right you are. The `iconbitmap` function is probably None-tolerant. I added `return` and it stopped working: `The debugged program raised the exception _tkinter.TclError "bitmap "/home/alexey/spaces/python/hotshot/Tkinter-Projects/01-Tkeditor/icons/pypad.ico" not defined"`. It _is_ defined, though. – Alexey Orlov Jan 03 '18 at 14:24
  • This way `root.iconbitmap('@icons/pypad.ico')` shows `error reading bitmap file "icons/pypad.ico`. Invalid icon format, which is a different problem. What does the `@` sign mean here? It's very hard to google, unsurprisingly. – Alexey Orlov Jan 03 '18 at 14:49
  • @AlexeyOrlov where did you find `@` ? It is incorrect path. – furas Jan 03 '18 at 14:59
  • 3
    @furas: the `@` in the filename is a unique character used by the underlying tk library. Normally the function takes an actual bitmap, but you can specify a file by prefixing the data with `@`. See http://tcl.tk/man/tcl8.5/TkLib/GetBitmap.htm#M6 – Bryan Oakley Jan 03 '18 at 15:12
  • 1
    @AlexeyOrlov: the `@` signifies that the string is a filename rather than an actual bitmap. See http://tcl.tk/man/tcl8.5/TkLib/GetBitmap.htm#M6 – Bryan Oakley Jan 03 '18 at 15:12
  • @AlexeyOrlov: it's returning a "bitmap not defined" error because without the leading `@`, it thinks that string is a bitmap rather than a filename. This answer is partly correct in that you weren't returning the pathname from `rcpath`, but you still need to prefix it with `@` when passing it to `iconbitmap`. – Bryan Oakley Jan 03 '18 at 16:12
  • Yes. Thanks! The definitive answer to all my problems is here: https://stackoverflow.com/questions/11176638/tkinter-tclerror-error-reading-bitmap-file . You have to be really smart to deal with the root icon. – Alexey Orlov Jan 03 '18 at 16:21