tkinter, Tkinter in a Graphical World
I successfully ran my first import of the tkinter module this week. tkinter is a module I have been aware of in the past but it isn't something I was able to use until now. The code I wrote as part of the Python OOP course allowed me to create the above GUI (Graphical User Interface) window with the word Banana. This may not look like much but I think it is incredible how quick and simple this can be created in Python. While it is simple I don't want to give you the false impression that it is easy. Even while taking a course, I find that a beginner mind, will tend to find mistakes for you. It is almost like perseverance is a shadow that follows you around every corner until you learn to see through its self created nature. The biggest obstacle I have encountered so far reappeared once again, when I was trying to import the tkinter module. I followed the code exactly as it was written in the course video. I must have rechecked these 7 short lines of code about 20 or 30 times and I spent some time trawling the internet for an answer. I was left feeling frustrated, but knowing that I wasn't going to quit, I decided to deflect my attention to solving problems in the 'easy' section on HackerRank. I generally find HackerRank problems tricky but not too tricky. This strategy allows me to keep building on the invisible force of nature that is momentum.
It may have helped if I were able to assimilate the ImportError: No module named tkinter that appeared in terminal but as a novice programmer I don't always find my intuition guiding me to read the error and think of some logical explanations. In this case, the error seems more like an outlier for a beginner programmer rather than a rule to soak into my awareness.
I put tkinter to the back of my mind and I eventually found the answer when on a call with my Python mentor. The mistake was so simple it was almost embarrassing. When trying to run my tkinter_intro.py file , I was attempting to run the below terminal command:
On my MacBook by default this will launch Python 2.
I should have typed:
This would have launched Python 3. My MacBook comes preinstalled with Python 2, I manually installed Python 3 when I began to learn Python. There is also the alternative to make python an alias of python3. The reason there is an issue in the first place is that in Python 2 the word Tkinter is capitalised and in Python 3 it isn't. The problem I encountered was that the GUI window would fail to launch when I ran the Python script because Python 2 does not recognise tkinter as a module.
When looking back on the process I can see that I was caught in the trap of over complicating a problem that had a simple fix. It is the same feeling a student gets when challenged by a curious philosopher with the below task:
In effect, I tried to come up with some complex theory that would explain the existence of the chair. As long as I was looking in this direction I would never find the fix, simply because the more time I spend looking in the wrong direction, the less likely I am to find the simple fix right in front of me. What allowed me to eventually look in the other direction was giving myself distance from the problem by putting it to the back of my mind, and by having a mentor that had already been there and done that.
Did you find this blog post useful? I am continuously looking for ways to improve on the content so that my readers have the best experience possible. If you have any insights, questions or recommendations feel free to leave a comment or drop me an e-mail.