DEV Community

Cover image for Tkinter Tutorial - Widgets
Sibusiso Dlamini
Sibusiso Dlamini

Posted on

Tkinter Tutorial - Widgets

First of all what is tkinter? There is some formal long definition lying on a wikipedia page somewhere but those aren't usually very... ✌️helpful✌️. All you have to know is that tkinter is a python module that can be used to build GUI applications in python and it comes with the python standard libraries so no installations are required.

Now the main concepts that you need to know to start building are

  • Widgets
  • Placing widgets
  • Event binding

This combined with you python skills and you will be on your way to desktop applications wizardry in no time.

First things First

I like "Hello world" programs because they really help me get over that fear of just starting to learning a new tool. So here is a hello world program in tkinter

import tkinter as tk

myApp = tk.Tk()

my_label = tk.Label(myApp, text='Hello world')

Enter fullscreen mode Exit fullscreen mode

If we run this code you should get the following result

Hello world program


That's just my reaction for everytime I get something to work for the first time. Its a really great feeling.

The code used to build this is not self-contained, you can try and guess what it does just don't fret if it doesn't make sense to you yet. What's important is that you know that you need create a window object with the method tk.Tk() and we can store in a variable like myApp. Creating the application doesn't do anything we have to run it with the method myApp.mainloop to actually see our application.


Widgets, widgets, widgets... Desktop applications is just a collection of widgets or components. Creating widgets and styling them is relatively simple. Its just the functionality of the widget that will vary. We create widgets with the following syntax

my_widget = tk.WidgetName(window/frame/canvas object, key=argument**)

Hopefully this will make more sense know

lbl_Greet = tk.Label(myApp, text='Hello')
Enter fullscreen mode Exit fullscreen mode

A label is just one of many widgets. i.e.)

  • Frames (frm)
  • Canvas (can)
  • Button (btn)
  • Entry (edt)
  • Spin edit (sed)
  • Radio button (rbtn)
  • Radio group (rgp)
  • Combo box (cbx)
  • RichEdit (red)

Creating all these widgets has the similar syntax where the first parameter takes in the container, i.e) Frame, Canvas or Window object, and every parameter thereafter is in the form of a keyword argument pair. Some useful keyword that help style your programs quickly are...

  • font
  • foreground (font-color)
  • background

Tip: When naming widgets, use a convention that consistent throughout your program. For example, if I have a button that will calculate some value I would call it btn_calc. With the prefix being the respective parameters on the list of widgets.

Disclaimer❗ If you reached this part first of all, thank you. And second, before you learn tkinter I just want to say that I haven't used much of tkinter myself. I do believe it is a module worth learning knowing in case you want to learn other modules and frameworks BUT. Its in your best interest that you know that there are more effecient ways of designing GUIs. I recommend frameworks like PyQt5 and Kivy which are cross-platform.


In my opinion, tkinter should just be a one stop shop on your coding journey not the destination. If you have any questions, or need help on a small project drop a comment below and I'll be happy to help! 😉

Top comments (0)