18
Creating Your First Application On Python Using 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๐ ..
First off to get started, we need to Install Tkinter using pip
On your terminal type,
pip install tk
Note: You will need the latest version of python(3.9.0) or higher.
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()
Now if you run the following code, you can see a window as shown below.
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')
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()
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()
Here's the output of the following code,
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.๐
18