【问题标题】:PyQT5: How to use both QLineEdit: Validator and InputMask?PyQT5:如何同时使用 QLineEdit:验证器和 InputMask?
【发布时间】:2020-05-07 08:08:42
【问题描述】:

我想同时使用 InputMask 和 Validator 来获取正确格式的日期。在下面的代码中,我使用 InputMask 以 DD.MM.YYYY 格式接收日期。我不知道如何限制它的每个部分(DD、MM 和 YYYY),因为现在用户可以输入 40.30.2020,理论上是正确的。

self.date = QLineEdit(self)
self.date.setInputMask("00.00.0000")

【问题讨论】:

    标签: python pyqt pyqt5 qlineedit qvalidator


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多