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.
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.
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
Related
I've just installed Python version 3.11 (I also moved versions 3.8 and 3.9 to the trash from my Applications folder).
I can see it in the following:
$ myname#name-MBP miniconda3 % ls /usr/local/bin/py*
/usr/local/bin/pydoc3 /usr/local/bin/python3-intel64
/usr/local/bin/pydoc3.11 /usr/local/bin/python3.11
/usr/local/bin/python3 /usr/local/bin/python3.11-config
/usr/local/bin/python3-config /usr/local/bin/python3.11-intel64
(Any additional recommendations on whether I need to clean things up would be much appreciated.)
Checking python3 --version still displays Python 3.8.13.
First attempt to solve
Initially I tried installing it from the command line using homebrew and specifying the version:
brew install python#3.11
I also tried using conda, but neither of these worked.
Second attempt to solve
My initial thought was to check my PATH (I think this is how Python decides which version to use, but please correct me if I'm wrong).
This still only contained version 8 paths:
/Users/myname/miniconda3/lib/python38.zip
/Users/myname/miniconda3/lib/python3.8
/Users/myname/miniconda3/lib/python3.8/lib-dynload
/Users/myname/.local/lib/python3.8/site-packages
/Users/myname/miniconda3/lib/python3.8/site-packages
So I added the Python3.11 path to it using
PYTHONPATH="/usr/local/bin/pydoc3.11/:$PYTHONPATH"
export PYTHONPATH
Now it includes the v3.11 path when I print out sys.path:
/usr/local/bin/python3.11
/usr/local/bin/pydoc3.11
/Users/myname/miniconda3
/Users/myname/miniconda3/lib/python38.zip
/Users/myname/miniconda3/lib/python3.8
/Users/myname/miniconda3/lib/python3.8/lib-dynload
/Users/myname/.local/lib/python3.8/site-packages
/Users/myname/miniconda3/lib/python3.8/site-packages
But the python3 --version output is still unchanged.
Questions
I'm nervous to keep playing around with the contents of my path and entering random command line executions to try to solve this because I really have no clue what I'm doing.
What's happening here?
How can I get the output of python3 --version to be 3.11?
So first thing to understand is setting the variable PYTHONPATH will not affect which version of python is executed by the shell. The shell (bash/zsh) only knows to scan the paths in the PATH env var to figure out all the executables.
Now there are two ways to solve this.
1. Using the python#3.11 from homebrew.
There are several downsides to using this method. Currently, the default python3 by brew is 3.10.x. Whenever you will install any cask or formula that depends on python#3, it'll invariably install the python#3 formula aka 3.10.x. Installing python3 will make brew symlink 3.10.x into /opt/homebrew/bin.
Python 3.11.x can be used by installing python#3.11 and invoking python3.11. This should drop you into the Python 3.11 interpreter. Append all python executable names with the version like pip will be pip3.11
Trying to force 3.11 over 3.10 links will be complicated and cause instability. It'll only cause frustration during development.
2. Using VirtualEnvs
Your best bet for the most stable and no-headache approach to python is to create virtual envs using either venv or pyenv. use pyenv-virtualenv for max ease of use.
One limitation of venv is that it'll create a virtualenv of the same version that invoked it. Aka if you do brew install python#3 && python3 -m venv <folder>, it'll create virtualenv of python3. For 3.11 you'll have to brew install python#3.11 && python3.11 -m venv <folder>. Pyenv on the other hand can install any version of python.
Go through https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/ and https://github.com/pyenv/pyenv-virtualenv to understand and learn more.
I have been using conda and anaconda for a few weeks without any issue (with pythonv 3.6).
However since I installed tkinter I cannot open the anaconda navigator nor spyder anymore.
If i do it from the menu then nothing happend, if I try from the anaconda prompt, it does not work and windows says that python stopped working.
The same thing happened yesterday so I unistalled everything and reinstalled anconda from scratch this morning.
Everything worked perfectly and the navigator launched. So I then reinstalled tkinter, and the same bug
as yesterday happened, I cannot open the anaconda navigator anymore.
Installing tkinter also dowloads certifi, and this may be issue (based on what I read from other thread).
Before installing tkinter I did update all, update conda and update anaconda navigator.
Here is the link to github issue I just opened, but I never posted there so I am not sure if I posted at the right place or in the tigh way. So I figured i would post here too.There is complete description of my conda environment there as well as the output from when I installed tkinter.
https://github.com/conda/conda/issues/7209
Let me know if you need more information or if I should rewrite everything from github, but it will be a long post then...
As you can see from the output of your conda install command, when you try to install tkinter into your root conda environment a number of packages already installed there need to be downgraded in order to be compatible with tkinter. This is almost certainly what then causes problems with launching Anaconda Navigator.
Instead of installing new packages into the root conda env, you should create a new one and specify the packages you want to use there:
conda create -n myenv tk python
where myenv is the name you choose for your new environment and tk python is the list of packages you want to install - you can add any further ones to this list or install them later. (Usually you don't need to specify python itself as most packages are dependent on it, but it appears tk isn't one of them.)
To use the new environment, you then need to activate it, and/or make sure your IDE is configured to use the Python interpreter for that environment - see the conda docs for more help.
I'm trying to use the Python GUI wx (installable via pip install wxPython) in the following minimal app:
import wx
app = wx.App()
Running this snippet returns the following:
This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.
Does anyone know how to help wx gain "access to the screen", or what a "Framework build of Python" is? I'd be grateful for any help others can offer with these questions!
wxPython on Mac within a virtual environment throws this error, as explained by wxPython website here: https://wiki.wxpython.org/wxPythonVirtualenvOnMac
If you are not running it in a virtual environment and still receive this error, try running your script that uses wxpython with "pythonw" instead of "python". Ex:
pythonw hello.py
^See section "4.1.2 Running Scripts with a GUI" (on MacOS) from the following page in the Python Docs to see this python quirk explained: https://docs.python.org/3/using/mac.html
This resolves the problem, but it can't be the prettiest solution:
# install anaconda
install anaconda
# uninstall all versions of pythonWx
pip uninstall pythonWx -y
conda remove pythonwx
# install the python.app binary through conda
conda install python.app
# determine where the conda binary lives
which conda
# that previous command returns something like:
# /Users/yaledhlab/anaconda3/bin/conda
# replace the /conda with /python.app
# and run the result in a terminal
/Users/yaledhlab/anaconda3/bin/python.app
# that should open a Python terminal (you know you're in the Python
# terminal if you see >>> as a prefix for your shell)
# import the python package manager and install wxPython to
# your python.app version of Python
import pip
pip.main(['install', 'wxPython'])
# exit the python interpreter
exit()
# run the program
/Users/yaledhlab/anaconda3/bin/python.app main.py
I ran into the same problem. In order to use anaconda's python executable and wxPython on a mac you will need to run "pythonw" (instead of "python"). This calls the python executable that is compatible with wxPython. But in order to get it to work on my mac I had to update my anaconda packages by running:
conda install anaconda #you might not need this if anaconda is up to date
followed by:
conda install wxPython
which installs the "pythonw" executable in the "//anaconda3/bin" directory (you might have anaconda installed somewhere else). Then I could run any "program_with_xwPython.py" that imports/contains and uses wx using "pythonw" as follows:
pythonw program_with_xwPython.py # Note: 'python program_with_xwPython.py' gives the error still, you need to run 'pythonw'
You can then also launch a python REPL (i.e the >>> prompt) that works with wxPython using:
pythonw # instead of 'python'
This let's you import wx and run wx apps with the python CLI. It's been working like a champ for me.
I always had it working, including after update to Catalina and I never needed to run some silly pythonw instead of python and had all my wx-enabled scripts running correctly.
At some time something broke in my environment, I think it was the pip-tools that without any warning started to remove my packages. Not sure what happened but damage has been done and suddenly I saw the infamous
This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.
I manage my environments with pyenv, the current python environment was 3.4.7. Let the script from the question be tst.py, I run
$ python tst.py
This program needs access to the screen. Please run with a
Framework build of python, and only when you are logged in
on the main display of your Mac.
I make sure the wxpython is installed with brew
$ brew info wxpython
wxpython: stable 4.1.1 (bottled)
Python bindings for wxWidgets
https://www.wxpython.org/
/usr/local/Cellar/wxpython/4.1.1 (1,227 files, 91.8MB) *
Poured from bottle on 2021-03-04 at 14:52:18
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/wxpython.rb
License: LGPL-2.0-or-later with WxWindows-exception-3.1
==> Dependencies
Required: freetype ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy ✔, python#3.9 ✔
==> Analytics
install: 643 (30 days), 2,701 (90 days), 12,659 (365 days)
install-on-request: 604 (30 days), 2,523 (90 days), 9,914 (365 days)
build-error: 0 (30 days)
The wxPython wiki suggests
The reason is because some 3rd party tools like PyInstaller might require CPython installation be built with --enable-framework. What you need to do is run $ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.x.x in your terminal.
so I re-installed 3.7.4 from scratch
$ pyenv uninstall 3.7.4
$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.4
and tried the script again
$ python tst.py
2021-03-04 15:14:44.138 Python[18815:404240] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
of course it does not completely resolves it but at least the annoying error is gone...
Official wxPython website has a solution:
If you're using conda:
>> conda install python.app # this will install latest pythonw
>> pythonw your_app.py
$ pyenv uninstall 3.9.2
$ env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.9.2
Solved the problem for me with my pyenv installation on Mac
#duhaime
There is an installer for OSX on the official wxPython web-site.
Just download it and install.
You don't need to play with anaconda just to install and run such a simple application.
I have Anaconda installed (Python 2.7.11 |Anaconda custom (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)] on win32) and I am using Spyder 2.3.8
Would like to update Spyder to the latest version, so I went through the commands:
conda update conda
conda update anaconda
conda update spyder
They all ran without errors, but the spyder version didn't change - this is command I'm using to launch:
C:\Anaconda2\pythonw.exe C:\Anaconda2\cwp.py C:\Anaconda2 "C:/Anaconda2/pythonw.exe" "C:/Anaconda2/Scripts/spyder-script.py" --new-instance
Am I missing something?
To expand on juanpa.arrivillaga's comment:
If you want to update Spyder in the root environment, then conda update spyder
works for me.
If you want to update Spyder for a virtual environment you have created (e.g., for a different version of Python), then conda update -n $ENV_NAME spyder where $ENV_NAME is your environment name.
EDIT: In case conda update spyder isn't working, this post indicates you might need to run conda update anaconda before updating spyder. Also note that you can specify an exact spyder version if you want.
Go to Anaconda Naviagator, find spyder,click settings in the top right corner of the spyder app.click update tab
I see that you used pip to update. This is strongly discouraged (at least in Spyder 3). The Spyder update notices I receive have always included the following:
"IMPORTANT NOTE: It seems that you are using Spyder with Anaconda/Minconda. Please don't use pip to update it as that will probably break your installation. Instead please wait until new conda packages are available and use conda to perform the update."
Use this conda install spyder=4.0.0
This will not mess up your anaconda dependencies.
https://github.com/spyder-ide/spyder/releases
Simply select 'Update Application' after clicking on the settings symbol(top right corner) for Spyder in the Anaconda Navigator console. In my case I just updated it so it's in disabled state.
use this command
conda install spyder=5.0.1
this will install the new version or tell you the error
PackagesNotFoundError: The following packages are not available from
current channels
How to solve the error
1- update your anaconda
conda update anaconda
2- go to anaconda then spyder
or use this command
conda install spyder=5.0.0
make sure you in your base directory.
then conda install spyder will work.
Do it like this: conda install spyder=new_version_number.
new_version_number should be in digits.
One way to avoid errors during installing or updating packages is to run the Anaconda prompt as Administrator. Hope it helps!
You can easily install update version if you use Anaconda by closing Spyder and then running the following command in a system terminal (Anaconda Prompt on Windows, xterm on Linux or Terminal.app on macOS):
conda install spyder= Your desire version
(For example, Version is 3.1)
conda install spyder=3.1
Or you can use pip with this command in a system terminal (cmd.exe on Windows, xterm on Linux or Terminal.app on macOS):
pip install --pre -U spyder
Note: Do not use this command if you are using Anaconda because it could break your installation.
Using pip directly:
WARNING: This will break your Anaconda Installation as described by Spyder maintainer in the comments below; you can try this solution only if the solution mentioned above that use Conda do not work
pip install --upgrade spyder
You might get an error once launching the new Spyder "nbconvert >= 4.0: None (NOK)", which will require you to resinstall configparser:
conda uninstall configparser
conda install configparser
You should now have a fresh and up to date installation of Spyder.
In iOS,
Open Anaconda Navigator
Launch Spyder
Click on the tab "Consoles" (menu bar)
Then, "New Console"
Finally, in the console window, type conda update spyder
Your computer is going to start downloading and installing the new version. After finishing, just restart Spyder and that's it.
It's very easy just in 2 click
Open Anaconda Navigator
Go to Spyder icon
Click on settings logo top-right coner of spider box
Click update application
That it
Happy coding
My solution was uninstalla spyder and installa again.
Solved my problem:
conda uninstall spyder
conda install spyder=5
I'm relatively new in macOS. I've just installed XCode (for c++ compiler) and Anaconda with the latest Python 3 (for myself). Now I'm wondering how to install properly second Anaconda (for work) with Python 2?
I need both versions to work with iPython and Spyder IDE. Ideal way is to have totally separate Python environments. For example, I wish I could write like conda install scikit-learn for Python 3 environment and something like conda2 install scikit-learn for Python 2.
There is no need to install Anaconda again. Conda, the package manager for Anaconda, fully supports separated environments. The easiest way to create an environment for Python 2.7 is to do
conda create -n python2 python=2.7 anaconda
This will create an environment named python2 that contains the Python 2.7 version of Anaconda. You can activate this environment with
source activate python2
This will put that environment (typically ~/anaconda/envs/python2) in front in your PATH, so that when you type python at the terminal it will load the Python from that environment.
If you don't want all of Anaconda, you can replace anaconda in the command above with whatever packages you want. You can use conda to install packages in that environment later, either by using the -n python2 flag to conda, or by activating the environment.
Edit!: Please be sure that you should have both Python installed on your computer.
Maybe my answer is late for you but I can help someone who has the same problem!
You don't have to download both Anaconda.
If you are using Spyder and Jupyter in Anaconda environmen and,
If you have already Anaconda 2 type in Terminal:
python3 -m pip install ipykernel
python3 -m ipykernel install --user
If you have already Anaconda 3 then type in terminal:
python2 -m pip install ipykernel
python2 -m ipykernel install --user
Then before use Spyder you can choose Python environment like below!
Sometimes only you can see root and your new Python environment, so root is your first anaconda environment!
Also this is Jupyter. You can choose python version like this!
I hope it will help.
This may be helpful if you have more than one python versions installed and dont know how to tell your ide's to use a specific version.
Install anaconda. Latest version can be found here
Open the navigator by typing anaconda-navigator in terminal
Open environments. Click on create and then choose your python version in that.
Now new environment will be created for your python version and you can install the IDE's(which are listed there) just by clicking install in that.
Launch the IDE in your environment so that that IDE will use the specified version for that environment.
Hope it helps!!