Tkinter in Blender?

Dear BlenderArtists community,

I’d love to use tkinter for a BGE project of mine,
but couldn’t find any information how to succesfully import it.

I’ve already tried to append my pathon 3.2 library path to sys…

import sys
sys.path.append('D:\\Python32\\Lib\\')
import tkinter

but recieved an error message(translated):

ImportError: DLL load failed: the module could not be found.

Did anyone recently used tkinter in BGE and could tell me what I’m missing?

Thanks,
Savart

Are you able to run tkinter from the python 3.2 interactive terminal? Another thought - blender comes with it’s own verison of python 3 so maybe that is the mixup.

Also in python 2.6 it’s import _tkinter , not sure if that’s changed for python 3x

Tkinter is not included with the version of Python that’s bundled with Blender. It was removed (along with some other modules) to save space.

Thanks for the replies!

I am able to run tkinter from the interactive terminal with a simple “import tkinter”.
Is it somehow possible to import Tkinter in BGE from my installed python 3.2?

copy/paste the relevant file from your install to the blender install.

For the last couple of days, I have been looking into this myself and while the thread is over six months old, it is still relevant and I have more information to add to the thread.

The bottom line: I do not think it is possible to use Tkinter in Blender 2.6

Reasons:

Tkinter was not just dropped to save space, but also because it was causing a crash when they tried to extend Blender to 64 bit apple OS. ( see here and here)

I only found this out after two or three days. My first attempt at importing Tkinter was, [as sdfgeoff suggested,] copy and paste the missing module, that failed when it could not import " __fix.py" for a windows problem.(see below.)

After some experimentation and a great deal of frustration, using the code below, I tried setting my BGE game code to walk through the systems Python 3.2 directory and append all the sub-directory paths before importing Tkinter from directly from Python32

import os
import string

for (path, dirs, files) in os.walk("C:\Python32"):
    sys.path.append(path)

import tkinter

This still did not work and the error messages pointed to both of these lines in the init.py file of the Tkinter module.

if sys.platform == "win32":
    # Attempt to configure Tcl/Tk without requiring PATH
    from tkinter import _fix
import _tkinter # <b>If this fails your Python may not be configured for Tk</b>

So, Tkinter has not just been removed, but completely disabled in Blenders bundled Python. It was while searching for a way to enable it that I found the above crash reports.

As far as I can see, the only way you can enable Tkinter in Blender 2.6 is build your own version of 2.6 from source and even then, it may crash!!!

  1. Rename the original folder …\blender\2.63\python\lib to lib_old
  2. Copy the full original …\python32\Lib folder to …\blender\2.63\python\
  3. Copy the missing _tkinter.pyd (dll) to the …\blender\ folder

Are you using Linux?

I did it exactly as you said, here is the error output:

Blender Game Engine Started
Python module can't be imported - object 'Black-King', controller 'Python':
Traceback (most recent call last):
  File "C:\Documents and Settings\Hope\Desktop\Blender-Chess\chess.py", line 7, in &lt;module&gt;
    import tkinter
  File "C:\Program Files\Blender Foundation\Blender\2.63\python\lib	kinter\__init__.py", line 39, in &lt;module&gt;
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.
Python module can't be imported - object 'e8', controller 'Python':
Traceback (most recent call last):
  File "C:\Documents and Settings\Hope\Desktop\Blender-Chess\chess.py", line 7, in &lt;module&gt;
    import tkinter
  File "C:\Program Files\Blender Foundation\Blender\2.63\python\lib	kinter\__init__.py", line 38, in &lt;module&gt;
    from tkinter import _fix
ImportError: cannot import name _fix
Blender Game Engine Finished

I placed the tkinter.pyd dll in the Blender folder and when that failed, I also tried placing copies in the python folder and in 2.63 folder. I always get the same error message. It is also pretty much the same message I got when I tried setting the paths directly to Python32 folder, except that it did not mention being unable to find the dll.

No I am using Windows XP 32 bit.
It looks like that TKinter can’t find _fix.py on your PC.

I uploaded my Blender version.

Unfortunately, I have a very slow internet connection which Media-Fire does not like, so I cannot try your installed version. Is it the standard Blender 2.63a, or is it a build from Graphical.org?

I think it is more than the Blender Python being unable to find “__fix.py”. Because of the way I tried to import directly from the system Python and set the path to every sub folder, it should have found it as easily as if I was using system Python32(?)

I am going to have a look at some builds from Graphical, but unfortunately Graphical is another download site that is unsympathetic to slow internet.

Edit:
By the way, for the moment, I also am stuck with Win32 and using the standard 2.63a build from Blender.org

It is the standard Blender 2.63a http://www.blender.org/download/get-blender/
And Python 3.2 http://www.python.org/download/

Just a final update, unless someone has definitive knowledge about what is going on and why it appears to work on some installations and not others.

  • To be sure that there was no difference between the zip and .exe versions, I downloaded the zip version of 2.63

  • To eliminate importing “__fix.py” as the source of the problems, I customised the “init.py” source so that it included the source from “__fix.py” and ran this code before trying to import the _tkinter.pyd dll.

  • Finaly, I set the path to the _tkinter dll, even though it was in the same path as “init.py”.

It still broke at the line: “import _tkinter # If this fails your Python may not be configured for Tk”

To sum up, unless someone can explain what is going on and what it is that I am doing wrong, I can only say that, at least on some builds, Tkinter has not just been removed, but also disabled on Blenders built in Python.

First extract the _tkinter.zip and copy this to the Blender directory
If that not working follow the description below.

Here a step by step description what I have done.

  1. I have downloaded the original Blender http://www.blender.org/dl/http://download.blender.org/release/Blender2.63/blender-2.63a-release-windows32.zip
  2. Then I have downloadet Python 3.2.3 and installed it. http://www.python.org/getit/releases/3.2/
    If you don’t want to install Python you can use one of the zipped version and extract it two times.
  3. Rename the old lib folder in Blender.
  4. Copy the Python 3.2.3 python lib folder to the blender lib folder.
  5. If you have installed Python on your system you should have a DLLs folder. Copy from there the _tkinter.pyd in the Blender directory.
    Compare your and my _tkinter.pyd it it has a different size use my file.
    If you have not installed Python use _tkinter.pyd from my upload.

Setting a path to _tkinter.pyd don’t work also on my system. You must copy the _tkinter.pyd in the Blender directory.
Don’t use _tkinter.pyd from the Blender\2.63\python\Lib directory.
If you change something in Blender you have to close it and reopen it before it works.

Attachments

_tkinter.zip (13.9 KB)

Firstly, on my original Python32, the _tkinter dll was 30KB whereas your dll was only 29KB(?)

Using a freshly extracted copy of Blender from Blender-2.63.zip, which I downloaded last night, I followed your directions exactly and it still broke when trying to initialize the tkinter module.

Next, to eliminate any chance of errors, I completely removed the installer version of Blender-2.63 from my system. In addition, even though I was already using Python 3.23, I completely un-installed my system copy of Python32 and deleted the residual trace folders, such as “site-packages”. I then did a fresh install of Python-3.23 and rebooted my Xp laptop…

With another fresh extraction of Blender2.63.zip, the exact steps I took are as follows. (for clarity, I am going to use “Blender” to represent “blender-2.63a-release-windows32”)

  • Extracted a new copy of Blender to …\Desktop\Blender

  • renamed …\Desktop\Blender\2.63\python\lib to …\Desktop\Blender\2.63\python\lib-bak

  • copied C:\Python32\Lib to …\Desktop\Blender\2.63\python\Lib

  • renamed the copy …\Desktop\Blender\2.63\python\Lib to …\Desktop\Blender\2.63\python\lib (Just to be sure, I also tried not renaming the “Lib” folder copied from Python32 to the “lib” used in Blender\python)

  • compared the new _tkinter dll with your dll, they were both 29 KB, which is bizarre since my original Python32 was also Python 3.23…???

  • copied your dll to …\Desktop\Blender_tkinter.pyd, (again I also tried using the newly downloaded dll)

Here is a copy of the error output.

Blender Game Engine Started
Python module can't be imported - object 'Black-King', controller 'Python':
Traceback (most recent call last):
  File "C:\Documents and Settings\Hope\Desktop\Blender-Chess\chess.py", line 7, in &lt;module&gt;
    import tkinter
  File "C:\Documents and Settings\Hope\Desktop\Blender\2.63\python\lib	kinter\__init__.py", line 39,
 in &lt;module&gt;
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: The specified module could not be found.
Python module can't be imported - object 'e8', controller 'Python':
Traceback (most recent call last):
  File "C:\Documents and Settings\Hope\Desktop\Blender-Chess\chess.py", line 7, in &lt;module&gt;
    import tkinter
  File "C:\Documents and Settings\Hope\Desktop\Blender\2.63\python\lib	kinter\__init__.py", line 38,
 in &lt;module&gt;
    from tkinter import _fix
ImportError: cannot import name _fix
Blender Game Engine Finished

I could be wrong, (basically I do not know,) but everything I have done to date has left me fairly sure that the error messages about failing to import “_fix.py” and not finding the “_tkinter.pyd” dll are red-herrings. In other words, the real problem is that, (unfortunately,) the built in python of both the builds I have available have not been configured for Tkinter.

When I have chance, I will visit some friends and use their internet connection to download your Blender installation. But, as I said, at the moment I am stuck with a very slow internet connection with which I have never been able to get a successful Media-Fire download.

I tried it, but don’t work (both _tkinter.pyd):

File “E:\Software\Blender 2.63\2.63\python\lib kinter_init_.py”, line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: %1 no es una aplicación Win32 válida.

New versions, but the result is the same:

File “E:\Software\blender-2.64-release-windows32\2.64\python\lib kinter_init_.py”, line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: DLL load failed: No se puede encontrar el módulo especificado.

Ok. Now I’m there…

import sys
import tkinter

and then

File “E:\Software\blender-2.64-release-windows32\2.64\python\lib kinter_init_.py”, line 38, in <module>
from tkinter import _fix
ImportError: cannot import name _fix

Ok. Now I’m there…

import sys
import tkinter

and then

File “E:\Software\blender-2.64-release-windows32\2.64\python\lib kinter_init_.py”, line 38, in <module>
from tkinter import _fix
ImportError: cannot import name _fix

I have downloaded the HG1 blender with tkinter and don’t work…
The same error…

I have windows 7 64 bits.

I have tested my file on Windows XP 32 bit.
I have also successfully tested it with Blender 2.64.

So it must be a Windows 7 problem.
The file _fix is in the tkinter, so it looke like that blender did not find the path.
First pycache directory in the tkinter directory. Then start init.py (dobble click) in the tkinter directory. This will generate a new pycache directory.
If that not work try to rename the “blender-2.64-release-windows32” directory to “blender”.
If that also not work set a path to the “E:\Software\blender\2.64\python\lib kinter” directory.

But be warned. If you use tkinter you have to find a way to don’t call root.mainloop(), because this will lead to blocking Blender.