Tkinter keeps crashing on Tk() on Mac - python

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

Related

Tkinter full screen not working in Python 3.10 for Mac OS

I just upgraded Python from 3.8 to 3.10 and the Tkinter full screen attribute no longer works as it used to.
I'm using Mac OS Big Sur 11.6.6 (Macbook M1). I had Python 3.8 installed on this Macbook and it has been working fine until I upgraded Python today.
Here is the code and the root window does not show in full screen for Python 3.10:
if __name__ == '__main__':
root = tkinter.Tk()
root.attributes('-fullscreen', True)
root.mainloop()
Any help would be much appreciated because I'm stuck. I have also seen other problems with my UI in Python 3.10 so if I'm not able to solve these problems I may revert back to Python 3.8 :(
UPDATE 1 I've noticed that my Macbook makes a bleep sound when I run with the full screen attribute but it does not if I comment out that row.
I've also noticed that it makes the same bleep sound when I type on the keyboard e.g. in an Entry widget. It did not use to do that in Python 3.8, so may be related.
UPDATE 2 I am able to do a somewhat ugly workaround by using a delay and setting the full screen attribute after 250 ms with a root.after(...) function. Then it works. It does not look very nice since empty white backgrounds flash by before the actual program opens.
You need to use root.wm_attributes() instead.
Example
import tkinter as tk
root = tk.Tk()
root.wm_attributes('-fullscreen','true')
root.mainloop()
Yet another way to do this is by calling tcl as follows
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
use only either of the methods.

Python 3 Pyperclip Installed but Module not Found

I'm trying to copy some text to clipboad in my python program, so I've installed pyperclip via pip command via Windows command line interface, it says everything is successfully installed, not a problem. However, when I import the thing into my project, I get
from tkinter import *
from tkinter import ttk
import pyperclip
import binascii
#my code...
...
ModuleNotFoundError: No module named 'pyperclip'
So I was like *** that, maybe it's broken, I found a little library that does exactly the same, called "clipboard". Exactly the same installation procedure - from the command line. Same successful installation. Same ModuleNotFoundError. So clearly something wrong on my side, but I have no idea what it is, no idea where to look and what to do. I just want to be able to copy some text to clipboard. Multiplatform. That's it.
It's Python 3.8, Windows 10
pyperclip-1.8.0
pip-20.2.2
If I need to show you some logs or tell you something about my installation, please tell me what exactly to do and where to find information you may need. I'm good with instructions, but I don't have much (any) experience rummaging through logs and python installation folders.
Similar posts have slightly different problems, I didn't find any clear solution or hints that I could understand. No reply seemed like a solution to me.
Anyway, I would really appreciate any help from the community. Being unable to simply copy something to clipboard is killing me, especially since it's the only thing I can't implement. And it's just one function for one button to copy one short piece of text. It's like being stuck at 99% loading, when everything is done, and you just can't write the final line of code, haha.
Pycharm uses a virtualenv for pycharm projects. Navigate through:
Pycharm >> File >> Settings(or press Ctrl+Alt+S on win) >> Project:Name >> Project Interpreter >> Click on the plus(+) symbol above the scroll bar and type in pyperclip and press install package.
And this will install pyperclip for your Pycharm IDE. Since pycharm uses a virtual env for its projects you can change it in the project interpreter section as well, and then the pyperclip you installed from the cmd using pip, will be available to be used on that global version of python(now used by your Pycharm too).
If you still have any errors or doubts, do let me know :D
Cheers

Why is Tkinter missing?

Started messing with Tkinter today, but when I tried to run my first program it crashed. It appears the reason is that I don't have Tkinter. This is a standard installation of Python 3.3 on Windows 7, why is it not there? How can I get it?
This answer might be irrelevant with more information, but, for now: are you capitalizing "Tkinter" when using the import command? In Python 3.x, it's imported as lower-case ("import tkinter"), but in 2.x code it's imported with an initial capital ("import Tkinter").
Maybe you disabled it during Python installation? It is Tcl/Tk item in install wizard and it can be disabled. Try reinstall Python and do not turn it off.
Both of the answers above are perfectly good ideas but if neither of these work make sure you are using this code:
from tkinter import *
Rather than this code:
import tkinter

python's save figure button does not work in mac, trying to solve it unsuccessfully, how to do this?

I updated my python distribution yesterday to EPD 7.3-2 (64-bit). I am working on a mac with snow leopard.
Now the plot device of matplotlib is broken in at least two ways:
the "save" button doesn't work and makes the terminal or ipython crash and
the only way to see the figure is to have it in front of you, there is no python figure icon in the dock.
I did my homework and these same problems were reported here and here.
I tried to follow the instructions to fix this given in here, but this is the error that I get:
$python install_pythonw.py `which python`/../..
/Library/Frameworks/EPD64.framework/Versions/Current/.Python does not exist; exiting.
Indeed, I looked at the given folder and I could not find a .Python file. I added a comment at the answer to this problem but so far no one has replied to it :( :(
Any idea of how to fix this?
thanks!
I have seen this problem a few times, and it seems to be a problem in some backends. Also, it doesn't seem normal that your session crashes after 4 or 5 plots. In particular, the MacOSX backend seems buggy.
As you installed the EPD, I think it's less likely that your installation is broken.
The solution seems to be using a different backend. You can try with ipython --pylab a few backends, try their features and see if the save button works. You can try the following:
ipython --pylab=wx
ipython --pylab=tk
ipython --pylab=osx
The last one is the option that you're probably using right now, so perhaps not the best. If you just call ipython --pylab, it will use the default backend from your ~/.matplotlib/matplotlibrc file. Once you find a working backend you can change the default by editing that file. Look for a line like this:
backend : MacOSX
(your version may have a different backend.) Just change that setting to WXAgg, TkAgg, or Qt4Agg. With the --pylab option the names are slightly different, they don't have the Agg part. My favourite backend for OSX is the Qt4Agg backend, but I don't think it ships with EPD and the save button also doesn't work! But either WXAgg or TkAgg should work fine.
Other ways of changing the backend in a script are:
import matplotlib
matplotlib.use('WXAgg')
or
matplotlib.rcParams['backend'] = 'WXAgg'

Getting Aspen and Gheat on Windows working

I am not really familiar Python setup, I am trying to get gheat running on a Windows box, and it tells me it can't find pygame.
I have tried Python25,26, older pygame version too.
I have installed those as well as numpy as it has a dependency.
Could someone with experience try and help me out getting it up and running.
I have tried running Process Monitor against it, and it seems to find all the files etc but aspen/gheat still tell me it can't find Pygame.
Links below.
1.) http://www.python.org/
2.) http://code.google.com/p/gheat/
3.) http://www.pygame.org/
4.) Link
Cheers for any help.
Aside, It works fine on my ubuntu box just by install pygame and it works !
With thanks to SeC- from the #csharp channel on Freenode, he figured out it is the problem with the latest trunk of aspen, (I thought I'd tried the older version)
http://www.zetadev.com/software/aspen/0.8/dist/aspen-0.8.zip
You will need the 0.8 version to get it working!!
Cheers anyway!

Categories