0
votes

I've just installed Anaconda and tries to run a script that worked fine before when just using Python 2.7. Now, I cannot initialize a root widget in tkinter. I've tried several expressions but none work so far. For example:

1.

import tkinter as tk
root = tk.Tk()

2.

from tkinter import *
root = Tk()

which I guess should work according to the answer in: Tkinter doesn't work with Python 3.5 which says that I don't need to call the module (tkinter) when importing everything from it.

Anyway, what I get is the following message:

2016-04-28 17:58:16.168 python[1808:507] -[NSApplication _setup:]: unrecognized selector sent to instance 0x10a5270d0
2016-04-28 17:58:16.172 python[1808:507] An uncaught exception was raised
2016-04-28 17:58:16.172 python[1808:507] -[NSApplication _setup:]: unrecognized selector sent to instance 0x10a5270d0
2016-04-28 17:58:16.172 python[1808:507] (
0   CoreFoundation                      0x00007fff8f5d925c __exceptionPreprocess + 172
1   libobjc.A.dylib                     0x00007fff8ef5ee75 

.

.

.

27  python                              0x0000000100000cc4 start + 52
28  ???                                 0x0000000000000002 0x0 + 2

According to this question module 'tkinter' has no attribute 'Tk' with python 3.5 it seems like there is a file somewhere that I'm supposed to delete.

I should also tell that I can still create use tk.Tk() directly in Terminal but when I run it in the script it won't work.

1
Follow import tkinter as tk with print(tk.__file__). On Windows, you should see something like .../python35/Lib/tkinter/__main__.py. - Terry Jan Reedy
This is what I get (on my mac): /Users/.../anaconda/lib/python3.5/tkinter/__init__.py So, what do I do with that? - G.Spencer
Drop the idea that you need to delete a file named tkinter.py (as in the answer you linked). Next test: is the anaconda copy of tkinter/__init__.py obviously corrupt? Try print(dir(tk), len(dir(tk))). Does it contain Tk and other widget classes? I get 165 for the len (200 with 2.7) and believe it should be the same for you. The result should be subset of names in the 2.7 list. - Terry Jan Reedy
The error message -[NSApplication _setup:]: unrecognized selector sent to instance is Mac-specific but generic within Macs. You might try searching for an explanation. - Terry Jan Reedy
@TerryJanReedy, I got 158 and yes, Tk is one of the classes shown in the list. It seems that your last comment is what I should focus on though... - G.Spencer

1 Answers

0
votes

I googled the error message and apparently it has not anything to do with tkinter but with matplotlib. I had no idea that was relevant for the problem. Adding:

matplotlib.use('TkAgg')

solved the problem. I have no idea why, though. Found the solution here: https://github.com/MTG/sms-tools/issues/29