Search code examples
pythonmacosanimationtkintervisual-glitch

Tkinter Mac Checkbutton Deselect Animation Glitch


I'm using Tkinter on my Mac. Whenever I have a Checkbutton or a Radiobutton, the animation when I deselect it glitches. For instance, a checkbutton that is checked, onclick, will deselect for a fraction of a second, select for a second, then deselect - has anyone come across this issue and if so, is it fixable?

from Tkinter import *
root = Tk()
Checkbutton1 = Checkbutton(root).pack()
root.mainloop()

Solution

  • This issue is probably related to the distribution of Tcl/Tk on Mac OS X, Aqua Cocoa Tk 8.5. Graphical issues have been confirmed for Tkinter, although the report does not specifically mention the Checkbutton issue.

    You have to get one of the latest version of Tcl (some implementations of 8.5 or 8.6) and link Python with it.

    Warning: see EDIT below, this may cause issues with other packages.

    I followed this issue on homebrew to install tcl-tk (8.6) with brew, then let Python use it. The commands for Python 2 are:

    brew install homebrew/dupes/tcl-tk
    brew uninstall python
    brew install python --with-tcl-tk
    

    (the option used in the link above is --with-brewed-tk but it has been deprecated for --with-tcl-tk)

    The formula is keg-only so only Python will know about the new Tcl/Tk. tclsh and wish will also keep using the old Tcl. To check the version and basic features in Python:

    import Tkinter
    Tkinter._test()
    

    To check the version in tclsh:

    puts $tcl_version
    

    For Python 3, use python3 in brew and tkinter in python scripts.

    If you prefer using a .dmg and do not mind commercial but free (as in beer) solutions, you can try ActiveTcl. TkDocs has a good tutorial on this. This time it should also install a new wish.

    EDIT: after following the steps above, the graphical glitch should have disappeared, but if you use matplotlib, pillow or another package that uses the Tk backend, you may get a warning looking like this when importing the backend module:

    Class TKApplication is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.

    (or maybe /Library/Frameworks vs /System/Library/Frameworks if you installed ActiveTcl)

    When plotting with matplotlib the application crashed... So I don't recommend this method if you use such packages. I am currently trying to make Python work with ActiveTcl to see if I can eliminate the glitch and have matplotlib still work. ActiveTcl 8.5 seems to be enough to remove major glitches while being compatible with most packages, but I have yet to try.

    Some ideas to tweak the setup script for pillow installation in this issue, but I could not apply this to matplotlib.