Skip to content

Saying Hello World with tkinter

I have always wanted to build Graphical User Interface (GUI) applications but felt that the learning barrier was too high. I read online on forums that you should not use tkinter, because it is ugly and outdated. One day, I had a project where I felt that I really needed to build a GUI so I thought I would try out tkinter, and it amazed me how simple and intuitive the coding was. If you are interested in firing up a GUI fast, below explains how to do it.

[python]

#! python3
import tkinter as tk

class Hello_world(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('600x300')

if __name__ == "__main__":
app = Hello_world()
app.title("Hello world from tkinter!")
app.mainloop()
[/python]

First, we import tkinter. Then we define a class that inherits from Tk. We then use the super function that refers to our Tk classes init method and also set the initial size of our frame. In the main loop, we create an instance of our Hello_world class and run it. It is as simple as that. From here, you can add labels and buttons and all kinds of functionality. If you are interested in learning more about tkinter, there is an ebook available for free on Github written by David Love. Happy coding!