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())