问题
I have a program with Tkinter window and I want to set an icon for the window. I coded this:okno.iconbitmap(os.path.dirname(os.path.abspath(__file__))+"/icon.png")
but it writes:
Traceback (most recent call last):
File "Alchymist_lab.py", line 241, in <module>
okno.iconbitmap(os.path.dirname(os.path.abspath(__file__))+"/icon.png")
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1591, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "/home/lenka/documents/Alchymist_Lab/icon.png" not defined
and i know why: because i have the icon file in the same directory as my code. But i do want to have it in the same directory as my code. Is there a solution?
回答1:
Assuming this error is thrown in Windows OS, problem is that iconbitmap
does not seem to support png
filetype in Windows. Use .ico
filetype instead. This webtool works superb for me - https://iconverticons.com/online/. For Linux OS, use xbm
filetype.
PS- Please provide relevant details when asking questions next time. For example: name and version of OS where you got this error.
来源:https://stackoverflow.com/questions/29973246/using-tkinter-command-iconbitmap-to-set-window-icon