User Tools

Site Tools


tkinter_pack

TKInter Pack

This tutorial was a big hit. I'll cut to the chase, we introduced each concept slowly but here is the final code:

import tkinter as tk
from tkinter import ttk
import codecs
import binascii

def code_master_base64():
    s = entry1.get()

    try:
        # Decoding input string from base64
        a = codecs.decode(s.encode('utf-8'), 'base64').decode('utf-8')
    except Exception as e:
        a = "Invalid code: {}".format(str(e))

    # Encoding input string to base64
    b = codecs.encode(s.encode('utf-8'), 'base64').decode('utf-8')

    # Update labels and entry
    label2.config(text=a)
    entry2.delete(0, tk.END)
    entry2.insert(0, b)

def code_master_hex():
    s = entry1.get()

    try:
        a = codecs.decode(s, 'hex')
    except Exception as e:
        a = "Invalid code: {}".format(str(e))

    b = codecs.encode(s.encode('utf-8'), 'hex')

    label2.config(text=a)
    entry2.delete(0, tk.END)
    entry2.insert(0, b)

def code_master_rot13():
    s = entry1.get()

    a = codecs.decode(s, 'rot_13')
    label2.config(text=a)
    entry2.delete(0, tk.END)
    entry2.insert(0, a)

def center_window(window, width, height):
    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()

    x = (screen_width - width) // 2
    y = (screen_height - height) // 3

    window.geometry('{}x{}+{}+{}'.format(width, height, x, y))


# Create the main window
root = tk.Tk()
root.geometry("400x400")
center_window(root, 400, 400)

# Make a label.
label1 = tk.Label(root, text = "Code Master v1.2")
label1.pack(pady=10)

# Create a Text Entry field.
entry1 = tk.Entry(root)
entry1.pack(pady=10)
entry1.focus_set()


# Make a label.
label2 = tk.Label(root, text = "TRANSLATE CODE")
label2.pack(pady=10)

# Create a Text Entry field.
entry2 = tk.Entry(root)
entry2.pack(pady=10)

# Make an action button
button1 = tk.Button(root, text = "ROT13", command=code_master_rot13)
button1.pack(pady=10)

button2 = tk.Button(root, text = "Hex", command=code_master_hex)
button2.pack(pady=10)

button3 = tk.Button(root, text = "Base64", command=code_master_base64)
button3.pack(pady=10)

# Start the window.
root.mainloop()
tkinter_pack.txt · Last modified: 2024/04/17 08:41 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki