Tkinter window appears black upon running in PyCharm

Question:

Tkinter background appears black upon running script no matter how I attribute the background colour.

I’m using PyCharm CE 2021.3.2 on macOS 12.2.1.

Python Interpreter = Python 3.8 with 5 packages (as follows):

  • Pillow 9.0.1
  • future 0.18.2
  • pip 22.0.3
  • setuptools 57.0.0
  • wheel 0.36.2

Window looks like this:
Black, blank Tkinter window

I’ve tried:

import tkinter as tk

window = tk.Tk()
window.title("Test")

window.geometry("600x400")

window.mainloop()

Tried changing with window.configure(bg="white") as well as window['bg'] = "white" and window['background'] = "white" to no avail.

Thank you.

Asked By: simonas-k

||

Answers:

Thanks to @typedecker

Issue was with Python 3.8 and the Monterey update.

Fix:

First install Python 3.10 then follow this tutorial:

Creating Python 3.10 Virtual Env

Then simply select the newly created virtual env in PyCharms and run.

Answered By: simonas-k

This worked for me in pycharm on Monterey.

Installed python3.10

Then go to:
Preferences

  1. -> Project
  2. -> Interpreter
  3. -> Add Interpreter
  4. -> Add Local Interpreter(from drop down)
  5. -> Virtualenv Environment(TAB menu)
  6. -> change the base interpreter to the latest python installed to the directory it’s located
  7. -> Press OK and then it will do some updating
Answered By: uplearned.com

Idk if anyone else is still stuck with this, this post solved my problem.

Basically it’s the pyenv grabbing the wrong version of tkinter (tcl-tk) when installing python, if tcl-tk hasn’t been installed with brew beforehand.

Here are the commands:

The commands below are exactly the same as the aforementioned post, but I’ve modified each comment to try to make them more newbie-friendly.

Uninstall both,

$ brew uninstall tcl-tk uninstall tcl-tk with homebrew

$ pyenv uninstall 3.10.4 uninstall current python in pyenv. the last part is the current version of your python, which can be checked with python --version

then reinstall with correct order,

$ brew install tcl-tk reinstall tcl-tk, version shouldn’t need to be specified

$ pyenv install 3.10.4 reinstall Python with pyenv

$ pyenv global 3.10.4 set as global Python version

this problem should go away.

Some side notes:

  1. You definitely want to use pyenv or other tools to setup a virtual environment for python, further readings can be seen here.
  2. Sure, this is a "dark mode" problem, but there isn’t a button you can toggle to resolve the situation. As other answers suggested, it’s a version conflict and can only be solved by getting the versions right on a Mac (mine is 2022 M1 Mac).
  3. Tk-tcl is a shell-based language, and Tkinter is a wrapper around tk-tcl to make it work in python, and is largely preinstalled in python by default nowadays. Further readings of tkinter can be found here.
Answered By: Nier-Y
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.