24- Enhance Your PyQGIS Toolbar with a Time Button: A Step-by-Step Guide

Learn how to enhance your PyQGIS toolbar with a time button by following this step-by-step guide. With the help of necessary modules and code, you can create a QGIS toolbar icon that displays the current time when clicked. Download the time icon and define the path to the icon to get started with this tutorial. Follow along with the instructions to create your own time button and upgrade your PyQGIS toolbar.

First, download the time icon image.

Then, import the necessary modules:

import os
import pytz
from datetime import datetime

Next, define the path to the icon:

icon = 'time.png'
data_dir = os.path.join(os.path.expanduser('~'), 'D://', 'Python_QGIS')
icon_path = os.path.join(data_dir, icon)

Next, define Baghdad local time by creating the "WhatTime" function:

def WhatTime():
    tz_Ba = pytz.timezone('Asia/Baghdad') 
    now = datetime.now(tz_Ba)
    current_time = now.strftime("%H:%M:%S")
    iface.messageBar().pushInfo('Baghdad Time', current_time)

Finally, create a QGIS toolbar icon that displays the current time when clicked:

action = QAction('What time is it?')
action.triggered.connect(WhatTime)
action.setIcon(QIcon(icon_path))
iface.addToolBarIcon(action)

Here's the overall code:

# Import the necessary modules:
import os
import pytz
from datetime import datetime

Define the path to the icon:
icon = 'time.png'
data_dir = os.path.join(os.path.expanduser('~'), 'D://', 'Python_QGIS')
icon_path = os.path.join(data_dir, icon)

# Define Baghdad local time
def WhatTime():
    tz_Ba = pytz.timezone('Asia/Baghdad') 
    now = datetime.now(tz_Ba)
    current_time = now.strftime("%H:%M:%S")
    iface.messageBar().pushInfo('Baghdad Time', current_time)

# Create a QGIS toolbar icon that displays the current time when clicked:    
action = QAction('What time is it?')
action.triggered.connect(WhatTime)
action.setIcon(QIcon(icon_path))
iface.addToolBarIcon(action)

In conclusion, this step-by-step guide provides a clear and concise tutorial on how to enhance your PyQGIS toolbar with a time button. By following the instructions and using the necessary modules and code, users can create a QGIS toolbar icon that displays the current time when clicked. This feature can be useful for those who need to keep track of time while working with PyQGIS. Overall, this tutorial is a useful resource for those looking to upgrade their PyQGIS toolbar.

If you like the content, please SUBSCRIBE to my channel for the future content

Did you find this article valuable?

Support Azad Rasul by becoming a sponsor. Any amount is appreciated!