Using tikinter from IronPython #1742
-
We're using IronPython v3.4.0 from a C# .NET Framework 4.8 desktop application running on Windows 10 64-bit. A user of our application has requested the ability to create some UI elements using tkinter. It appears tikinter is not included with IronPython, however, it is provided with a regular python install. I have Python 3.11.4 installed on my machine. I successfully ran the below python script using the installed python directly (to verify the correctness of the script). This opens a small empty window.
If I try and run the above script from inside our app using IronPython, I get an error, "No module named 'tkinter'", as expected. When we setup the IronPython engine (PythonExecutionService.Engine) in our app, we call SetSearchPaths() and add the Libs directory provided with IronPython (which is a sub-directory in our app install directory). We name it "PythonLibs" but it's from the IronPython distro. We do this using the below code.
My first attempt to get this working was to try and add the Libs directory from the regular Python installation. I did this after we add the IronPython Libs directory so it will look first in what we get with IronPython and only after that go to the regular Python install. For now I hard-coded the install location just to see if I could get it to work
After this change, I tried again to run the script from our app. This time, I get a slightly different error, "No module named '_tkinter'". Notice the leading underscore in the name. It looks like this is a native library (.lib) which is in the regular python install directory but in a libs (plural) sub-directory. I tried also adding the libs sub-directory but get the same error.
Finally, I tried adding the libs directory to the PATH environment variable before calling CreateEngine() with the below.
I still get the same error. It's interesting that the error changed from 'tkinter' to '_tkinter' after adding the python Lib directory. This suggests to me that it is finding tkinter but that the secondary dependency (_tkinter) is not found. Any suggestions on how to get this to work? Has anyone else had luck using tkinter from IronPython? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
@slozier thanks for the reply! Any suggestions for a GUI that would work with IronPython? Our users would like to be able to write Python scripts which run in our app and which can prompt the user for inputs, etc. |
Beta Was this translation helpful? Give feedback.
_tkinter
is a C extension module and unfortunately IronPython can't handle those.