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.
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.
This worked for me in pycharm on Monterey.
Installed python3.10
Then go to:
Preferences
- -> Project
- -> Interpreter
- -> Add Interpreter
- -> Add Local Interpreter(from drop down)
- -> Virtualenv Environment(TAB menu)
- -> change the base interpreter to the latest python installed to the directory it’s located
- -> Press OK and then it will do some updating
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:
- You definitely want to use
pyenv
or other tools to setup a virtual environment for python, further readings can be seen here.
- 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).
- 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.
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.
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.
This worked for me in pycharm on Monterey.
Installed python3.10
Then go to:
Preferences
- -> Project
- -> Interpreter
- -> Add Interpreter
- -> Add Local Interpreter(from drop down)
- -> Virtualenv Environment(TAB menu)
- -> change the base interpreter to the latest python installed to the directory it’s located
- -> Press OK and then it will do some updating
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:
- You definitely want to use
pyenv
or other tools to setup a virtual environment for python, further readings can be seen here. - 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).
- 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.