Creating Your First Application On Python Using Tkinter

dhakshinesh

dhakshinesh

Posted on December 23, 2021

Creating Your First Application On Python Using Tkinter

What is Tkinter

For those who don't know what tkinter is, It is basically a python module for making GUI(Graphical User Interface), in which we can do all the beautiful things on the frontend and hide your messy backend code๐Ÿ˜…..

Getting Started

First off to get started, we need to Install Tkinter using pip
On your terminal type,

pip install tk
Enter fullscreen mode Exit fullscreen mode

Note: You will need the latest version of python(3.9.0) or higher.

Now to the fun stuff ๐Ÿ‘พ๐Ÿ‘พ..

After installing tkinter, you are ready to make your first python application.
On your text-editor, create a python file(.py) and then you'll be needing the following code to set up tkinter on your application.

#importing the tkinter module
from tkinter import *
import tkinter

#assigning a "root" as the main window for our application
root = tkinter.Tk()

root.mainloop()
Enter fullscreen mode Exit fullscreen mode

Now if you run the following code, you can see a window as shown below.

Tkinter Application window

This is the place all of our widgets which are the elements are going to be shown.

Before we go ahead, We need to make a fixed size for our application.
Just after assigning the root = tkinter.Tk() add,

root.geometry('500x500')
Enter fullscreen mode Exit fullscreen mode

What is better than making a "Hello World!" program?

Now we're going to a widget by tkinter which lets us make some cool text.

#Just a basic text.
Label(root, text="Hello World!").pack()

#Just a basic text but bigger.
Label(root, text="Hello World!", font=20).pack()

#A bold text with a different font
Label(root, text="Hello World!", font=('Jokerman',25,'bold')).pack()

#A text with red background
Label(root, text="Hello World!",bg='red', font=10 ).pack()

#A text with red color
Label(root, text="Hello World!",fg='red', font=10 ).pack()
Enter fullscreen mode Exit fullscreen mode

Here's the complete code of the program that we just made,

#importing the tkinter module
from tkinter import *
import tkinter

root = tkinter.Tk()
root.geometry('500x500')
#Main
#Just a basic text.
Label(root, text="Hello World!").pack()
#Just a basic text but bigger.
Label(root, text="Hello World!", font=20).pack()
#A bold text with a different font
Label(root, text="Hello World!", font=('Jokerman',25,'bold')).pack()
#A text with red background
Label(root, text="Hello World!",bg='red', font=10 ).pack()
#A text with red color
Label(root, text="Hello World!",fg='red', font=10 ).pack()
root.mainloop()
Enter fullscreen mode Exit fullscreen mode

Here's the output of the following code,

Hello World!

Congrats๐ŸŽ‰๐ŸŽ‰ you've successfully made your first python application!

That's it for this tutorial on making your first application, If you want to see more widgets by tkinter you can check this link

And by the way this is my first dev-post, I would really like to see what you think about it.๐Ÿ˜„

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
dhakshinesh
dhakshinesh

Posted on December 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About