我想同时使用InputMask和Validator以正确的形式获得日期。在下面的代码中,我使用InputMask来接收格式为DD.MM.YYYY的日期。我不知道如何限制它的每个部分(DD,MM和YYYY),因为现在用户可以输入40.30.2020,理论上是正确的。
self.date = QLineEdit(self)
self.date.setInputMask("00.00.0000")
QDateTimeEdit
类该
QDateTimeEdit
类提供了一个用于编辑日期和时间的小组件。
import sys
from PyQt5.QtCore import QDate
from PyQt5.QtWidgets import (QApplication, QWidget, QDateTimeEdit,
QFormLayout, QLabel)
from PyQt5.QtGui import QFont
class Demo(QWidget):
def __init__(self):
super(Demo, self).__init__()
self.datetime = QDateTimeEdit(QDate.currentDate())
self.v_layout = QFormLayout(self)
self.v_layout.addRow(QLabel('DD.MM.YYYY'), self.datetime)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setFont(QFont("Times", 12, QFont.Bold))
demo = Demo()
demo.show()
sys.exit(app.exec_())