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=T…



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')

image

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

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")

image



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)

image

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…


Print Share Comment Cite Upload Translate
APA
Aatmaj | Sciencx (2024-03-28T14:54:40+00:00) » Learning Python- Intermediate course: Day 21, Hello world in Tkinter !. Retrieved from https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/.
MLA
" » Learning Python- Intermediate course: Day 21, Hello world in Tkinter !." Aatmaj | Sciencx - Tuesday September 7, 2021, https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/
HARVARD
Aatmaj | Sciencx Tuesday September 7, 2021 » Learning Python- Intermediate course: Day 21, Hello world in Tkinter !., viewed 2024-03-28T14:54:40+00:00,<https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/>
VANCOUVER
Aatmaj | Sciencx - » Learning Python- Intermediate course: Day 21, Hello world in Tkinter !. [Internet]. [Accessed 2024-03-28T14:54:40+00:00]. Available from: https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/
CHICAGO
" » Learning Python- Intermediate course: Day 21, Hello world in Tkinter !." Aatmaj | Sciencx - Accessed 2024-03-28T14:54:40+00:00. https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/
IEEE
" » Learning Python- Intermediate course: Day 21, Hello world in Tkinter !." Aatmaj | Sciencx [Online]. Available: https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/. [Accessed: 2024-03-28T14:54:40+00:00]
rf:citation
» Learning Python- Intermediate course: Day 21, Hello world in Tkinter ! | Aatmaj | Sciencx | https://www.scien.cx/2021/09/07/learning-python-intermediate-course-day-21-hello-world-in-tkinter/ | 2024-03-28T14:54:40+00:00
https://github.com/addpipe/simple-recorderjs-demo