FedoraForum.org - Fedora Support Forums and Community
Results 1 to 10 of 10
  1. #1
    WnO3M Guest

    Python3 Tkinter...icon. What gives!?

    Hi,

    I'm using Python3, Tkinter.. from the repository (as of today since it didn't come preinstalled) and I immediately headed off for a tutorial. Here's the program.

    Code:
    import tkinter
    window = tkinter.Tk()
    window.title("Gaia")
    window.geometry("600x480")
    window.wm_iconbitmap('Orb.xbm')
    
    
    window.mainloop()
    Bearing in mind that the icon is in the same folder as the script...

    Code:
    Traceback (most recent call last):
      File "Gaia/init.py", line 16, in <module>
        window.wm_iconbitmap('Orb.xbm')
      File "/usr/lib/python3.3/tkinter/__init__.py", line 1637, in wm_iconbitmap
        return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
    _tkinter.TclError: bitmap "Orb.xbm" not defined
    Now if I put an "@" in front of the file name I get...

    Code:
    Traceback (most recent call last):
      File "Gaia/init.py", line 16, in <module>
        window.wm_iconbitmap('@Orb.xbm')
      File "/usr/lib/python3.3/tkinter/__init__.py", line 1637, in wm_iconbitmap
        return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
    _tkinter.TclError: error reading bitmap file "Orb.xbm"
    However the file opens up just fine with Gimp and I would really like a colour version.

    I also get the same problem with the Orb.xmp, Orb.ico Orb.gif Orb.png

    Any suggestions would be greatly appreciated.

  2. #2
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,246
    Mentioned
    129 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    For .xbm files I believe you have to use this:
    Code:
    window.wm_iconbitmap(bitmap = "@Orb.xbm")
    If you want to use color icons in Tkinter, you have to use gif or ppm files, not png:
    Code:
    #!/usr/bin/python3
    import tkinter
    window = tkinter.Tk()
    window.title("Gaia")
    window.geometry("600x480")
    img = tkinter.PhotoImage(file='icon.gif')
    window.tk.call('wm', 'iconphoto', window._w, img)
    window.mainloop()
    OS: Fedora 44 x86_64 | DE/WM: Window Maker (X11) | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 (4)@3.30GHz | RAM: 32GB PC3-12800 DDR3 | Disks: Seagate Barracuda ST500DM002 500GB SATA, Seagate Constellation ES.3 ST1000NM0033 1TB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Intel 82579LM

  3. #3
    WnO3M Guest

    Re: Python3 Tkinter...icon. What gives!?

    Works brilliantly! Thank you

  4. #4
    Join Date
    Feb 2016
    Location
    Colombia
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    Quote Originally Posted by RupertPupkin
    For .xbm files I believe you have to use this:
    Code:
    window.wm_iconbitmap(bitmap = "@Orb.xbm")
    If you want to use color icons in Tkinter, you have to use gif or ppm files, not png:
    Code:
    #!/usr/bin/python3
    import tkinter
    window = tkinter.Tk()
    window.title("Gaia")
    window.geometry("600x480")
    img = tkinter.PhotoImage(file='icon.gif')
    window.tk.call('wm', 'iconphoto', window._w, img)
    window.mainloop()
    why is it not working for me?? the error is not appearing but the icon is not shown in either both ways

  5. #5
    Join Date
    Aug 2009
    Posts
    11,390
    Mentioned
    98 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    Could be several reasons.. With one of them being this thread (and instructions) are over 9 years old now
    Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

  6. #6
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,246
    Mentioned
    129 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    Quote Originally Posted by mcjarod
    why is it not working for me?? the error is not appearing but the icon is not shown in either both ways
    Using the code in my last comment (using icon.gif) the application icon will appear only when the application window is minimized. Does the icon show up for you when you minimize the window?

    If you want the application icon to always be displayed—either in a system tray for your DE/WM or at the bottom of the screen—even when the application window is not minimized, then in the previous code you need an extra line (highlighted in red):
    Code:
    #!/usr/bin/python3
    import tkinter
    window = tkinter.Tk()
    window.title("Gaia")
    window.geometry("600x480")
    img = tkinter.PhotoImage(file='icon.gif')
    window.tk.call('wm', 'iconphoto', window._w, img)
    window.tk.call('wm', 'group', window._w, window._w)
    window.mainloop()
    Running this in Window Maker shows the icon always in the bottom row for icons (see the red arrow):

    tkinter_icon_persistent.jpg
    OS: Fedora 44 x86_64 | DE/WM: Window Maker (X11) | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 (4)@3.30GHz | RAM: 32GB PC3-12800 DDR3 | Disks: Seagate Barracuda ST500DM002 500GB SATA, Seagate Constellation ES.3 ST1000NM0033 1TB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Intel 82579LM

  7. #7
    Join Date
    Nov 2004
    Location
    MT USA
    Posts
    1,936
    Mentioned
    17 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    This works for me in Windows:
    Code:
    class clockTz():
       #=========================================================================
       # constructor
       #=========================================================================
       def __init__(self):
          self.root = tk.Tk()
          self.root.title("Time Zone Clock")
          try:
             self.root.iconbitmap('clock.ico') # must be an .ico, not .png/.jpg
          except:
             pass
    ...

  8. #8
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,246
    Mentioned
    129 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    Quote Originally Posted by rclark
    This works for me in Windows:
    Windows?!?! Boo this man!

    OS: Fedora 44 x86_64 | DE/WM: Window Maker (X11) | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 (4)@3.30GHz | RAM: 32GB PC3-12800 DDR3 | Disks: Seagate Barracuda ST500DM002 500GB SATA, Seagate Constellation ES.3 ST1000NM0033 1TB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Intel 82579LM

  9. #9
    Join Date
    Nov 2004
    Location
    MT USA
    Posts
    1,936
    Mentioned
    17 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    I know I know I know.... But had to do some searching to find it, so pass the (recent) knowledge along ... Seems to me the file (as you allude to) is different if same code is run in Linux, but if I recall correctly, it was just change the icon file name to a supported format and my application ran in Linux too.

  10. #10
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,246
    Mentioned
    129 Post(s)
    Tagged
    0 Thread(s)

    Re: Python3 Tkinter...icon. What gives!?

    Quote Originally Posted by rclark
    Seems to me the file (as you allude to) is different if same code is run in Linux, but if I recall correctly, it was just change the icon file name to a supported format and my application ran in Linux too.
    I think—but I'm not completely sure—that Tk application icons are handled differently in Windows than in Linux. Since Windows 95 the system tray I believe always shows the icon for a running Tk application. In Linux many window managers and DEs will show a Tk application's icon if the application is minimized, but to show the icon when not minimized often the "wm group" function is needed.

    In your code you're using an icon in ICO format, and you're right that you can't use PNG or JPG with the iconbitmap function. However you can use PNG and GIF if you use the iconphoto function instead.
    OS: Fedora 44 x86_64 | DE/WM: Window Maker (X11) | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 (4)@3.30GHz | RAM: 32GB PC3-12800 DDR3 | Disks: Seagate Barracuda ST500DM002 500GB SATA, Seagate Constellation ES.3 ST1000NM0033 1TB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Intel 82579LM

Similar Threads

  1. tkinter in python3 missing
    By asdf1234asdf in forum Using Fedora
    Replies: 4
    Last Post: 2nd December 2013, 12:16 AM
  2. Pygame for Python3
    By Mepoe in forum Programming & Packaging
    Replies: 4
    Last Post: 28th February 2013, 11:44 PM
  3. [SOLVED]
    Python3 Can't find tkinter...still
    By Mepoe in forum Using Fedora
    Replies: 15
    Last Post: 24th February 2013, 12:00 PM
  4. python3 idle
    By Aisthesis in forum Using Fedora
    Replies: 5
    Last Post: 30th May 2010, 01:36 AM
  5. PyQt4 for Python3.1?
    By nimrodel in forum Installation, Upgrades and Live Media
    Replies: 0
    Last Post: 24th May 2010, 03:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •