PyQt5 – QDateTimeEdit
QDateTimeEdit允许用户通过使用键盘或箭头键来增加和减少日期和时间值来编辑日期。箭头键可用于在 QDateTimeEdit 框中从一个部分移动到另一个部分。日期和时间按照设置的格式显示。下面是 QDateTimeEdit 的样子
例子 :
具有 QDateTimeEdit 小部件和标签的窗口以及每次日期时间小部件中的值更改标签时将显示当前日期时间。
下面是实现
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 500, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for components
def UiComponents(self):
# creating a QDateTimeEdit widget
datetime = QDateTimeEdit(self)
# setting geometry
datetime.setGeometry(100, 100, 150, 35)
# creating a label
label = QLabel("GeeksforGeeks", self)
# setting geometry to the label
label.setGeometry(100, 160, 200, 60)
# making label multi line
label.setWordWrap(True)
# adding action to the datetime
datetime.dateTimeChanged.connect(lambda: dt_method())
# method called by the datetime
def dt_method():
# getting current datetime
value = datetime.dateTime()
# setting text to the label
label.setText("Current date time : " + str(value))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :