In this PyQt5 article i want to show How To Create QTimeEdit In PyQt5 , the TimeEdit widget provides an editable box from which time value can be displayed and edited. for Creating QTimeEdit in PyQt5 we are using QTimeEdit class.
What is PyQt5 QTimeEdit
PyQt5 QTimeEdit is a widget provided by PyQt5, which allows users to input and display time values in user friendly way. it is graphical user interface (GUI) control that displays time input field that allows users to enter time values in hours, minutes, and seconds.
QTimeEdit widget allows users to adjust the time value by using up and down arrows, by typing the time value in the input field directly, or by selecting the value from a drop-down menu.
Also QTimeEdit widget provides properties and methods to set and retrieve the time value, and to format the displayed time according to specific formatting rules. this widget is commonly used in applications that require time input, such as scheduling, time tracking or event management applications.
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Psyide2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
So this is the complete code for How To Create QTimeEdit In PyQt5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTimeEdit from PyQt5.QtCore import QTime import sys class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Window" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.MyTime() self.show() def MyTime(self): vbox = QVBoxLayout() time = QTime() time.setHMS(13,15,40) timeedit = QTimeEdit() timeedit.setFont(QtGui.QFont("Sanserif", 15)) timeedit.setTime(time) vbox.addWidget(timeedit) self.setLayout(vbox) App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
This is our main window class that extends from QWidget and we initialize some window requirements like title and geometry, also we have added our IniWindow() method in this class.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 Window" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() |
In here we have created QVBoxLayout object, also we have created the object of QTime in here, so a QTime object contains a clock time, which it can express as the numbers of hours, minutes, seconds, and milliseconds since midnight. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds.
1 2 3 |
vbox = QVBoxLayout() time = QTime() time.setHMS(13,15,40) |
And this is our QTimeEdit, the QTimeEdit class provides a widget for editing times based on the QDateTimeEdit widget.
1 2 3 |
timeedit = QTimeEdit() timeedit.setFont(QtGui.QFont("Sanserif", 15)) timeedit.setTime(time) |
Also every PyQt5 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Run the complete code and this will be the result.
Watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
How do I display “seconds” in addition to hours and minutes?
just add this code:
time_edit.setDisplayFormat(“h:mm:ss AP”)