Python Tutorial: Tkinter Course – Creating Graphic User Interfaces

Posted by


Tkinter is a popular Python library used to create Graphical User Interfaces (GUIs). In this tutorial, we will cover the basics of Tkinter and how to create simple GUI applications using the library.

Installing Tkinter:
Tkinter is included in the standard library of Python, so there is no need to install it separately. You can simply import the library in your Python script using the following command:

import tkinter as tk

Creating a Window:
The first step in creating a GUI application using Tkinter is to create a window. This can be done using the Tk() method, which creates a new Tkinter window. You can customize the window by setting properties such as its title, size, and background color. Here is an example code to create a simple window:

import tkinter as tk

root = tk.Tk()
root.title("My GUI Application")
root.geometry("400x300")
root.configure(bg="white")

root.mainloop()

Adding Widgets:
Widgets are the building blocks of a GUI application. There are many types of widgets available in Tkinter, such as labels, buttons, entry fields, and more. You can add widgets to the window using methods like Label(), Button(), Entry(), etc. Here is an example code to add a label and a button to the window:

import tkinter as tk

def on_button_click():
    label.config(text="Button clicked!")

root = tk.Tk()
root.title("My GUI Application")

label = tk.Label(root, text="Hello, World!")
label.pack()

button = tk.Button(root, text="Click me!", command=on_button_click)
button.pack()

root.mainloop()

Layout Management:
Tkinter provides several layout managers to arrange widgets within a window. Two common layout managers are pack() and grid(). The pack() method is used to place widgets in a vertical or horizontal layout, while the grid() method is used to place widgets in a grid layout. Here is an example code to create a simple grid layout:

import tkinter as tk

root = tk.Tk()
root.title("Grid Layout Example")

label1 = tk.Label(root, text="Label 1")
label1.grid(row=0, column=0)

label2 = tk.Label(root, text="Label 2")
label2.grid(row=0, column=1)

button = tk.Button(root, text="Button")
button.grid(row=1, column=0, columnspan=2)

root.mainloop()

Handling Events:
You can define functions to handle events like button clicks, key presses, or mouse movements in Tkinter. You can use the command parameter of widgets to specify the function to be called when an event occurs. Here is an example code to handle button clicks:

import tkinter as tk

def on_button_click():
    label.config(text="Button clicked!")

root = tk.Tk()
root.title("Event Handling Example")

label = tk.Label(root, text="Hello, World!")
label.pack()

button = tk.Button(root, text="Click me!", command=on_button_click)
button.pack()

root.mainloop()

Conclusion:
In this tutorial, we covered the basics of creating GUI applications using Tkinter in Python. We learned how to create a window, add widgets, manage layouts, and handle events. Tkinter is a versatile library that can be used to create a wide variety of GUI applications, from simple tools to complex applications. With practice and experimentation, you can create powerful and user-friendly interfaces using Tkinter.

0 0 votes
Article Rating
44 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@abhinabgogoi2000
1 month ago

I am kinda confused… I visited the website and the same course is priced at $49 and here it is taught for free. How are they profitable?

@FrankT860
1 month ago

Day 1º 29:35
Day 2º 1:49:00

@LindaRodriguez-l3r
1 month ago

Brown Frank Hall Timothy Miller Deborah

@марияагафоник
1 month ago

Rodriguez Helen Lopez Ruth Clark Sharon

@KulshresthChauhan
1 month ago

54:00

@fatalityA2B2
1 month ago

39:56 ,checkpoint

@fatalityA2B2
1 month ago

26:38 can someone explain this to me? Why doesn't it work with parenthesis?

@Minemario6
1 month ago

At around 1:40:34, you can just configure the image to change and use a separate variable to keep track of the current image, like this.

cur_img = 0

def forward(img):

global cur_img

cur_img += 1

img.configure(image=image_list[cur_img])

And,

right_arrow = Button(root, text="->", command=lambda: forward(Main_img))

You can also do this for the back arrow, just change the += sign to a -= sign.

@重血
1 month ago

thx!!!

@RamanMichelle-y2i
1 month ago

Little Fields

@Sabinajorina
1 month ago

Johnson Kevin Thomas Richard Miller Larry

@nazeerhussain3364
1 month ago

sir, I have no words how to thank you.. appreciate your time and effort.

@Bessadi777
1 month ago

GOAT

@brianfuller2241
1 month ago

If you want a good running start at Tkinter then this video is worth your time. Thank you John Elder for building and making your zillion other videos available for either no cost or beyond reasonable low cost.

@useronthegrind
1 month ago

great course man… respect!

@useronthegrind
1 month ago

Hey guys, I made this calculator… check it out pls…
———————————————————————————————
from tkinter import *

root = Tk()
root.title('Completely Non-Sus Calculator')

# all variables
button_size = 21
previous_num = 0
add_nums = False
sub_nums = False
mul_nums = False
div_nums = False

# defining the required functions here
def addition():
global previous_num
global add_nums
first_value = e.get() # 3
previous_num += int(first_value) # 3 – 6
e.delete(0, END)
add_nums = True
return int(previous_num)

def subtraction():
global previous_num
global sub_nums
first_value = e.get()
if previous_num == 0:
previous_num += int(first_value)
else:
previous_num -= int(first_value)
e.delete(0, END)
sub_nums = True
return int(previous_num)

def multiplication():
global previous_num
global mul_nums
first_value = e.get()
if previous_num == 0:
previous_num += int(first_value)
else:
previous_num *= int(first_value)
e.delete(0, END)
mul_nums = True
return int(previous_num)

def division():
global previous_num
global div_nums
first_value = e.get()
if previous_num == 0:
previous_num += int(first_value)
else:
previous_num /= int(first_value)
e.delete(0, END)
div_nums = True
return int(previous_num)

def button_click(number):
current_num = e.get()
e.delete(0, END)
e.insert(0, str(current_num) + str(number))
if number == 'delete':
e.delete(0, END)
global previous_num
previous_num = 0

def button_equal():
global add_nums
global sub_nums
global mul_nums
global div_nums
global previous_num
if add_nums:
first_num = e.get() # 1
e.delete(0, END)
previous_num += int(first_num) # 6 – 7
e.insert(0, previous_num)
previous_num = 0
elif sub_nums:
first_num = e.get()
e.delete(0, END)
previous_num -= int(first_num)
e.insert(0, previous_num)
previous_num = 0
elif mul_nums:
first_num = e.get()
e.delete(0, END)
previous_num *= int(first_num)
e.insert(0, previous_num)
previous_num = 0
elif div_nums:
first_num = e.get()
e.delete(0, END)
previous_num /= int(first_num)
e.insert(0, previous_num)
previous_num = 0
add_nums = False
sub_nums = False
mul_nums = False
div_nums = False

# define your buttons
e = Entry(root, borderwidth=5, width=35)
e.get()
button1 = Button(root, text='1', padx=button_size, pady=button_size, command=lambda: button_click(1))
button2 = Button(root, text='2', padx=button_size, pady=button_size, command=lambda: button_click(2))
button3 = Button(root, text='3', padx=button_size, pady=button_size, command=lambda: button_click(3))
button4 = Button(root, text='4', padx=button_size, pady=button_size, command=lambda: button_click(4))
button5 = Button(root, text='5', padx=button_size, pady=button_size, command=lambda: button_click(5))
button6 = Button(root, text='6', padx=button_size, pady=button_size, command=lambda: button_click(6))
button7 = Button(root, text='7', padx=button_size, pady=button_size, command=lambda: button_click(7))
button8 = Button(root, text='8', padx=button_size, pady=button_size, command=lambda: button_click(8))
button9 = Button(root, text='9', padx=button_size, pady=button_size, command=lambda: button_click(9))
button0 = Button(root, text='0', padx=button_size, pady=button_size, command=lambda: button_click(0))
clearAll = Button(root, text='C', padx=button_size, pady=55, command=lambda: button_click('delete'))
equalsTo = Button(root, text='=', padx=button_size, pady=55, command=button_equal)
add = Button(root, text='+', padx=button_size, pady=button_size, command=addition)
subtract = Button(root, text='-', padx=button_size, pady=button_size, command=subtraction)
multiply = Button(root, text='*', padx=button_size, pady=button_size, command=multiplication)
divide = Button(root, text='/', padx=button_size, pady=button_size, command=division)

# put the buttons on the screen
e.grid(column=1, row=1, columnspan=4)
button1.grid(column=1, row=3)
button2.grid(column=2, row=3)
button3.grid(column=3, row=3)
button4.grid(column=1, row=4)
button5.grid(column=2, row=4)
button6.grid(column=3, row=4)
button7.grid(column=1, row=5)
button8.grid(column=2, row=5)
button9.grid(column=3, row=5)
button0.grid(column=2, row=6)
clearAll.grid(column=4, row=3, rowspan=2)
equalsTo.grid(column=4, row=5, rowspan=2)
add.grid(column=1, row=2)
subtract.grid(column=2, row=2)
multiply.grid(column=3, row=2)
divide.grid(column=4, row=2)

root.mainloop()
—————————————————————————————–

@lisadykstra8308
1 month ago

Why not use Visual Studio Code, where you can run the program in the same window instead of switching back and forth? It's frustrating because I'd like to see the coding screen to still be typing it out in my own window when he's taking the time to run it.

@angelicagabrieli7169
1 month ago

Thank you so so much for this informative and awesome course! God Bless you, brother!

@hdbnrw6442
1 month ago

Mehr sinnfreies gequatsche als alles andere

@VictorVictory-te2ij
1 month ago

Thank you so very much Sir!