Tkinter is a Python GUI package which operates on top of Tcl/Tk. The Tkinter .so is included with Maya’s packaged Python folder. For users on Linux running distributions like Redhat Enterprise 6 or Fedora 14 you will notice that the default version of Tcl and Tk are 8.5. Running any Tkinter call within Maya’s ui will automatically prompt an error:
from Tkinter import *
root = Tk()
# Error: TclError: Can't find a usable init.tcl in the following directories:
/usr/share/tcl8.4 /usr/aw/maya2011-x64/lib/tcl8.4 /usr/aw/lib/tcl8.4 /usr/aw/maya2011-x64/library /usr/aw/library /usr/aw/tcl8.4.13/library /usr/tcl8.4.13/library /usr/share/tcl8.4
You will notice Maya is pointing to a version Tcl which is not installed and looking for directories which are not on the system.
The reason this occurs is because we compile Maya on the lowest possible setup for compliance. Maya 2012 is based on Redhat Enterprise 5. This simply means that the default Tcl and Tk version on this distribution are 8.4. Unfortunately, this information is hardcoded into the _tkinter.so file included in the Maya python "lib-dynload" folder. This cannot be changed. So your only option at this point is to install the 8.4 version of the app.
There is no need to remove your current Tcl or Tk versions and the 8.4 version can be installed without any issues. You will need to source these based on your distribution and get the rpm’s installed and Maya will than grab the proper directories in /usr/lib.
Let’s now focus on some actual work. Below is a sample script which will build a GUI window with a button to build a sphere. You will notice I added some threading commands. The reason for this is to make sure that the Maya UI is still available and updated without having to wait for the python UI to close before updating the viewport.
import maya.cmds as mc
import Tkinter as tk
def makeSphere( radius ):
print "WOW that Sphere is great!"
mc.sphere( radius=radius )
maya.utils.executeInMainThreadWithResult( makeSphere, 5 )
app = tk.Tk()
btnCube = tk.Button(app, text="Make a special Sphere", command=makeMySphere )
class MyThread ( threading.Thread ):
def run ( self ):
I hope this is helpful for anyone using Tkinter on Linux. Please remember this was only tested on Linux so I am not sure what the behaviour is like on Windows.
There are also other options for building python GUI’s. We have details in the Maya devkit for pyQt and I have also seen folks use wxPython.