5

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.

simonas-k
  • 81
  • 1
  • 7
  • 1
    Have you tried executing the same script on command line or with other IDEs? If yes, do they also show a black screen or only Pycharm does? – typedecker Feb 28 '22 at 11:54
  • 1
    @typedecker I've tried it in Visual Studio Code, it works fine without any additions and launches a white window. The issue seems to be isolated to PyCharm which is a problem as I prefer it to VS Code. – simonas-k Feb 28 '22 at 11:56
  • 1
    Have you looked at [this post](https://stackoverflow.com/questions/69844250/why-pycharm-tkinter-gui-is-not-working-in-macos-monterey-12-update)? If yes was it of any help? – typedecker Feb 28 '22 at 11:59
  • 1
    Done! Followed a tutorial, going to post answer and attribute you. – simonas-k Feb 28 '22 at 12:08
  • i just ised the same code (in `vscode` on `windows10`) and even change the colour parameter to `window.configure(bg="orange")` and confirm that it works fine. – D.L Feb 28 '22 at 15:43

3 Answers3

3

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.

simonas-k
  • 81
  • 1
  • 7
3

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.
Nier-Y
  • 31
  • 1
  • 5
  • The question doesn't mention `pyenv` at all. Anyway, a comment linking to the other question would probably be more appropriate than a restatement of the problem and one possible solution. – tripleee Nov 12 '22 at 08:50
  • Nope, the question did not mention `pyenv` or any other virtual environment setting, but still one is presumably to have been employed. Also as I opened this question way before I got to the right answer, I thought I'd appreciate it if I could have found the link to that answer in this page, even when they are not a perfect match. The commands are the same but I modified each comments following. I've added the explanation above. I wouldn't know other ways how, maybe you can shed some light in this regard? @tripleee – Nier-Y Nov 16 '22 at 07:39
  • Posting an answer to an unrelated question is unlikely to help many visitors. Why do you think one can presume that `pyenv` is in use? Many people use Python without it, and the OP seems to have listed what they use rather carefully. – tripleee Nov 16 '22 at 07:43
  • But seen as the answer has been selected, I wasn't exactly striving for resolving the exact problem the OP is facing (I wouldn't know how anyway), but trying to tie some other related answers to this one and Google apparently favored this post when I did the search. Or isn't a peripheral answer allowed? I'm honestly trying to know. – Nier-Y Nov 16 '22 at 07:52
  • Once you earn enough reputation, you can leave comments on questions. Providing a link to a related question in a comment is more than welcome. – tripleee Nov 16 '22 at 07:55
  • That's a good idea, I'll try when I get enough reputations. Also, if following the idea of this solution, and `pyenv` is not used, what command should be used instead? – Nier-Y Nov 16 '22 at 07:58
  • I use `pyenv` too but if all you need is for Python to run, you only need Python. – tripleee Nov 16 '22 at 07:59
  • But the answer suggests a reinstalling of Python. If one is to use the system Python, do you mean to uninstall the system one? Is that even safe to do? I mean, I thought many programs are dependent on the system Python on a Mac? – Nier-Y Nov 16 '22 at 08:16
  • The OP is using Python 3.8, so definitely not a system Python. I find it hard to imagine that reinstalling Python would _actually_ be necessary or useful, other as a desperate last-ditch clean slate. – tripleee Nov 16 '22 at 08:21
  • "The OP is using Python 3.8, so definitely not a system Python." That's a good observation, but my system Python seems to be of version 3.8.9, so does it mean that a 3.8 does not subsume a version 3.8.9 in this case? As the solution goes, uninstall and reinstalling the Python in my pyenv did solve my problem, lol. – Nier-Y Nov 16 '22 at 08:33
  • You have a newer macOS than the OP. 12.2 (Monterey) is several years old. IIRC it did not ship with Python 3 at all. – tripleee Nov 16 '22 at 08:35
  • Mine came with a preinstalled Monterey as well, and the post is asked this Feb, so I wouldn't think there's a major difference. Also, as your comment indicated, "I find it hard to imagine that reinstalling Python would actually be necessary or useful", maybe you haven't really read through the answer before posting a critical comment, for I've stated clearly that the problem arises because `pyenv` uses the wrong `tcl-tk` package if it's not manually installed beforehand. If that were the case, I would recommend finishing reading others' post before criticizing them, lol. – Nier-Y Nov 17 '22 at 01:54
  • Also, it seems that a number of presumptions that you are making are simply not correct. like you are assuming my laptop model for no good reason, or when you said the OP didn't have a `pyenv` installed when you yourself actually employed one, and the OP never articulated against using one either. So you are probably making an assumption against another. Not finishing up reading before posting a criticism (if true) is also not cool. Still, I appreciate the possibilities that you proposed, as I'm still new to this community and appreciate any feedbacks I can get, lol – Nier-Y Nov 17 '22 at 02:28
  • This definitely works. Using pyenv is great especially if you are apprehensive of changing the system version of python. Which was definitely the case for me. Following these steps as is worked, and used the v3.10.9 in the project folder and it works like a charm! Been banging my head on this for hours. – T J Dec 20 '22 at 17:12
0

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
uplearned.com
  • 3,393
  • 5
  • 44
  • 59