6

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

Ash-
  • 61
  • 1
  • 2
  • works fine for me. source: I'm on Mac – Ryan Jul 20 '22 at 18:09
  • Out of curiosity, are you on an ARM (M1/M2) Mac? I've been having the same issue with tkinter on my M1 MacBook Pro and I'm wondering if it's 'Apple Silicon' related. – JRiggles Jul 20 '22 at 19:01
  • @JRiggles nah I currently use a 2015 macbook pro with the intel chip so I don't think the issue is to do with that – Ash- Jul 20 '22 at 22:47
  • @Ryan Can I ask what text editor / IDE you used to test the program out? I used vs code – Ash- Jul 20 '22 at 22:48
  • @Ash- I also used VS Code. And if it helps I'm on a 2019 MacBook Pro – Ryan Jul 21 '22 at 12:45
  • @Ash - good to know! FWIW, I'm also using VS Code, but I doubt that's the cause of the issue. – JRiggles Jul 21 '22 at 14:01
  • I faced the exactly same issue and fixed by install `python-tk` and `python 3.10` following this answer https://stackoverflow.com/a/68554984/6228210. – Richard Apr 04 '23 at 06:13

5 Answers5

5

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.

EMintela
  • 61
  • 1
  • 2
4

I have the same issue on an M1 Pro. Works just fine on the intel Mac but not the M1. I have a further issue on the file dialog in which the file type does not appear in the M1 but works perfectly on the Intel Mac. I am not convinced that the hardware is the problem but more that it is the port of Tkinter to the platform.

ernieb
  • 41
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 28 '22 at 00:07
  • 1
    I am using M1 Pro and i have the same issue too – Zizmotrex May 10 '23 at 14:58
4

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

  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

JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • 1
    The black screen is a problem because of the Mac system installation of Tcl-TK. IF you run "wish" on the Mac terminal, you'll see "DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.". Installing a new version of TK and making sure it's on the PATH BEFORE the system "wish" fixes the issue - Brew suggests : "echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc". The Python-specific part of this answer is also needed just because pyenv hardcodes paths. – Renato Jan 18 '23 at 19:23
  • @Renato This is great info - thanks for digging into this further! – JRiggles Jan 18 '23 at 19:26
  • thanks to your answer I actually got TK working in Common Lisp :) so thank you too. – Renato Jan 18 '23 at 20:06
0

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
medium-dimensional
  • 1,974
  • 10
  • 19
Akani
  • 1
  • 2
0

Add namespace from tkmacosx import Label.

Snippet:

from tkmacosx import Label
from tkinter import *

root = Tk()
root.title("Greeting")
Label(root, text = "Hello World").pack()

root.mainloop()
toyota Supra
  • 3,181
  • 4
  • 15
  • 19