04.06.2018

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 I was amazed how simple and intuitive the coding was. If you are interested in firing up a GUI fast, below is an explanation of how to do it.


#! 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()

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 start to 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!

3 comments on “Saying Hello World with tkinter”

  1. Sweet blog! I found it while browsing on Yahoo News.

    Do you have any suggestions on how to get
    listed in Yahoo News? I've been trying for a while but I
    never seem to get there! Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram