PyQt QTimeEdit

QTimeEdit

The QTimeEdit widget is used to let the user select and edit time. In the example, let's create one QTimeEdit object and set it to appear as the current time.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTimeEdit, QVBoxLayout
from PyQt5.QtCore import QTime

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        lbl = QLabel('QTimeEdit')

        timeedit = QTimeEdit(self)
        timeedit.setTime(QTime.currentTime())
        timeedit.setTimeRange(QTime(3, 00, 00), QTime(23, 30, 00))
        timeedit.setDisplayFormat('hh:mm:ss')

        vbox = QVBoxLayout()
        vbox.addWidget(lbl)
        vbox.addWidget(timeedit)
        vbox.addStretch()

        self.setLayout(vbox)

        self.setWindowTitle('QTimeEdit')
        self.setGeometry(300, 300, 300, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

The Edit Time widget (QTimeEdit) appears in the window.

qtimeedit

Related course: Create Desktop Apps with Python PyQt5

Description

timeedit = QTimeEdit(self)
timeedit.setTime(QTime.currentTime())
timeedit.setTimeRange(QTime(3, 00, 00), QTime(23, 30, 00))

Use the QTimeEdit class to create a timeedit.

Enter QTime.currentTime() in the setTime method so that it is displayed as the current time when the program runs.

SetTimeRange allows you to limit the range of time you can choose.

The minimum time is set to 00:00:00 00 seconds 000 milliseconds by default, and the maximum time is set to 23:59:59 999 milliseconds.

timeedit.setDisplayFormat('hh:mm:ss')

Using the setDisplayFormat method, I set the time to appear in the form of 'hh:mm:ss'.

vbox = QVBoxLayout()
vbox.addWidget(lbl)
vbox.addWidget(timeedit)
vbox.addStretch()

self.setLayout(vbox)

Use the vertical box layout to place the label and time edit widget vertically and set it as the layout of the entire widget.

Related course: Create Desktop Apps with Python PyQt5