050.PyQt5_QDateTimeEdit_日期时间步长调节器

QDateTimeEdit 日期时间步长调节器

  • 描述

    • 编辑日期和时间的单行文本框
    • 既可以用箭头调节, 也可以用键盘编辑输入
    • 可以单独调节某个部分
  • 继承自:QAbstractSpinBox

  • 功能作用

  • 构造函数

    • 根据指定日期或时间,创建出日期时间编辑器控件并会初始化该控件展示的section
    • section:控件中的每一个单独元素。如:年、月、日、时、分、秒等都是单独元素都是一个section
    QDateTimeEdit(parent: QWidget = None)                                           # 创建对象的时候设置父对象
    QDateTimeEdit(Union[QDateTime, datetime.datetime], parent: QWidget = None)      # 创建对象的时候通过QDateTime对象设置默认日期时间,并且设置父对象
    QDateTimeEdit(Union[QDate, datetime.date], parent: QWidget = None)              # 创建对象的时候通过QDate对象设置默认日期,并且设置父对象
    QDateTimeEdit(Union[QTime, datetime.time], parent: QWidget =None)               # 创建对象的时候通过QTime对象设置默认时间,并且设置父对象
    
    • QDateTime对象、QDate对象、QTime对象详细讲解
  • 显示格式

    setDisplayFormat(format_str)                    # 设置日期时间显示格式
    displayFormat() -> str                          # 获取日期时间显示格式
    
    • format_str 日期时间格式符
      格式 说明
      日期格式
      d 没有前导零的数字的日期(1到31)
      dd 有前导零的数字的日期(01到31)
      ddd 缩写的本地化日期名称(例如'Mon'到'Sun'
      dddd 完整本地化的日期名称(例如“星期一”到“星期日”)
      M 没有前导零的数字的月份(1-12)
      MM 月份为前导零的数字(01-12)
      MMM 缩写的本地化月份名称(例如'Jan'到'Dec')
      MMMM 完整的本地化月份名称(例如“1月”到“12月”)
      yy 年份为两位数字(00-99)
      yyyy 年份为四位数字
      时间格式
      h 没有前导零的小时(如果显示AM / PM,则为0到23或1到12)
      hh 前导零的小时(如果AM / PM显示,则为00到23或01到12)
      H 没有前导零的小时(0到23,即使有AM / PM显示)
      HH 前导零的小时(00到23,即使有AM / PM显示)
      m 没有前导零的分钟(0到59)
      mm 前导零(00到59)的分钟
      s 整个秒没有前导零(0到59)
      ss 带有前导零(00到59)
      z 第二个小数部分, 没有尾随零的毫秒(0到999)
      zzz 第二个小数部分, 有尾随零的毫秒(000到999)
      AP / A 使用AM / PM显示
      ap / a 使用am / pm显示
      t 时区
  • section控制

    • section:控件中的每一个单独元素。如:年、月、日、时、分、秒等都是单独元素都是一个section
    • 设置要操作的section时,要先让QDateTimeEdit对象获取光标在设置要操作的section
    • 如果先设置要操作的section,再设置QDateTimeEdit对象获取光标,系统会重置要操作的section为第一个
    sectionCount() -> int                           # 获取section个数
    
    setCurrentSectionIndex(int)                     # 通过索引设置当前操作的section
    currentSectionIndex() -> int                    # 获取当前操作的section索引
    
    setCurrentSection(QDateTimeEdit.Section)        # 通过QDateTimeEdit对象的枚举值设置当前操作的sction
    currentSection() -> QDateTimeEdit.Section       # 获取当前操作的section,返回QDateTimeEdit枚举值
    
    sectionAt(index_int) -> QDateTimeEdit.Section   # 获取指定索引位置的section,返回枚举值
    sectionText(QDateTimeEdit.Section) -> str       # 获取指定section的文本内容
    
    # 参数  QDateTimeEdit.Section
    # 枚举值      # section对象                     # 说明
        0    QDateTimeEdit.NoSection                # 没有section
        1    QDateTimeEdit.AmPmSection              # AM/PMsection
        2    QDateTimeEdit.MSecSection              # 毫秒section
        4    QDateTimeEdit.SecondSection            # 秒section
        8    QDateTimeEdit.MinuteSection            # 分钟section
       16    QDateTimeEdit.HourSection              # 小时section
      256    QDateTimeEdit.DaySection               # 天section
      512    QDateTimeEdit.MonthSection             # 月section
     1024    QDateTimeEdit.YearSection              # 年section
    
  • 最大和最小日期时间

    # 日期时间
        setMaximumDateTime(QDateTime)                   # 设置最大日期时间,默认9999年12月31日 23:59:59 999毫秒
        maximumDateTime() -> QDateTime                  # 获取最大日期时间
        clearMaximumDateTime()                          # 清除自定义最大日期时间
        
        setMinimumDateTime(QDateTime)                   # 设置最小日期时间
        minimumDateTime() -> QDateTime                  # 获取最小日期时间
        clearMinimumDateTime()                          # 清除自定义最小日期时间
        
        setDateTimeRange(min_datetime, max_datetime)    # 设置一个日期时间范围
    
    # 日期
        setMaximumDate(QDate)                           # 设置最大日期,默认包含9999年12月31日
        maximumDate() -> QDate                          # 获取最大日期
        clearMaximumDate()                              # 清除自定义最大日期, 恢复默认
        
        setMinimumDate(QDate)                           # 设置最小日期,默认1752年9月14日
        minimumDate() -> QDate                          # 获取最小日期
        clearMinimumDate()                              # 清除自定义最小日期, 恢复默认
        
        setDateRange(min_date, max_date)                # 设置一个日期范围
    
    # 时间          
        setMaximumTime(QTime)                           # 设置最大时间
        maximumTime() -> QTime                          # 获取最大时间
        clearMaximumTime()                              # 清除自定义最大时间
        
        setMinimumTime(QTime)                           # 设置最小时间
        minimumTime() -> QTime                          # 获取最小时间
        clearMinimumTime()                              # 清除自定义最小时间
        
        setTimeRange(min_time, max_time)                # 设置一个时间范围
    
  • 日历选择控件

    • 通过日历选择控件, 快速的让用户输入日期
    setCalendarPopup(bool)                              # 设置是否弹出日历选择控件
    calendarPopup()                                     # 获取是否弹出日历选择控件
    
    setCalendarWidget(QCalendarWidget)                  # 设置自定义日历选择控件
    calendarWidget() -> QCalendarWidget                 # 获取自定义日历选择控件
    
  • 获取日期和时间对象

    dateTime() -> QDateTime                             # 获取日期和时间,返回QDateTime对象
    date() -> QDate                                     # 获取日期,返回QDate对象
    time() -> QTime                                     # 获取时间,返回QTime对象
    
    # 也可以通过sectionText(QDateTimeEdit.section)来获取指定section的文本内容
    
  • 可用信号

    dateTimeChanged(QDateTime datetime)                 # 日期时间发生改变时发射信号,传递QDateTime对象
    dateChanged(QDate date)                             # 日期发生改变时发射信号,传递QDate对象
    timeChanged(QTime time)                             # 时间发生改变时发射信号,传递QTime对象
    


相关子类:QDateEdit 日期步长调节器

  • 描述
    • 基于QDateTimeEdit控件的小控件,主要操作日期部分
  • 继承自:QDateTimeEdit
  • 功能作用
  • 构造函数
    QDateEdit(QWidget *parent = nullptr)
    QDateEdit(const QDate &date, QWidget *parent = nullptr)
    
  • 显示格式
    setDisplayFormat(format_str)            # 设置日期时间显示格式
    displayFormat() -> str                  # 获取日期时间显示格式
    
  • 设置最大和最小日期
    setMaximumDate(QDate)                   # 设置最大日期,默认包含9999年12月31日
    maximumDate() -> QDate                  # 获取最大日期
    clearMaximumDate()                      # 清除自定义最大日期, 恢复默认
    
    setMinimumDate(QDate)                   # 设置最小日期,默认1752年9月14日
    minimumDate() -> QDate                  # 获取最小日期
    clearMinimumDate()                      # 清除自定义最小日期, 恢复默认
    
    setDateRange(min_date, max_date)        # 设置日期范围
    
  • 日历选择控件
    • 通过日历选择控件, 快速的让用户输入日期
    setCalendarPopup(bool)                              # 设置是否弹出日历选择控件
    calendarPopup()                                     # 获取是否弹出日历选择控件
    
    setCalendarWidget(QCalendarWidget)                  # 设置自定义日历选择控件
    calendarWidget() -> QCalendarWidget                 # 获取自定义日历选择控件
    
  • 获取日期
    date() -> QDate                         # 获取日期,返回QDate对象
    
  • 信号
    • 继承父类

相关子类:QTimeEdit 时间步长调节器

  • 描述
    • 基于QDateTimeEdit控件的小控件,主要操作时间部分
  • 继承自:QDateTimeEdit
  • 功能作用
  • 构造函数
    QTimeEdit(QWidget *parent = nullptr)
    QTimeEdit(const QTime &time, QWidget *parent = nullptr)
    
  • 显示格式
    setDisplayFormat(format_str)            # 设置日期时间显示格式
    displayFormat() -> str                  # 获取日期时间显示格式
    
  • 设置最大/最小时间
    setMaximumTime(QTime)                   # 设置最大时间
    maximumTime() -> QTime                  # 获取最大时间
    clearMaximumTime()                      # 清除最大时间
    
    setMinimumTime(QTime)                   # 设置最小时间
    minimumTime() -> QTime                  # 获取最小时间
    clearMinimumTime()                      # 清除最小时间
    
    setTimeRange(min_time, max_time)        # 设置时间范围
    
  • 获取时间
    time() -> QTime                         # 获取时间,返回QTime对象
    
  • 信号
    • 继承父类


  • 示例代码
  • 示例1:QDateTimeEdit创建使用
    from PyQt5.Qt import *
    import sys
    
    class Windows(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle('QDateTimeEdit-创建使用')
            self.resize(500, 500)
            self.widget_list()
    
        def widget_list(self):
            self.add_widget()
    
        def display_format(self):
            # 设置显示格式
            self.dte2.setDisplayFormat('yyyy/MM/dd HH:mm:ss')
            self.dte3.setDisplayFormat('yyyy/MM/dd dddd')
            self.dte4.setDisplayFormat('yyyy/MM/dd MMMM dddd')
    
        def operation_section(self):
            # section操作
            print('section个数:', self.dte2.sectionCount())   # 获取section个数
            print('section个数:', self.dte3.sectionCount())
            print('section个数:', self.dte4.sectionCount())
    
            # 通过索引设置要操作的section
            # self.dte2.setFocus()            # 需要先让QDateTimeEdit对象获取光标在设置要操作的section
            # self.dte2.setCurrentSectionIndex(3)
    
            # 通过QDateTimeEdit对象的枚举值设置当前操作的sction
            # self.dte3.setFocus()
            # self.dte3.setCurrentSection(QDateTimeEdit.DaySection)
    
            # 获取指定索引位置的section,返回QDateTimeEdit对象枚举值
            # print(self.dte4.sectionAt(0))
    
            # 获取指定section的值
            print(self.dte2.sectionText(QDateTimeEdit.YearSection))
    
        def set_range(self):
            # 设置最大最小取值范围
            # self.dte2.setMaximumDateTime(QDateTime.currentDateTime())       # 设置最大取值为当天日期时间
            # self.dte2.setMinimumDateTime(QDateTime(2020, 1, 1, 0, 0, 0))    # 设置自小取值为2020年1月1日 0时0分0秒
            # 设置取值范围为当天日期 前后3天
            self.dte2.setDateTimeRange(QDateTime.currentDateTime().addDays(-3),QDateTime.currentDateTime().addDays(3))
    
            # self.dte3.setMaximumDate(QDate.currentDate())
            # self.dte3.setMinimumDate(QDate(2020,1,1))
            self.dte3.setDateRange(QDate.currentDate().addDays(-3), QDate.currentDate().addDays(3))
    
            # self.dte4.setMaximumTime(QTime.currentTime())
            # self.dte4.setMinimumTime(QTime(0, 0, 0))
            self.dte4.setTimeRange(QTime.currentTime().addSecs(-(3600 * 3)),QTime.currentTime().addSecs((3600 * 2)))
    
        def calendar(self):
            # 设置是否开启日历选择控件
            self.dte3.setCalendarPopup(True)
    
            print(self.dte3.calendarPopup())
            pass
    
        def get_datetime(self):
            # 获取日期和时间
            print(self.dte2.dateTime())
            print(self.dte3.date())
            print(self.dte4.time())
    
        def add_widget(self):
            dte1 = QDateTimeEdit(self)                                  # 创建一个空QDateTimeEdit对象
            dte2 = QDateTimeEdit(QDateTime.currentDateTime(), self)     # 创建QDateTimeEdit对象的同时,通过QDateTime对象设置日期和时间
            dte3 = QDateTimeEdit(QDate.currentDate(), self)             # 创建QDateTimeEdit对象的同时,通过QDate对象设置日期
            dte4 = QDateTimeEdit(QTime.currentTime(), self)             # 创建QDateTimeEdit对象的同时,通过Time对象设置时间
    
            self.dte1 = dte1
            self.dte2 = dte2
            self.dte3 = dte3
            self.dte4 = dte4
    
            w = 200
            h = 30
            dte1.resize(w, h)
            dte2.resize(w, h)
            dte3.resize(w, h)
            dte4.resize(w, h)
    
            dte1.move(50, 50)
            dte2.move(50, 100)
            dte3.move(50, 150)
            dte4.move(50, 200)
    
            btn = QPushButton('显示格式', self)
            btn.resize(150, 30)
            btn.move(50, 250)
            btn.clicked.connect(self.display_format)
    
            btn1 = QPushButton('section操作', self)
            btn1.resize(150, 30)
            btn1.move(210, 250)
            btn1.clicked.connect(self.operation_section)
    
            btn2 = QPushButton('设置最大最小值', self)
            btn2.resize(150, 30)
            btn2.move(50, 300)
            btn2.clicked.connect(self.set_range)
    
            btn3 = QPushButton('日期选择控件', self)
            btn3.resize(150, 30)
            btn3.move(210, 300)
            btn3.clicked.connect(self.calendar)
    
            btn4 = QPushButton('获取日期时间', self)
            btn4.resize(150, 30)
            btn4.move(50, 350)
            btn4.clicked.connect(self.get_datetime)
    
            # ****************可用信号****************** 开始
            dte1.dateTimeChanged.connect(lambda val: print('日期时间发生改变', val))
            dte1.dateChanged.connect(lambda val: print('日期发生改变', val))
            dte1.timeChanged.connect(lambda val: print('时间发生改变', val))
    
            # ****************可用信号****************** 结束
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = Windows()
    
        window.show()
        sys.exit(app.exec_())
    
  • 示例2:QDateEdit创建使用
    from PyQt5.Qt import *
    import sys
    
    class Windows(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle('QDateEdit-创建使用')
            self.resize(500, 500)
            self.widget_list()
    
        def widget_list(self):
            self.add_widget()
    
    
        def add_widget(self):
            de = QDateEdit(self)
            de1 = QDateEdit(QDate(2023, 3, 13), self)
            de2 = QDateEdit(QDate.currentDate(), self)
    
            de.setDisplayFormat('yyyy-MM-dd')
            de1.setDisplayFormat('yyyy年M月d日')
            de2.setDisplayFormat('yyyy/MM/dd dddd')
    
            de.resize(300, 30)
            de1.resize(300, 30)
            de2.resize(300, 30)
    
            de.move(50, 50)
            de1.move(50, 100)
            de2.move(50, 150)
    
            # ****************设置取值范围****************** 开始
            # de1.setMaximumDate(QDate(2023, 5, 1))
            # de1.setMinimumDate(QDate(2023, 1, 1))
            de1.setDateRange(QDate(2023, 1, 1), QDate.currentDate().addDays(30))
            # ****************设置取值范围****************** 结束
    
            print(de1.date())
    
            # 设置开启日历选择控件
            de1.setCalendarPopup(True)
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = Windows()
    
        window.show()
        sys.exit(app.exec_())
    
  • 示例3:QTimeEdit创建使用
    from PyQt5.Qt import *
    import sys
    
    class Windows(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle('QTimeEdit-创建使用')
            self.resize(500, 500)
            self.widget_list()
    
        def widget_list(self):
            self.add_widget()
    
    
        def add_widget(self):
            te = QTimeEdit(self)
            te1 = QTimeEdit(QTime(8, 12, 12), self)
            te2 = QTimeEdit(QTime.currentTime(), self)
    
            te.resize(200, 30)
            te1.resize(200, 30)
            te2.resize(200, 30)
    
            te.move(50, 50)
            te1.move(50, 100)
            te2.move(50, 150)
    
            # ****************显示格式设置****************** 开始
            te.setDisplayFormat('HH:mm:ss')
            te1.setDisplayFormat('A HH:mm:ss')
            te2.setDisplayFormat('A HH:mm:ss')
            # ****************显示格式设置****************** 结束
    
            # ****************设置取值范围****************** 开始
            te.setMaximumTime(QTime(22,59,59))
            te.setMinimumTime(QTime(1,1,1))
            # te.setTimeRange(QTime(2,2,2), QTime(22,59,59))
    
            # ****************设置取值范围****************** 结束
    
            print(te1.time())
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = Windows()
    
        window.show()
        sys.exit(app.exec_())

你可能感兴趣的:(PyQt5,qt5,pyqt,python,qt,开发语言)