Generate Hashtags from user input string

Tool to generate Hashtags


Here is one application to generate Hashtags using the string entered in a Text box and display the same in another text widget . We can copy the generated Hashtags to use.

Reading the user entered string

We will read the user entered string inside the text widget and then remove the line breaks and carriage returns.
my_str=t1.get("1.0",'end-1c') # read the inputs
my_str = my_str.replace('\n', ' ').replace('\r', '')# linebreak carriage
create a list from this string by using split(), one space is used as delimiter.
tags_all=my_str.split(' ') # break with one space gap to create list

Negative List of words

Some words we will remove as we don't want these words to be used to create the Hashtags.
As we are using set() to create the list, in our final list will have unique words only.
tags_remove=['all','All','to','To','And','and','or','is','.',',']
tags_all=list(set(tags_all)-set(tags_remove))
The above list of negative words ( tags_remove ) can be taken from database or from other sources. There are many sample lists are available in Internet.

Generating List with Hashtags

By using for loop, we can add hashtags ( # ) to each element of the list.
tags_all=['#' + s  for s in tags_all] # add hash to all elements
Using the list ( tags_all ) we will create a string. We have used map() to convert each element of the list to string before joining them using join().
my_str = ' '.join(map(str, tags_all)) # from list create string
Remove the previous data if any and then add the string to Text widget ( t2 ).
t2.delete('1.0','end') # remove the previous entry if any 
t2.insert(tk.END, my_str) # add to text 
In this tool the data is available within the application. To copy to clipboard and take out we have to use pyperclip library. Install pyperclip
pip install pyperclip
To copy
t2.tag_add("sel", "1.0","end") # all text selected
t2.tag_config("sel",background="green",foreground="red")
pyperclip.copy(t2.selection_get()) # copy to clipboard 

Tkinter tool to generate Hashtags from the User input in a text widget and copy to clip board


Full code including the pyperclip is here

import tkinter as tk
#from tkinter import END
import pyperclip # to copy data to system clipboard 
my_w = tk.Tk()
my_w.geometry("700x500")  # Size of the window 
my_w.title("www.plus2net.com")  # Adding a title
l1 = tk.Label(my_w,  text='Your Data', width=10,font=20 )  # added one Label 
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) # text box
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') # read the inputs
    my_str = my_str.replace('\n', ' ').replace('\r', '')# linebreak carriage 
    tags_all=my_str.split(' ') # break with one space gap to create list
    
	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] # add hash to all elements
    my_str = ' '.join(map(str, tags_all)) # from list create string
    
	t2.delete('1.0','end') # remove the previous entry if any 
    #print(tags_all)
    t2.insert(tk.END, my_str) # add to text 
    t2.tag_add("sel", "1.0","end") # all text selected
    t2.tag_config("sel",background="green",foreground="red")
    pyperclip.copy(t2.selection_get()) # copy to clipboard 
my_w.mainloop()  # Keep the window open

Entry Cut copy paste in Entry widgets Python Tkinter Projects Spinbox DoubleVar
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    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 FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer