2 weeks ish in! Made a Word Counter

‘’’import tkinter as tk
import re
import string
window = tk.Tk()
window.geometry("900x800")
window.title("WORD COUNTER")

def countWords():
textInput = str(text.get("1.0", tk.END))

allPunctuation = string.punctuation
punctuation = f"[ {re.escape(allPunctuation)}]+"

wordList = re.split(punctuation, textInput)
wordCount = len(wordList)
resultLbl["text"] = f"There are {wordCount} words in the above text!"
print(wordList)

def quit():
window.destroy()

#define frame
window.rowconfigure(0, weight=8)
window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
window.columnconfigure(0, weight=1)
window.resizable(height=False, width=False)

#INPUT TEXT
text = tk.Text(window)
text.grid(row=0, column=0, padx=10, pady=10, sticky="news")

#LABEL
resultLbl = tk.Label(window, text="Write or Paste in your message, then click on the Button to count")
resultLbl.grid(row=1, column=0, padx=5, sticky="news")

#BUTTONS
frame = tk.Frame(window)
frame.grid(row=2, column=0, sticky="news")

quitButton = tk.Button(frame, text="Quit", foreground="white", background="red", relief="ridge", command = quit)
quitButton.pack(side="right", padx=5, pady=5)

countButton = tk.Button(frame, text="Get Count", foreground="white", background="blue", relief="ridge", command = countWords)
countButton.pack(side="right", padx=5, pady=5)

window.mainloop()’’’

Posted Jun 3, 2026, 1:22 PM UTC

10
Loading comments…