PyQt - QTimeEdit



In previous chapter, we learned the concept of editing in QDateTimeEdit where both date and time was changed by the users. Here, we implement the task of time edit using QTimeEdit.

Implementing of time in GUI development is important for various factor−

  • Real time Updates − Computer users can track their time while doing multitasking operation on system.
  • Event Scheduling − Developer can added the features of time during development of software.
  • User Interaction − The response time is one factor that determines how the user interface (UI) unfolds.
  • Animation and Visual Effect is crucial to display the time within GUI application.
qtimeedit_ex_one

What is QTimeEdit?

A class QTimeEdit works on widgets for editing time based on the widget QDateTimeEdit. There are some common matches of properties between the classes QTimeEdit and QDateTimeEdit and these are listed below −

  • time() − The widgets return the time as display output. It also returns the type Qtime. Using the method toPytime(), it converts datetime.time object.
  • displayFormat − This method defines the string formats and it is display in the widget.
  • minimumTime − This method can be used to set the earliest time.
  • maximumTime − This method can be set the new or current time.

Example 1

Following example illustrate the time with format using QTimeEdit class and it's relevant methods.

import sys
from PyQt6.QtCore import Qt, QTime
from PyQt6.QtWidgets import(
   QApplication,
   QMainWindow,
   QLabel,
   QVBoxLayout,
   QWidget,
   QTimeEdit,
)
class TimeEditDemo(QMainWindow):
   def __init__(self):
      super().__init__()
      self.init_ui()

   def init_ui(self):
      self.setWindowTitle("Time Edit Demo")
      self.setGeometry(300, 300, 400, 150)

      # Create a label to display the selected time
      self.time_label = QLabel("Selected Time:", self)

      # Create a QTimeEdit widget
      self.time_edit = QTimeEdit(self)
      # Set initial time using QTime
      self.time_edit.setTime(QTime.currentTime())  
      # Connect the timeChanged signal to update the label
      self.time_edit.timeChanged.connect(self.update_time_label)

      # Set up the layout
      layout = QVBoxLayout()
      layout.addWidget(self.time_label)
      layout.addWidget(self.time_edit)
      central_widget = QWidget()
      central_widget.setLayout(layout)
      self.setCentralWidget(central_widget)

   def update_time_label(self, new_time):
      # Formatted time string
      self.time_label.setText(f"Selected Time: {new_time.toString(Qt.DateFormat.ISODate)}")  

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = TimeEditDemo()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output-

qtimeedit_ex_one

Example 2

Following is an another example demonstrate the time with format using QTimeEdit.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTimeEdit, QLabel, QFormLayout
class MainWindow(QWidget):
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)

      self.setWindowTitle('QTimeEdit')
      self.setMinimumWidth(200)

      # create a grid layout
      layout = QFormLayout()
      self.setLayout(layout)

      self.time_edit = QTimeEdit(self)
      self.time_edit.editingFinished.connect(self.update)

      self.result_label = QLabel('', self)

      layout.addRow('Time:', self.time_edit)
      layout.addRow(self.result_label)

      # show the window
      self.show()

   def update(self):
      value = self.time_edit.time()
      self.result_label.setText(str(value.toPyTime()))

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = MainWindow()
   sys.exit(app.exec())

Output

The above code produces the following output-

qtimeedit_ex_two
Advertisements