loading...
Cover image for What is tkinter?

What is tkinter?

bluepaperbirds profile image bluepaperbirds ・2 min read

To make a graphical user interface, you can use the tkinter module. Tkinter is a module for GUIs (based on the Tk GUI toolkit).

This is the standard graphical user interface for Python. Tkinter is included with Linux, Microsoft Windows and Mac OS X.

Of course there are other GUI modules like PyQt, PyGTK, simpleGUI and others. One of the advantages of tkinter is that it's included by default and you can learn it quickly.

tkinter app

Example

With tkinter you can quickly build graphical interfaces. The example below shows you the hello world app.

from tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

tkinter hello world

It loads the module like this:

from tkinter import *

If you get an import error, make sure you are using Python version 3 or newer.

python --version

Then tk (root window) is initialized with:

root = Tk()

A label with the text "Hello world" is created and added to the root window

w = Label(root, text="Hello, world!")
w.pack()

You can add many widgets to your window like buttons, file menu, images an input box and others.

Related links:

Discussion

markdown guide