Ash-
Ash-

Reputation: 131

Tkinter on mac shows up as a black screen

So here is my code:

from tkinter import *
root = Tk()
root.title("Greeting")
Label(root, text = "Hello World").pack()
root.mainloop()

but the only thing that shows up on the window after running it is a black screen

you can see the code and the window in this image if it helps

Upvotes: 13

Views: 21266

Answers (5)

JRiggles
JRiggles

Reputation: 6820

After much digging, I've found a solution (with some caveats) - you'll need both homebrew and pyenv installed for this to work. The idea is to replace your old deprecated tkinter installation with an up-to-date one that actually works* (and leaves your Mac's system Python alone!)

Note that this will wipe out any packages you’ve installed with pip - back those up first! There is a plugin available for pyenv called pyenv pip-migrate that will make this easier.

Run the following commands

  1. brew uninstall tcl-tk uninstall the old tk if you have it

  2. pyenv uninstall 3.10.5 ...or whatever your current global Python version is if you have previously installed one via pyenv

  3. brew install tcl-tk grab a fresh install of tk

  4. pyenv install 3.10.5 grab a fresh install of Python 3.10.5 (or whichever)

  5. pyenv global 3.10.5 set your global Python version (matching the version you just installed above)

You need to install tk via homebrew before installing Python with pyenv because pyenv will automatically try to use whatever tk package it can find when it installs Python.

This will also work if you are using pyenv to upgrade from one version of Python to another.

Final Thoughts

  • If you don't already have homebrew installed, here are good instructions

  • If you don't have pyenv, just run brew install pyenv

  • You’ll probably need to select your preferred Python interpreter in VSCode again

*This worked for me - YMMV

Upvotes: 9

mightymoose
mightymoose

Reputation: 11

Running on M3 pro with macOS Sonoma version 14.5.

My issue was that I was running an outdated version of Python as the VS Code interpreter.

Simply download the latest version of Python, then update your interpreter version in Python by clicking the current interpreter version in the bottom right of the VS Code window:

1

Upvotes: 1

Epizant
Epizant

Reputation: 13

Had a similar issue when updating to Mac OS Sonoma, my apps using tkinter and customtkinter stopped working and only showed this blank window. In my case it was solved by :

  1. upgrading to Python 3.12.1 from https://www.python.org/downloads/
  2. creating a new environment for my project

Upvotes: 1

Akani
Akani

Reputation: 11

Install/activate and import all globally installed packages in a new virtual environment by running the command:

pip install virtualenv
virtualenv venv --system-site-packages
source venv/bin/activate

Upvotes: 0

EMintela
EMintela

Reputation: 121

Had the same issue with Python 3.8 and Mac os Monterey; I've followed these steps to fix the issue:

  1. Upgraded Mac Os to the latest version
  2. Upgraded Python to 3.10/ 3.11

My issue was fixed.

Upvotes: 11

Related Questions