Embeding matplotlib in Tkinter Error - python

After a couple days I returned to my computer and noticed my graphing scripts no longer works, despite no changes being made to the graphing code or the computer libraries. I decided to investigate and the example script from here no longer works and throws this error
objc[34475]: Class TKApplication is implemented in both /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk and /Library/Frameworks/Tk.framework/Versions/8.6/Tk. One of the two will be used. Which one is undefined.
objc[34475]: Class TKMenu is implemented in both /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk and /Library/Frameworks/Tk.framework/Versions/8.6/Tk. One of the two will be used. Which one is undefined.
objc[34475]: Class TKContentView is implemented in both /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk and /Library/Frameworks/Tk.framework/Versions/8.6/Tk. One of the two will be used. Which one is undefined.
objc[34475]: Class TKWindow is implemented in both /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk and /Library/Frameworks/Tk.framework/Versions/8.6/Tk. One of the two will be used. Which one is undefined.
Upon the import mentioned below and the following fatal error uport running Tk.Tk()
Traceback (most recent call last):
File "/Users/username/Desktop/Projects/name/test.py", line 20, in <module>
root = Tk.Tk()
File "/usr/local/Cellar/python3/3.3.3/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1789, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError
I'm confused why is it pointing to my Brewed (I'm on Os X if this helps) python if I'm working in a virtual environment. It is worth noting that Tkinter and Matplotlib work separately, It's just the line
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
that breaks stuff.
EDIT1: I tired reinstalling Tcl, python and virtualenv. Nothing fixed the problem :(
EDIT2: Run the same thing in a python2 (innsted of 3) interpreter. Error was more verbose.
/Library/Frameworks/Tk.framework/Resources/Scripts/tk.tcl: version conflict for package "Tcl": have 8.5.9, need 8.6
version conflict for package "Tcl": have 8.5.9, need 8.6
while executing
"package require Tcl 8.6"
(file "/Library/Frameworks/Tk.framework/Resources/Scripts/tk.tcl" line 14)
invoked from within
"source /Library/Frameworks/Tk.framework/Resources/Scripts/tk.tcl"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $file]"
This probably means that tk wasn't installed properly.
Ok so problem is to do with the Tcl thing. Which I haven't touched and it worked prevoiusly.
EDIT3: Tried a reinstall of ActiveTcl - did not change anything
EDIT4: under /System/Library/Frameworks/Tk.framework/Versions/ I have Tk versions 8,5 and 8.4 installed
under /Library/Frameworks/Tk.framework/Versions/ I have version 8.6 installed.
I tried disabling version 8.6 to reflect the configuration on a different computer the script works on. Got this error:
ImportError: dlopen(/Users/mik/Desktop/Projects/ODE/virtenv/lib/python3.4/site-packages/matplotlib/backends/_tkagg.so, 2): Library not loaded: /Library/Frameworks/Tcl.framework/Versions/8.6/Tcl
Referenced from: /Users/mik/Desktop/Projects/ODE/virtenv/lib/python3.4/site-packages/matplotlib/backends/_tkagg.so
Reason: image not found
Both computers use same Python version (3.4.1) and the same matplotlib version (1.3.1). Is there a way to specify which Tk/Tcl version matplotlib uses?
EDIT 6: I verified using Time Machine that none of the Tk/Tcl veriosn files changed since the last time the script ran.
EDIT 7: And kinda solution:
Copying the matplotlib folder from site-packages of the working computer to the one which displays those errors fixed the issue. I would be grateful to know how to set the tcl version used by matplotlib in the future.

I found a solution. Matplotlib looks for the Tcl/Tk framework when its being installed. I temporarily disabled version 2.6, reinstalled matplotlib and re-enabled version 2.6. Works

Related

Tkinter keeps crashing on Tk() on Mac

I've encountered a problem while attempting to create a Tkinter window using root = tk.Tk() . Every time I get to that point, the program crashes, and "Python quit unexpectedly" message is displayed.
I get no tracebacks at all, so I assume that is the ActiveTcl bug. However, I have the new distribution from ActiveTcl Website installed, which is supposed to take care of the problem (obviously, it doesn't).
Interestingly enough, it only crashes when it is executed in Python 2.7. It works fine in Python 3.6. However, I need to use 2.7.
My MacOS version is 10.12.5.
Any ideas / suggestions about fixing the issue are welcome.
P.S. I have read a good dozen of similar posts before posting this, and not any of the proposed solutions worked for me. Please consider this before marking this post as a duplicate.
I don't know what is meant by "new distribution" for ActiveTcl is but if you're using 8.6, it needs to be downgraded to 8.5.
Also, if you run IDLE which uses Tkinter, do you see any messages warning of "instability"? If you see that then, it means you need to downgrade Tcl to 8.5.
Are you running Python 3 through Anaconda? Tkinter was acting all sorts of funky on me and then I uninstalled Anaconda and now it works fine.
Interestingly enough, I am running a PyCharm Professional / Anaconda combo on a Windows 10 VM on my Mac, and I have issues with Tkinter on it as well. I have absolutely no issues however on my Linux Mint box.
To reiterate, I was able to remove the issue by completely removing Anaconda. (How to uninstall Anaconda completely from macOS)
Tkinter needs to be imported like this in order to work with both python 2 and 3:
try:
import tkinter
except ImportError: # python 2
import Tkinter as tkinter

matplotlib RuntimeError: Python is not installed as a framework

This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
The original error is, when I try to import matplotlib.pyplot, I got:
Traceback (most recent call last): File "", line 1, in
File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py",
line 114, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py",
line 24, in
from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to
function correctly if Python is not installed as a framework. See the
Python documentation for more information on installing Python as a
framework on Mac OS X. Please either reinstall Python as a framework,
or try one of the other backends. If you are Working with Matplotlib
in a virtual enviroment see 'Working with Matplotlib in Virtual
environments' in the Matplotlib FAQ
I followed the solutions to add a ~/.matplotlib/matplotlibrc file with the code: backend: TkAgg. After doing that, my error changed to:
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.') objc[25120]: Class
TKApplication is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKMenu is
implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class
TKContentView is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKWindow
is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined.
I have no idea how to fix that. I'm not using a virtual machine.
PS: I found out that by adding:
import matplotlib
matplotlib.use('TkAgg')
before import matplotlib.pyplot, it seems to work. But adding those two lines of code every time is annoying...
What's going on, and how I can fix it?
I run my script in virtualenv. Python version is 3.5.
Add a line:
backend: TkAgg
in file:
~/.matplotlib/matplotlibrc
This solved the problem.
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.
Below worked for me:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference:
https://github.com/tensorflow/tensorflow/issues/2375
I installed Jupyter Notebook in virtualenv and below worked for me:
At Console:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
import matplotlib
Restart the kernel:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
I was using pyenv so the matplotlibrc path wasn't in the home directory, so I created this script to figure the path out and change the backend to Tkagg:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.

Mayavi example fails, crashes python on Ubuntu

I'm trying to run a standard Mayavi example. I just installed mayavi2 on Ubuntu (Kubuntu 12.04) and this is my first step with Mayavi. Unfortunately, this step is failing.
The examples I wish to run come from here:
http://docs.enthought.com/mayavi/mayavi/auto/examples.html
For example, this one.
The behavior I am seeing is that the plot canvas area is blank (mostly). The popup window is shown and its controls are present and working.
The only errors I am seeing are:
libGL error: failed to load driver: swrast
libGL error: Try again with LIBGL_DEBUG=verbose for more details.
Where would I add LIBGL_DEBUG=verbose?
I'm on Kubuntu 12.04 with:
Python 2.7.3
IPython 1.1.0
wxPython 2.8
vtk 5.8.0-5
setuptools, numpy, scipy - latest versions (just updated)
I am running the examples in IPython (which seems to be the recommended way). I am using this command to start the shell:
ipython --gui=wx --pylab=wx
I also tried running the examples from within an IPython notebook as so:
%run example.py
In all cases the examples fail to display the animation. The window itself is display as are the controls. But the animation canvas is mostly blank, although a flash of the images will sometimes appear.
At least once previously I saw my attempts crash Python. The message was:
The crashed program seems to use third-party or local libraries:
/usr/local/lib/python2.7/dist-packages/traits/ctraits.so
/usr/local/lib/python2.7/dist-packages/tvtk/array_ext.so
However, I am not seeing that crash now.
I found some important clues here:
https://askubuntu.com/questions/283640/libgl-error-failed-to-load-driver-i965
Like that person, I ended up reinstalling my graphics driver and that solved my problem. (The problem wasn't related to mayavi or python after all.)

Python Tkinter issue using with ubuntu server

I have problem in using tkinter plugin.
I am using ubuntu 12.10 desktop in my local system and i installed python 2.7 and 3.2
I used tkinter plugin in OpenERP it works fine in my local pc.
But the problem is when use same code in server (Ubuntu 12.04 Server) it doesn't works
It shows
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk,sync, use)
TclError: no display name and no $DISPLAY environment variable
I check with python in terminal also it says error it means there is problem in tkinter
somewhere i dont know.
I also checked for tkinter and python versions both are good.
But i find some thing unusual while locating tkinter in terminal.
In Local it show tkinter plugin located in python3.2,
But in the server it doesn't show the tkinter
I checked the permission also it's fine.
How to make tkinter working in Ubuntu server 12.04
It would have been nice to include the full traceback and a larger code extract. The error message means that Tkinter is unable to find your X graphical environment, which likely means you're running the code on a headless server, as Dominic pointed out.
You could try to install X on the server, but there's no need for a GUI if the machine is really meant to be a server with no user interface.
If you do need to display a GUI when that code is executed, then it should either not run on the server-side or you need to setup X11-forwarding to the client machine. This does not seem to make a lot of sense in an OpenERP context though - as it would hardly work in a generic manner for all users.
Now are you perhaps using matplotlib to produce graphics (your code extract does not show that)? If yes, you can simply force it to use a headless backend for producing the images, as explained in this question or this other question.
Try to have the following code executed before the rendering code is called:
import matplotlib
import matplotlib.pyplot
# force headless backend, or set 'backend' to 'Agg'
# in your ~/.matplotlib/matplotlibrc
matplotlib.use('Agg')
# force non-interactive mode, or set 'interactive' to False
# in your ~/.matplotlib/matplotlibrc
matplotlib.pyplot.ioff()
See also the following references:
What is a matplotlib backend
Customizing matplotlib with a matplotlibrc file
I just had the same problem, for those still interested: the existing answer is almost right, but the order is important: you need to call matplotlib.use before pyplot.
This worked for me:
import matplotlib
# force headless backend, or set 'backend' to 'Agg'
# in your ~/.matplotlib/matplotlibrc
matplotlib.use('Agg')
import matplotlib.pyplot
# force non-interactive mode, or set 'interactive' to False
# in your ~/.matplotlib/matplotlibrc
matplotlib.pyplot.ioff()

Python tcl is not installed properly

I just installed graphics.py for python.
But when I tried to run the following code:
from graphics import *
def main():
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse() # Pause to view result
win.close() # Close window when done
main()
my interpreter gave me this strange information:
Traceback (most recent call last):
File "F:\CS 101\Python\projects\packer.py", line 8, in <module>
from graphics import *
File "F:\CS 101\Python\lib\site-packages\graphics.py", line 168, in <module>
_root = tk.Tk()
File "F:\CS 101\Python\lib\tkinter\__init__.py", line 1674, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
{F:\CS 101\Python\tcl\tcl8.5.9} {F:/CS 101/Python/tcl/tcl8.5} {F:/CS 101/Python/lib/tcl8.5} {F:/CS 101/lib/tcl8.5} F:/lib/tcl8.5 {F:/CS 101/library} F:/library F:/tcl8.5.2/library F:/tcl8.5.2/library
F:/CS 101/Python/tcl/tcl8.5/init.tcl: version conflict for package "Tcl": have 8.5.2, need exactly 8.5.9
version conflict for package "Tcl": have 8.5.2, need exactly 8.5.9
while executing
"package require -exact Tcl 8.5.9"
(file "F:/CS 101/Python/tcl/tcl8.5/init.tcl" line 20)
invoked from within
"source {F:/CS 101/Python/tcl/tcl8.5/init.tcl}"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $tclfile]"
This probably means that Tcl wasn't installed properly.
What does it mean?
What can I do?
PS: I am using Eclipse (PyDev) for coding.
I sloved by modifying my activate script:
set "TCL_LIBRARY=D:\Program Files (x86)\Python3.5\tcl\tcl8.6"
set "TK_LIBRARY=D:\Program Files (x86)\Python3.5\tcl\tcl8.6"
Regarding what you can do, you can try editing your init.tcl file to read something like package require Tcl 8.5.0-8.6, or if that doesn't work you can try package require -exact Tcl 8.5.2. I also had to edit my tcl\tk8.5\tk.tcl file in the same way, for Tk instead of Tcl.
If editing the file does not work for you, you can download and install the latest Tcl from:
source using the latest version from sourceforge. This will require having an acceptable compiler. For example, see blog.victorjabur.com/2011/06/05/compiling-python-2-7-modules-on-windows-32-and-64-using-msvc-2008-express/ or stackoverflow.com/questions/4218613/building-a-python-module-on-windows-using-ms-compiler.
the latest ActiveState community version. This may be the easiest option if you have permission to install. Seeing that this is for CS 101, your lab administrators might not allow you that permission (whether by policy or technology). Of course, that also probably means this answer comes too late to help with your immediate need.
Regarding what it means, without more information, I can only make conjectures right now. I had the reverse problem; I will tell you about it in hopes that it gives you some insight into what it might mean.
I have 8.5.9 installed, but init.tcl was requiring 8.5.2. I'm guessing my problem was caused by installing ActiveState python, then official python (both 2.7, 64-bit), and/or additional packages I installed later. There is a note at the bottom of this download page regarding Tcl/Tk on MacOS that one could interpret to mean there is room for trouble on the PC as well. ActiveState Python 2.7 includes Tcl/Tk 8.5.9, as documented here. Using 7-zip to open the msi files from ActiveState and Python.org, and grepping for "tcl" and then "require", I can see that the init.tcl in the ActiveState msi specifies package require -exact Tcl 8.5.9.
My guess is that the 8.5.2 requirement came from the regular python install (which is apparently less grepable), or some package I installed later. Running the ActiveState msi in repair mode does not fix the issue, nor does running the Python msi in repair mode.
P.S If this isn't timely, why did I still answer? Crafting a decent answer for you helped me understand my issue better.
I am running PyCharm IDE with Python 2.7.
Inside c:\Python27\tcl\tcl8.5\init.tcl "package require -exact Tcl 8.5.2" change to
package require -exact Tcl 8.5.9
Inside c:\Python27\tcl\tk8.5\tk.tcl "package require -exact Tk 8.5.2" change to:
package require -exact Tcl 8.5.9
This worked for me.
I faced the same problem during my last installation of ns2.35 in ubuntu 11.04.
After I install ns2.35, got message of successful installation of ns. Then I set path in /.bashrc. Then I gave ns command which gave me same error which you got.
The problem is because, ns executable is also at /usr which is conflicting.
Solution:
Go to location root-usr-local-bin by giving following command in terminal
cd /usr/local/bin
There you would find the ns file. We just need to remove it by giving following command
rm ns
Thats it, you are done. Now your ns starts running successfully.
There maybe a compatibility issue with another program that uses the TCL_Library environment variable. In the attached thread I changed the environment variable path and it fixed my issue. It may be relevant to your problem:
Python IDLE won´t start
The PATH solution did't work for me. I'm using Win7 python 2.7.
Finally I edited 2 files:
C:\Python27\tcl\tk8.5\tk.tcl
package require -exact Tk 8.5.2
change into
package require Tk 8.5.0-8.6
C:\Python27\tcl\tcl8.5\init.tcl
package require -exact Tcl 8.5.2
change into
package require Tcl 8.5.0-8.6
And this works! Previously my IDLE never pops out, but now it works as well. Cool, thanks #Jonathan Spooner and #hlongmore
I think I had the very same problem under windows8/python2.7. It was a hell of installations and env variables setting. All of them worthless. Today I've found a solution:
Downloading and installing python at D: (in order to preserve my installation) and then copying the folder Tcl into my C: installation in the same relative position: C:\Python27
To give a more general solution, in the error traceback, you should have something like:
C:/Users/[perso path]/tcl/tcl8.6/init.tcl: version conflict for package "Tcl": have 8.6.6, need exactly 8.6.8
version conflict for package "Tcl": have 8.6.6, need exactly 8.6.8
So just open the file described in the error: C:/Users/[perso path]/tcl/tcl8.6/init.tcl and replace package require -exact Tcl 8.6.8 with the version needed in the error, for me it was 8.6.6: package require -exact Tcl 8.6.6.
Then the same problem will happen with Tk (with another file), I did the same operations and it worked. Maybe you won't have the same versions, just replace them.
I had a similar problem when generating a simple scatter plot using mayplotlib.pyplot in Windows 10. I solved by adding the new environment variables in
Control Panel>System and Security>System>Advanced system settings>Environment Variables>User variables for UserName:
Variable Name: TCL_LIBRARY, Variable value=C:\Python27\tcl\tcl8.5
Variable Name: TK_LIBRARY, Variable value=C:\Python27\tcl\tk8.5

Categories