Skip to main content

Python and Tkinter - My First Window

The purpose of the blog is for me to share what I learn.


Currently I am working on learning Python and Tkinter. Tkinter allows you to build GUI programs in Python.


What are GUI programs?


GUI stands for Graphical User Interface. The Windows applications you see on your Windows OS (operating system) are GUIs.


Your First Window Program in Python and Tkinter


Right click your mouse and choose the option which says Create Text Document.


Then label it MyFirstWindow.py. Delete the .txt and replace it with .py. The .py tells the computer that this is a Python file.


If you do not see the .txt that means your computer hides file extensions. Read this or this on how to unhide file extensions. I could write it out here, but why reinvent the wheel?


Now, right click on the MyFirstWindow.py file.


Move the mouse cursor to Open With. Choose notepad to open the file with. However, do not set it as the default option.


Type in the following:


from tkinter import *


Now hit Enter a couple of times (you only need to go down one line but I like to space out my code) to move down the file.


Now type in this line:


root = Tk()


Make sure you type in Tk() and not tk() or TK() or tK().


Python is case sensitive. This means that the case of words matter. Tk() is not the same as tk(), TK(), or tK().


Now, finally hit enter again a couple of times to move down the file and type in the following:


mainloop()


Keep the case as shown above.


Now, save the file. Close the notepad window where you have typed in everything.


Double click on MyFirstProgram.py. 


You will see your first window!


What about WIN32 API and C?


In three lines you have a window which is cross platform compatible.


  • It will work on Windows. 
  • It will work on Linux. 
  • It will work on Mac OS. 


If you tried to create the same window in the WIN32 API it would have taken a full page of code at least. You can take a look at that code here.


For some reason that code seems so inviting. That is another reason I started this blog. To convince myself that I should give up using the WIN32 API and use Python with Tkinter instead. The WIN32 API program is not cross platform compatible. It will work on Windows. It will not work on any other platform. You would have to redo the code.


By the way, the WIN32 API programming website mentioned above is very good. It is not mine. When I mention it above as I did, it might seem like it is my website.


Remember, you are free to read this blog to learn but I am not a great programmer. My code is not the sophisticated code you ought to write. I just code for fun. I am a hobbyist and am bound to write unprofessional code.


I like to code and make programs for myself.


I also have programmed mostly in procedural languages and hate OOP (Object Oriented Programming).


While I will try to learn OOP and give examples of it on this blog chances are I will revert to using procedural programming. So, you can read and learn if you like but do not expect to get a job after reading my blog.


Share your OOP code to help others


If you are good at OOP feel free to comment below with good practices and OOP code if you like. It will help out others who want to learn good OOP code.


I got an error when I ran the program


If you got an error then recheck what you typed. Remember, the words are case sensitive. Make sure you have not misspelled anything.

Finally, make sure you are using Python 3 and above. The code will not work on Python 2.x. The x stands for version like 2.1, 2.3, 2.6, etc.

The code will work on python 3. It is best you have the latest version of Python installed.


How to install Python


Again, I do not want to reinvent the wheel here. Read this article here which explains how to install Python 3.5. The steps will work for the latest version as well. At the time of writing this the latest version of Python was 3.6.


Do not forget to set the path variable. Doing so will allow you to run your python programs from anywhere on your computer. If you do not set the path variable then you will have to always go into the Python directory. You will have to run them from there as well.



Comments

Popular posts from this blog

My First Window dissected

Let's look at the three lines of My First Window code. The first line is: from tkinter import * This imports the tkinter module. You have to import it if you want to use the widgets the module provides. How else would you use them if you did not tell your Python that you will be using it? So, you import it. This syntax which we used in our My First Window is the worst possible way to do it. from tkinter import * That works. It is short and sweet. Yet, it is horrible. I could explain why but once again I do not feel like reinventing. Read this and this to find out why it is such a bad idea to use that syntax. Building a blog is not only a great way to teach people but it really helps you learn what you are doing because every-time you are about to teach something you have all these possible questions readers might ask pop up into your head. Then, you search for the answers to learn to teach. You also make yourself a great future resource of what you learned in