Write "Hello World" With tkinter Python | Creating GUI with Python

This is a quick tutorial to set up an Environment for creating Graphical user interfaces with the Python library name, Tkinter. I will walk you, to setup environment and create your first Tkinter app. We will not do some crazy things. I will introduce you to the Tkinter Python Library, in a step-by-step, by starting with something simple. 

What is Tkinter?

Tkinter is a python library for creating Graphical user interfaces with Python programming language. It is the most used python library for creating applications for desktops. With Tkinter, you can create a Desktop application for Windows, macOS, and Linux. 

Installing Tkinter 

You do not need to install Tkinter if you are using windows and you already have Python installed on your computer because it is already building to the python installer. If you do not have python install, go to their official website of Python and download the latest version and then install it. 

Creating Hello World App with Tkinter Python?

When we start learning a new programming language, our first task is to see the hello world is the output. So is the case here😂. Let's see how we can create a hello world app with Tkinter, a Python library for creating graphical user interfaces for desktops. 

Run the following code in your Code Editor, suppose you have installed an IDE for running the Python programs. If you have not installed any IDE, I recommend install Pycharm as an IDE, and believe me you will love it. 

    
from tkinter import Tk
from tkinter import ttk
window = Tk()
ttk.Label(window, text="Hello World").grid()
window.mainloop()


The above python program is a very simple and basic app that will just create a window and create a label in that window and say "hello world". 

Below is the output of the above code. Although this is the first step, As I mentioned this is the first step for creating advanced python graphical user interfaces. 

Explaining the Code:-

So now let's see what each line of code means in the above program that created the "hell world " desktop app.


from tkinter import Tk
from tkinter import ttk

This means that we have used the Tkinter Library to import the TK() class. This is how we import classes and other kinds of stuff from a Python library. 



window = Tk()

In this line of code, we have created the object of the TK class. Most of the time the object name is root. But it is up to you whatever you call the object of the TK() class. In my case, I have named the object as a window. This provides a root for our graphic user interface and we will be building things on top of this root. 

ttk.Label(window, text="Hello World").grid()

Now once we have the root (widow) ready we have created a label and set the layout to the grid. the Label() is a widget and we will look into it in a few moments. The label widget has two parameters,

the window: provides the root, text: it specifies the text of the label. 


window.mainloop()

This provides the main loop and helps the window frames to be there. This is also called the event loop, which meant that we are creating a loop for the events. this is necessary for everything to appear on the screen. 

Learn Tkinter In Easy Way

Post a Comment

0 Comments