
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
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.

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-

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-

Advertisements