
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.
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(" ")
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].
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)
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())
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()
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."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.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.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.