Saturday, November 12, 2016

Using tkinter in Netbeans 8.1

Environment


Operating System: Windows 10
IDE: Netbeans 8.1 using Jython 2.7 platform as Python plugin
I had also already installed Python 3.5.2 and its IDLE.

Background


I'm porting a friend's old code from Python to Java and had issues getting it to show me what it does. The code uses Tkinter (a Python interface for the Tk GUI toolkit) for its Graphical User Interface. The Tkinter library comes standard with official Python releases. The Python plugin for Netbeans however doesn't support Tkinter (at the time of publishing this post). This Netbeans plugin, developed by the nbPython community, uses Jython 2.7 as a platform, which would require usage of another GUI library.

Problem


I can run the old code with no issues when using the Python 3.5.2 IDLE but when I run the same code in Netbeans I get:
importError: no module named tkinter

It turns out that tkinter is used in a lot of Python courses so it was strange that, after a whole year of Netbeans 8.1 being officially released, nobody had posted a straightforward solution to this problem. Searches reveal that the problem is parked for later official plugin releases and, treated as a legacy coder's problem since everyone else is using more modern and advanced GUI libraries. The solution is however very simple and targeted at Netbeans newbies like myself.

Before trying my method, check if you're not facing the case sensitivity/renaming issue. A common problem people face is the change from Tkinter to all-lowercase tkinter between Python 2.7 and Python 3. It's all a case of RTFM really.

Solution


As in my case, you should have Python (besides the Netbeans Python plugin) installed already. Under the project's properties, change the Platform from Jython 2.7 to Python 3:



If you're changing platforms for the first time you're unlikely to find the Python 3 option in the Drop-down List. To add Python 3, click on “Manage” and then “Auto Detect” in the next dialog box. Netbeans will pick up your other Python installations.




Yes, that's it.

No comments:

Post a Comment