Generate Hashtags from User Input in a Tkinter Text Widget

Tool to generate Hashtags from Tkinter Text widget

This desktop utility reads text from a Tkinter Text widget (t1), removes unwanted words or punctuation, formats words as hashtags, displays them in a second Text widget (t2), and copies the result to the system clipboard.

1. Reading & Cleaning Text Input

In Tkinter, a Text widget stores multi-line content using "line.column" indices. Reading from "1.0" to "end-1c" captures the text without the trailing newline character added by Tkinter.

my_str = t1.get("1.0", "end-1c")
my_str = my_str.replace("\n", " ").replace("\r", "")

Splitting the cleaned text produces a Python list of words. Calling split() with no arguments handles multiple spaces cleanly, whereas split(" ") uses a single space delimiter and can leave empty items when consecutive spaces exist:

tags_all = my_str.split(" ")

2. Filtering Stop Words with Set

Words like conjunctions, articles, and standalone punctuation marks are usually skipped when building hashtags. Converting both the word list and a stop-word list into a Python set removes unwanted terms and duplicate entries.

tags_remove = ["all", "All", "to", "To", "And", "and", "or", "is", ".", ","]
tags_all = list(set(tags_all) - set(tags_remove))

You can also load excluded words from a text file, JSON config, or SQL table (for example, using SQLite SELECT with WHERE) rather than hardcoding them in the script[cite: 12].

3. Adding # & Joining Strings

Use a for loop or list comprehension to add # in front of each word, pass items through map() if needed, and join them with join() into a single string before updating t2.

tags_all = ["#" + s for s in tags_all]
my_str = " ".join(map(str, tags_all))

t2.delete("1.0", tk.END)
t2.insert(tk.END, my_str)

4. Selection & Clipboard Copy

To highlight the output inside t2 and copy it to the system clipboard, add a "sel" tag across the text range, style it, and copy the selected text with pyperclip.

t2.tag_add("sel", "1.0", "end")
t2.tag_config("sel", background="green", foreground="red")
pyperclip.copy(t2.selection_get())
Tkinter tool to generate Hashtags from the User input in a text widget and copy to clip board


5. Complete Tkinter Application

If pyperclip is not installed in your Python environment yet, running the script will raise:

ModuleNotFoundError: No module named 'pyperclip'

Install pyperclip from your terminal or command prompt before running the full script:

pip install pyperclip

Full code including pyperclip:

import tkinter as tk
import pyperclip  # Copy generated text to system clipboard

my_w = tk.Tk()
my_w.geometry("700x500")
my_w.title("www.plus2net.com")

l1 = tk.Label(my_w, text="Your Data", width=10, font=20)
l1.grid(row=0, column=0, padx=10, pady=10, columnspan=2)

t1 = tk.Text(my_w, height=6, width=50, bg="yellow", font=28)
t1.grid(row=1, column=0, padx=10, columnspan=2)

b1 = tk.Button(my_w, text="Generate hashtags #", font=22, command=lambda: my_tags())
b1.grid(row=2, column=0, pady=5)

t2 = tk.Text(my_w, height=6, width=50, font=14)
t2.grid(row=3, column=0, padx=20, pady=20)

def my_tags():
    my_str = t1.get("1.0", "end-1c")
    my_str = my_str.replace("\n", " ").replace("\r", "")
    tags_all = my_str.split(" ")

    tags_remove = ["all", "All", "to", "To", "And", "and", "or", "is", ".", ","]
    tags_all = list(set(tags_all) - set(tags_remove))

    tags_all = ["#" + s for s in tags_all]
    my_str = " ".join(map(str, tags_all))

    t2.delete("1.0", tk.END)
    t2.insert(tk.END, my_str)
    t2.tag_add("sel", "1.0", "end")
    t2.tag_config("sel", background="green", foreground="red")
    pyperclip.copy(t2.selection_get())

my_w.mainloop()

6. FAQs & Common Issues

  • Why does set() change the order of words? A Python set does not preserve insertion order, so words may appear in a different sequence after set difference.
  • Why use "end-1c" when reading from t1? Tkinter appends a trailing newline character to a Text widget; "end-1c" reads up to one character before the end to avoid extra blank lines.
  • How do I avoid tk.TclError when clicking Generate on empty text? If t2 has no selected text, calling t2.selection_get() raises tk.TclError; checking whether my_str is non-empty before selecting and copying prevents that error.

Related Tkinter & Python Tutorials

Entry Cut copy paste in Entry widgets Python Tkinter Projects Spinbox DoubleVar



Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer