Interactive applications have been an area of interest for the past few years. But writing one is a trivial affair and needs a lot of effort. In this blog, I will be sharing a tool I made. This tool is an Instagram hashtag generator in Python with a graphical interface and can create any number of Instagram hashtags. We will be using pyqt5 for creating this Instagram hashtag generator.

Why create an Instagram Hashtag Generator in Python?

There are many reasons to use an Instagram Hashtag Generator in Python. For one, Python is a very popular language, so many resources and libraries are available. Additionally, Python is very easy to learn and use, so even if you’re not an experienced programmer, you should be able to get started quickly.

Another reason to use a Python Hashtag Generator is that it can automate the process of finding and using hashtags on Instagram. This can save you a lot of time, especially if you’re managing multiple accounts or running a business on Instagram. Additionally, a Python Hashtag Generator can help you ensure that you’re using the most popular and relevant hashtags for your posts, which can help you get more likes, comments, and followers.

Prerequisites:

Pyqt5:

Pyqt5 is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. It helps app developers to use Python to design their own Graphical User Interfaces (GUIs) using a drag-and-drop interface. It offers a comprehensive set of Python bindings for Qt v5. It is implemented as a Python plug-in and can be used in various Qt-based applications. 

The advantage of using Pyqt5 is that it is very easy to use and design GUI interfaces using Python, compared to other languages like C++. It is also cross-platform, meaning that it can be used on a variety of operating systems, including Windows, Linux, and macOS.

To install pyqt5 use the following code in the terminal:

pip install pyqt5

To install QT designer go to the following link then install the software:

https://build-system.fman.io/qt-designer-download

Step 1: launch the qt designer and setup the graphical interface

  1. Add the vertical layout and then lay it out vertically in the layout option.
  2. Add a horizontal layout
  3. Add a push button at the bottom
  4. Add two new vertical layouts to the horizontal layout

The window should be looking like this

  1. Add a new button in each vertical layout
  2. Then add a new label in each vertical layout
  3. Now add a new list view in the first vertical layout
  4. Add a new tree view in the second vertical layout

The window should be looking like this

  1. Now add the new menu with the file 
  2. Then add save and load in the file menu

The window should look like this

  1. Change the single selection mode to multi selection mode in the qabstractionitme view
  2. At this point, we can rename all the labels and buttons of the interface.
  3. And if you want to, you could also try changing your font size or font family for a better appearance in the qabstraction view.

Step 2: Create a new python file and code

Note: don’t forget to check the object name of your qt app because it can be different from mine.

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont
from PyQt5 import uic
from PyQt5.Qt import QStandardItemModel, QStandardItem
import pickle
import json
# We need to import json because we are importing the hashtags from a json file.

# lest create mygui class

class MyGUI(QMainWindow):

    def __init__(self):
        super(MyGUI, self).__init__()
        uic.loadUi('hastag.ui', self)
        self.show()

        # set the title
        self.setWindowTitle("hastag genrator")
        # then initialize the dictionary with json file
        self.hastag_dict = json.load(open('text.json'))
        self.init_tree_view()


        self.slected = []

        self.list_model = QStandardItemModel()
        self.listView.setModel(self.list_model)
     
        self.pushButton.clicked.connect(self.copy_to_clipboard)
        self.pushButton_2.clicked.connect(self.choose_selected)
        self.pushButton_3.clicked.connect(self.remove_selected)

        self.actionsave.triggered.connect(self.save_hashtag)
        self.actionLoad.triggered.connect(self.load_hashtag)


    def init_tree_view(self):
        self.treeView.setHeaderHidden(True)


        # define model
        self.tree_model = QStandardItemModel()
        self.treeView.setModel(self.tree_model)

        root_node = self.tree_model.invisibleRootItem()

        for category in self.hastag_dict.keys():
            category_item = QStandardItem(category)
            for hastag in self.hastag_dict[category]:
                category_item.appendRow(QStandardItem(hastag))
            root_node.appendRow(category_item)

        self.treeView.expandAll()

    def choose_selected(self):
        if len(self.treeView.selectedIndexes()) != 0:
            for index in self.treeView.selectedIndexes():
                if index.parent().isValid():
                    if index.data() not in self.slected:
                        self.slected.append(index.data())
                        self.list_model.appendRow(QStandardItem(index.data()))



 
    def remove_selected(self):
        if len(self.listView.selectedIndexes()) != 0:
            for index in reversed(sorted(self.listView.selectedIndexes())):
                self.slected.remove(index.data())
                self.list_model.removeRow(index.row())


 
    def copy_to_clipboard(self):
        if (len(self.slected) != 0):
            clipboard = QApplication.clipboard()
            clipboard.setText('#' + '#'.join(self.slected))


    def save_hashtag(self):
        filename, confirmed = QFileDialog.getSaveFileName(self, 'Save Hashtag', '', 'Tag File (*.tags)')
        if confirmed:
            with(open(filename, 'wb')) as f:
                pickle.dump(self.selected, f)

    def load_hashtag(self):
        filename, confirmed = QFileDialog.getOpenFileName(self, 'Load Hashtag', '', 'Tag File (*.tags)')
        if confirmed:
            with (open(filename, 'rb')) as f:
                self.selected = pickle.load(f)
            self.list_model = QStandardItemModel()
            for tag in self.selected:
                self.list_model.appendRow(QStandardItem(tag))
            self.listView.setModel(self.list_model)



app = QApplication([])
window = MyGUI()
app.exec_()

Explanation:

  1. Import all the dependencies
  2. Created my GUI class and added all the buttons labels listview and treeview.
  3. Imported hashtags from the JSON files.
  4. Defined the treeview and listview.
  5. Then defined the select and remove function of a hashtag
  6. Defined the function of all three buttons.

Output:

After running the app all the functions all working fine like selecting the hashtags, copying to the clipboard, saving the file, and loading the file.

Final Words

The Instagram hashtag generator is a tool that can be used by anyone. It’s really easy to use and even the most tech-savvy users will have an easy time making professional-looking hashtags for their photos! In order to make these hashtags, first, you pick a category. Then you decide how many hashtags you want to generate then it will convert them into strings of hashtags that you can copy into Instagram or any social media post. We hope you had fun making your hashtags and don’t forget to comment below if you have any questions.