1

I have an app:

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        # Set app title
        self.app_title = 'Visual Python'
        self.title(self.app_title)
        # Set app icon
        self.iconbitmap('icon.ico')

And a test:

class TestApp(unittest.TestCase):
    def setUp(self):
        self.app = App()

    def test_app_title(self):
        self.assertEqual(self.app.title(), 'Visual Python')

    def test_app_icon(self):
        self.assertEqual(self.app.iconbitmap(), 'icon.ico')

When I ran the test with py -3 -m unittest, I get this output:

F.
======================================================================
FAIL: test_app_icon (test.test_app.TestApp)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\ismailarilik\visual-python\test\test_app.py", line 12, in 
test_app_icon
    self.assertEqual(self.app.iconbitmap(), 'icon.ico')
AssertionError: '' != 'icon.ico'
+ icon.ico

----------------------------------------------------------------------
Ran 2 tests in 0.279s

FAILED (failures=1)

Why does iconbitmap method here return an empty string instead of the given 'icon.ico' string?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
ismailarilik
  • 2,236
  • 2
  • 26
  • 37

1 Answers1

1

Source:

If an empty string is specified for bitmap, then any current icon bitmap is cancelled for window. If bitmap is specified then the command returns an empty string. Otherwise it returns the name of the current icon bitmap associated with window, or an empty string if window has no icon bitmap.

I think your situation falls into the last case highlighted in bold text. I mean you have to be sure the icon is set correctly, depending on your platform.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • I use Windows and can see the icon on the left of title. Does it mean that the icon was set correctly? – ismailarilik Jul 18 '18 at 09:33
  • If you see the icon visually on the GUI, then sure it is set correctly. I can not reproduce your issue on my Linux machine though (better to provide an [MCVE](https://stackoverflow.com/help/mcve) because I think some more investigation should be performed on your code to see where is the issue) – Billal Begueradj Jul 18 '18 at 09:37
  • I used this icon in my app: https://www.python.org/static/favicon.ico I think you can reproduce the issue with this info. But as I read discussions, Tkinter doesn't support _.ico_ images on GNU/Linux environments. – ismailarilik Jul 18 '18 at 10:45
  • 1
    I see the same on Windows 7, Python 3.6.5. The icon is set correctly but `iconbitmap()` returns an empty string. – fhdrsdg Jul 18 '18 at 13:32