`tkinter.iconbitmap` method returns empty string

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?

1 answer

  • answered 2018-07-18 08:14 Billal BEGUERADJ

    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.