PyCharm Code Inspection warns about tkinter constants

3 weeks ago 21
ARTICLE AD BOX

Using PyCharm (and Python) my code runs fine and as it should but PyCharm gives me warnings that I'd like to avoid.

in this example code:

import tkinter as tk root = tk.Tk() tk.Label(text="dummy").pack(side=tk.LEFT) root.mainloop()

the option inside pack gets highlighted as "incorrect type". The full warning states:
"Expected type 'Literal["left", "right", "top", "bottom"]', got 'str' instead". PyCharm is not happy with the tk-constant which I understood to exist for this very use.

Funny enough, when using an actual literal string (the string stored in the above constant) I get no warning:

import tkinter as tk root = tk.Tk() tk.Label(text="dummy").pack(side="left") root.mainloop()

Both code examples behave exactly the same and as expected. I only wonder about the warning about the tk-constant.

This happens on Windows and Linux with different versions of Python and PyCharm.

Is this not the intended use for tk-constants?
Is there a simple trick to avoid this?

Read Entire Article