DEV Community

Mate Technologies
Mate Technologies

Posted on

Build a Caesar Cipher GUI App in Python with Tkinter

In this tutorial, we’ll create a Caesar Cipher tool using Python’s tkinter. By the end, you’ll have a desktop app that can encrypt and decrypt text with a shift of your choice.

We’ll break it down step by step.

Step 1: Import Required Libraries

First, we need to import the libraries:

import tkinter as tk
from tkinter import messagebox
Enter fullscreen mode Exit fullscreen mode

tkinter: The standard Python library for creating GUI applications.

messagebox: Optional, used to show popup messages (like errors or alerts).

We don’t need sys or os for this app, so we can skip them.

Step 2: Define the App Theme

Let’s define some colors and fonts to make the app look modern:

# =========================
# THEME
# =========================
APP_BG = "#121212"
PANEL_BG = "#1F1F1F"
BTN_BG = "#2C2C2C"
ACCENT = "#FF6F61"
TEXT_CLR = "#E0E0E0"
SUBTEXT_CLR = "#AAAAAA"

FONT = ("Segoe UI", 11)
Enter fullscreen mode Exit fullscreen mode

APP_BG: Main background color

PANEL_BG: Panel background color

BTN_BG: Button color

ACCENT: Highlight color for headers or buttons

TEXT_CLR / SUBTEXT_CLR: Colors for text and subtext

FONT: Default font for labels and buttons

This makes the app look consistent and modern.

Step 3: Create the Main App Class

We’ll create a Python class to structure our app:

class CaesarCipherApp:
    def __init__(self, root):
        self.root = root
        root.title("MateTools – Caesar Cipher")
        root.geometry("800x500")
        root.configure(bg=APP_BG)
        root.resizable(False, False)
Enter fullscreen mode Exit fullscreen mode

root: The main Tkinter window

title(): Sets the window title

geometry(): Sets the window size

configure(bg=...): Sets background color

resizable(False, False): Prevents window resizing

Step 4: Add the Left Panel (Input & Buttons)

The left panel will hold input text, shift value, and buttons:

# Left panel
left = tk.Frame(root, bg=PANEL_BG, width=350)
left.pack(side="left", fill="y")

# Header
header = tk.Frame(left, bg=PANEL_BG)
header.pack(fill="x", padx=16, pady=(18, 10))

tk.Label(
    header,
    text="MateTools",
    bg=PANEL_BG,
    fg=ACCENT,
    font=("Segoe UI", 20, "bold")
).pack(side="left")
Enter fullscreen mode Exit fullscreen mode

Frame: A container to organize widgets

pack(): Places the frame on the window

Label: Shows text

fg / bg: Font and background color

We can also add a title and description:

tk.Label(left, text="Caesar Cipher Tool", bg=PANEL_BG, fg=TEXT_CLR, font=("Segoe UI", 14, "bold")).pack(anchor="w", padx=16)
tk.Label(left, text="Encrypt or decrypt text using Caesar cipher", bg=PANEL_BG, fg=SUBTEXT_CLR, font=("Segoe UI", 10)).pack(anchor="w", padx=16, pady=(0, 16))
Enter fullscreen mode Exit fullscreen mode

Step 5: Add Input Fields

We need two inputs: the text to encrypt/decrypt and the shift number:

# Text input
tk.Label(left, text="Enter Text:", bg=PANEL_BG, fg=TEXT_CLR, font=FONT).pack(anchor="w", padx=16)
self.input_text = tk.Text(left, height=5, width=40, bg="#1E1E1E", fg=TEXT_CLR, font=FONT)
self.input_text.pack(padx=16, pady=(0, 10))

# Shift input
tk.Label(left, text="Shift:", bg=PANEL_BG, fg=TEXT_CLR, font=FONT).pack(anchor="w", padx=16)
self.shift_var = tk.IntVar(value=3)
tk.Entry(left, textvariable=self.shift_var, bg="#1E1E1E", fg=TEXT_CLR, font=FONT, width=5).pack(padx=16, pady=(0, 16))
Enter fullscreen mode Exit fullscreen mode

Text widget: Multi-line input

Entry widget: Single-line input

IntVar(): Holds the integer shift value

Step 6: Add Buttons for Encrypt & Decrypt

We’ll create buttons and attach them to methods:

btn_frame = tk.Frame(left, bg=PANEL_BG)
btn_frame.pack(fill="x", padx=16, pady=16)

def make_btn(text, cmd, color=BTN_BG):
    return tk.Button(
        btn_frame,
        text=text,
        command=cmd,
        bg=color,
        fg="white",
        font=("Segoe UI", 11, "bold"),
        relief="flat",
        height=2,
        width=18
    )

make_btn("Encrypt", self.encrypt_text, ACCENT).pack(side="left", expand=True, padx=4)
make_btn("Decrypt", self.decrypt_text, "#4CAF50").pack(side="left", expand=True, padx=4)
Enter fullscreen mode Exit fullscreen mode

command: Links the button to a function

flat relief: Modern button style

expand=True: Makes buttons fill available space

Step 7: Add the Right Panel (Output)

The right panel will display the encrypted or decrypted text:

right = tk.Frame(root, bg=APP_BG)
right.pack(side="right", fill="both", expand=True)

self.output_card = tk.Frame(right, bg=PANEL_BG)
self.output_card.pack(padx=30, pady=40, fill="both", expand=True)

tk.Label(self.output_card, text="Output", bg=PANEL_BG, fg=TEXT_CLR, font=("Segoe UI", 14, "bold")).pack(pady=(20, 10))
self.output_text = tk.Text(self.output_card, height=10, bg="#1E1E1E", fg=TEXT_CLR, font=FONT)
self.output_text.pack(padx=16, pady=10, fill="both", expand=True)
Enter fullscreen mode Exit fullscreen mode

Output text widget: Shows the result

fill="both" & expand=True: Makes the widget stretch

Step 8: Add Caesar Cipher Logic

Now we write the encryption and decryption logic:

def caesar_cipher(self, text, shift):
    result = ""
    for char in text:
        if char.isupper():
            result += chr((ord(char) - 65 + shift) % 26 + 65)
        elif char.islower():
            result += chr((ord(char) - 97 + shift) % 26 + 97)
        else:
            result += char
    return result
Enter fullscreen mode Exit fullscreen mode

ord(): Converts character to ASCII

chr(): Converts ASCII to character

% 26: Wraps around the alphabet

Non-alphabet characters are left unchanged

Step 9: Connect Buttons to Functions

def encrypt_text(self):
    text = self.input_text.get("1.0", tk.END).strip()
    shift = self.shift_var.get()
    self.output_text.delete("1.0", tk.END)
    self.output_text.insert(tk.END, self.caesar_cipher(text, shift))

def decrypt_text(self):
    text = self.input_text.get("1.0", tk.END).strip()
    shift = self.shift_var.get()
    self.output_text.delete("1.0", tk.END)
    self.output_text.insert(tk.END, self.caesar_cipher(text, -shift))
Enter fullscreen mode Exit fullscreen mode

.get("1.0", tk.END): Gets text from the Text widget

.insert() / .delete(): Updates the output widget

Encrypt adds shift; decrypt subtracts shift

Step 10: Run the App

Finally, start the Tkinter main loop:

if __name__ == "__main__":
    root = tk.Tk()
    app = CaesarCipherApp(root)
    root.mainloop()
Enter fullscreen mode Exit fullscreen mode

Tk(): Creates the main window

mainloop(): Keeps the window running

✅ Done! You now have a complete Caesar Cipher GUI app in Python.

It looks modern, allows text input, lets the user set a shift value, and shows encrypted/decrypted output.

Caesar Cipher

Top comments (0)