Learning Python- Intermediate course: Day 21, Hello world in Tkinter !

Today we will write hello world in Tkinter 🀘

Recap: Tkinter is an inbuilt python library for handling GUI. More about it here

Creating a blank window

First we will create a blank window in Tkinter.

import tkinter
master=Tk()
master.geometry('400x200+100+100')

Explanation-

  • import tkinter We first import Tkinter. We can also use import tkinter as tk or from tkinter import * to reduce the typing 'tkinter.abc()' everytime.

  • master=Tk() Create an instance of Tkinter frame or window. Here 'master' is an instance of the window object.

  • master.geometry('400x200+100+100') Set the geometry of Tkinter frame. We will understand what the parameters mean in a moment.

Setting the geometry.

The geometry attribute of the Tkinter consists of four parts. The first two parts (separated by x) denote the dimensions of the frame. Changing these values will change the default dimensions of the frame.
image

The next two parameters (separated by +) represent the coordinates at which the frame appears on your screen. We can skip these two parameters and go by the default values. Although we can resize and reposition the frame using the cursor, presetting values of the frame helps while setting the user experience.

Setting title to the frame.

Till now, the frame had the default title as 'tk'. We will now change the title to 'My First Program'

import tkinter
master=tkinter.Tk()
master.geometry('300x100')
master.title("My First Program")

Adding text to the window.

We can add text to the window using the label widget. The label widget here takes two parameters. One of them is the text we want to input while other is the instance of the frame. (that is the Tk() instance). After we do that, we then need to place the widget in a proper coordinate on the frame. We do that using the place() attribute. The place attribute takes two inputs, the x and the y values of the coordinates. place(x = 150, y = 150)

import tkinter
master=tkinter.Tk()
master.geometry('300x300')
master.title("My First Program")
label = tkinter.Label(master, text = "Hello world").place(x = 150, y = 150)

Forgetting the place() attribute will not display the text
image

So friends we have successfully understood and completed the 'Hello world' program in Tkinter! More exciting UI to come, as the journey continues...

17