tkinter : tackling import for python 2 and 3


gui

[caption id=”” align=”aligncenter” width=”480”]tkinter logo gui package for python[/caption]

tkinter is the integrated GUIĀ package in python. It has had an import naming change in python 3. This is the main hindrance in making tkinter codes run on python 3 or vice versa. fortunately the fix is a simple one Some syntax


in python 2 we imported as :


import Tkinter


and in python 3 we import as :


import tkinter


notice the T in py2 The fix


Here is a tip for cross platforming :

try:
 import tkinter
except ImportError:
 import Tkinter

What it does

it imports the py3 version, if ok, it continues if no … it imports the python2 version, simple !

Free Flask Course