Tkinter is a popular GUI toolkit in Python that allows you to create graphical applications with ease. In this tutorial, we'll walk through the process of building a simple calculator using Tkinter.

Prerequisites

Before starting, ensure you have Python installed on your system. Tkinter comes pre-installed with most Python distributions.

Step 1: Importing required modules

First, import the necessary modules from Tkinter:

from tkinter import *
from tkinter import ttk

Step 2: Setting up colors

Define a color scheme for the application:

co1 = "#feffff"  # White
co2 = "#6f9fbd"  # Blue
co3 = "#38576b"  # Display color
background = "#3b3b3b"
co10 = "#ECEFF1"
color1 = '#FFAB40'
color2 = '#ff333a'
color3 = '#6bd66f'
color4 = "#ab8918"

Step 3: Creating the main window

Initialize the main application window:

window = Tk()
window.title('Simple Calculator')
window.geometry('235x318')
window.configure(bg=co1)
style = ttk.Style(window)
style.theme_use("clam")

Step 4: Creating frames

Divide the window into two frames: one for the display and another for buttons.

ttk.Separator(window, orient=HORIZONTAL).grid(row=0, columnspan=1, ipadx=280)
frame_display = Frame(window, width=300, height=56, bg=co3, relief="flat")
frame_display.grid(row=1, column=0, sticky=NW)
frame_buttons = Frame(window, width=300, height=340, bg=background, relief="flat")
frame_buttons.grid(row=2, column=0, sticky=NW)

Step 5: Defining functions

Define functions to handle input, calculations, and clearing the display:

def entering_values(event):
    global all_values
    all_values += str(event)
    value_text.set(all_values)

def calculate():
    global all_values
    try:
        result = str(eval(all_values))
        value_text.set(result)
        all_values = ""
    except:
        value_text.set("Error")
        all_values = ""

def clear_screen():
    global all_values
    all_values = ""
    value_text.set("")

Step 6: Display setup

Create a label to display entered values and results:

all_values = ""
value_text = StringVar()
display = Label(frame_display, width=16, height=2, textvariable=value_text, padx=7, anchor="e",
                bd=0, justify=RIGHT, font=('Ivy 18 '), bg='#37474F', fg=co1)
display.place(x=0, y=0)

Step 7: Adding buttons

Create buttons for numbers and operations, linking them to appropriate functions:

Button(frame_buttons, text="C", width=11, height=2, bg=co10, fg=background, font=('Ivy 13 bold'),
       relief=RAISED, overrelief=RIDGE, command=clear_screen).place(x=0, y=0)
Button(frame_buttons, text="%", width=5, height=2, bg=co10, fg=background, font=('Ivy 13 bold'),
       relief=RAISED, overrelief=RIDGE, command=lambda: entering_values('%')).place(x=118, y=0)
Button(frame_buttons, text="/", width=5, height=2, bg=color1, fg=co1, font=('Ivy 13 bold'),
       relief=RAISED, overrelief=RIDGE, command=lambda: entering_values('/')).place(x=177, y=0)

numbers = [("7", 0, 52), ("8", 59, 52), ("9", 118, 52), ("4", 0, 104), ("5", 59, 104),
           ("6", 118, 104), ("1", 0, 156), ("2", 59, 156), ("3", 118, 156), ("0", 0, 208)]
for num, x, y in numbers:
    Button(frame_buttons, text=num, width=5 if num != '0' else 11, height=2, bg=co10, fg=background,
           font=('Ivy 13 bold'), relief=RAISED, overrelief=RIDGE,
           command=lambda n=num: entering_values(n)).place(x=x, y=y)

operations = [("*", 177, 52), ("-", 177, 104), ("+", 177, 156), (".", 118, 208), ("=", 177, 208)]
for op, x, y in operations:
    Button(frame_buttons, text=op, width=5, height=2, bg=color1 if op != '=' else color3, fg=co1,
           font=('Ivy 13 bold'), relief=RAISED, overrelief=RIDGE,
           command=lambda o=op: entering_values(o) if o != '=' else calculate()).place(x=x, y=y)

Step 8: Running the application

Finally, start the Tkinter main loop to display the calculator:

window.mainloop()

Conclusion

Congratulations! You have successfully built a basic calculator using Tkinter. This project can be expanded by adding more advanced mathematical functions, themes, or improved error handling. Have fun experimenting!