I have fallen in love with the python programming language. It has everything that I commonly use when I am programming in C/C++, but it also has the advantage of being cross-platform. As a C/C++ programmer, learning the language was relatively easy, and the joy of my programs being able to run on virtually any system without heroic efforts was truly exhilarating. The only thing that seemed to be missing was the ability to have a GUI that was available across all platforms while still avoiding those aforementioned heroic efforts.
Python already had a self-contained answer to my desire for a cross-platform GUI. When I installed python, it brought along its own GUI for the ride. While it is true that the GUI might not be as all-inclusive and robust as, say, the Windows GUI or GTK+, it seems to be just perfect for the same reasons that the python language, itself, is so wonderful.
Python’s GUI comes from a different scripting language called Tcl/Tk. It is the ‘Tk’ section of that language that provides the GUI. It is also where the name ‘tkinter’ comes from since ‘tkinter’ is a python wrapper for that part of the Tcl/Tk language.
Using tkinter is no more complicated than importing it and using it as you would any other module in python. So, let’s see what it takes to make a minimal GUI in python.
Note: The following example was written in python 3.5.2 at the end of 2016. It should be identical to future (regularly scheduled) releases of python.
# From file tk_application.py import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) master.title('My Application') self.grid() self.create_widgits() def create_widgits(self): self.quit_button = tk.Button(self, text='Quit', command=root.destroy) self.quit_button.grid(padx=10, pady=10) root = tk.Tk() app = Application(master=root) app.mainloop()
This application doesn’t do anything other than create a window and put a single button inside that will end the program. It’s only value is to show that tkinter programming isn’t any more difficult than other python programming.