1
\$\begingroup\$

I am using PySide and the following widget sublassed from QLabel to display either the current day or time in my application:

# -*- coding: utf-8 -*-

import datetime
from PySide import QtGui, QtCore

class DateTimeLabel(QtGui.QLabel):
    """ Displays current date or time in a label."""
    choice = "Time"

    def __init__(self, parent=None):
        super(DateTimeLabel, self).__init__(parent)

        timer = QtCore.QTimer(self)
        timer.setInterval(100)
        timer.timeout.connect(self.display_current_date_time)
        timer.start()

    def display_current_date_time(self):
        """ Get today's date and current time"""
        now = datetime.datetime.now()
        today_date = datetime.datetime.strftime(now, "%d/%m/%Y")
        today_time = datetime.datetime.strftime(now, "%H:%M:%S")

        if self.choice == "Time":
            content = today_time
        else:
            content = today_date
        self.setText(content)

I think it can be improved. Any ideas ?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.