PyQt5_QDateTimeEdit_日期时间步长调节编辑器


from PyQt5.Qt import *

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QDateTimeEdit_日期时间步长调节编辑器')
        self.resize(800,800)
        self.iniUI()

    def iniUI(self):
        #创建日期时间对象,
        dte1 = QDateTimeEdit(self)
        dte2 = QDateTimeEdit(QDateTime.currentDateTime(),self)
        dte3 = QDateTimeEdit(QDateTime.currentDateTime(),self)
        dte3.setMaximumDateTime(QDateTime.currentDateTime().addYears(100))#设置最大日期,100年后
        dte3.setMinimumDateTime(QDateTime.currentDateTime().addMonths(-60))#设置最小日期,60个月之前

        dt4 = QDateTime(1996, 12, 28, 23, 59, 58)      #创建日期时间对象
        dte4 = QDateTimeEdit(dt4,self)              #创建日期时间对象的编辑器

        de = QDateTimeEdit(QDate.currentDate(),self)
        te = QDateTimeEdit(QTime.currentTime(),self)

        # dt4 = dt4.addMonths(10)
        dte1.setCalendarPopup(True)  # 弹出日历

        self.dte1 = dte1
        self.dte2 = dte2
        self.dte3 = dte3
        self.dte4 = dte4
        self.de = de
        self.te = te
        self.dte1.setAccelerated(True)
        self.dte3.setAccelerated(True)
        dte1.resize(self.width() * 9 / 12, self.height() / 12)
        dte2.resize(self.width() * 9 / 12, self.height() / 12)
        dte3.resize(self.width() * 9 / 12, self.height() / 12)
        dte4.resize(self.width() * 9 / 12, self.height() / 12)

        de.resize(self.width() * 9 / 12, self.height() / 12)
        te.resize(self.width() * 9 / 12, self.height() / 12)
        dte1.move((self.width() - dte1.width()) / 2, (self.height() - dte1.height()) * 1 / 7)
        dte2.move((self.width() - dte2.width()) / 2, (self.height() - dte2.height()) * 2 / 7)
        dte3.move((self.width() - dte3.width()) / 2, (self.height() - dte2.height()) * 3 / 7)
        dte4.move((self.width() - dte3.width()) / 2, (self.height() - dte2.height()) * 4 / 7)

        de.move((self.width() - de.width()) / 2, (self.height() - de.height()) * 5 / 7)
        te.move((self.width() - te.width()) / 2, (self.height() - te.height()) * 6 / 7)

        btn = QPushButton(self)
        self.btn = btn
        self.btn_w = self.width() / 3
        self.btn_h = self.height() * 3 / 32
        self.btn.resize(self.btn_w, self.btn_h)
        self.btn_x = (self.width() - self.btn_w) / 2
        self.btn_y = self.height() * 7 / 8 + (self.height() / 8 - self.btn_h) / 2
        self.btn.setText('测试按钮')
        self.btn.setStyleSheet('font-size:30px')
        self.btn.move(self.btn_x, self.btn_y)

        self.btn.clicked.connect(self.btn_test)

        self.dte3.dateChanged.connect(lambda date: print(date))
        self.dte3.timeChanged.connect(lambda time: print(time))
        self.dte3.dateTimeChanged.connect(lambda datetime: print(datetime))
        ##############################################设置日期时间对象的 格式与分隔符
        #
        # yy                两位数的年
        # yyyy              四位数 年
        # M                 1-12
        # MM                01-12
        # MMM               Jac-Dec
        # MMMM              1月-12月
        # d                 1-31
        # dd                01-31
        # ddd               Mon-Sun
        # dddd              星期一-星期日

        dte1.setDisplayFormat('yyyy:MM/dd HH/mm.ss')
        dte2.setDisplayFormat('yyyy-MM-dd HH:mm:ss')
        dte3.setDisplayFormat('yyyy-MM-dd HH:mm:ss')
        de.setDisplayFormat('yyyy-MM-dd')
        te.setDisplayFormat('HH:mm:ss')
        #############################设置日期时间对象的格式与分隔符


        ##############################################计算两个日期的时间差


        dt5 = QDateTime.currentDateTime()
        print('我已经存在于这世界:', dt4.secsTo(dt5), '秒')
        #############################计算两个日期的时间差


        ####################################################################################时间日期对象 Section控制,
        #
        # 现在需求:
        #           时间日期对象 Section控制,
        #           获取时间日期对象  针对某一特定时间单位的 操作
        # 解决方法:
        #           section
        #  dte4.sectionText(  某一特定的section   )  获取特定的时间单位数值,哪一年 哪一天
        # 某一特定的section 的表示方法:
        #                       1,dte4.currentSection()
        #                       2,dte4.sectionAt(2) #日         年月日时分秒 012345
        #                       3,QDateTimeEdit.DaySection
        print(dte4.sectionText(dte4.sectionAt(2)))
        print(dte4.sectionText(QDateTimeEdit.DaySection))
        print('日期时间对象编辑器dte4的section个数为:',  dte4.sectionCount() )#即 年月日时分 五个

        ##############################################设置当前操作的 section 即设置焦点在哪一个section上面
    #     设置当前的激活 section
    #     dte.setCurrentSection(QDateTimeEdit.HourSection)

    def datetimeSetCurrentSection(self):
        self.dte4.setFocus()
        self.dte4.setCurrentSection(QDateTimeEdit.HourSection)
        ############################## 设置当前操作的 section 即设置焦点在哪一个section上面
        ################################################################################时间日期对象 Section控制




    ##############################################获取日期时间
    #
    # self.dte4.dateTime()              QDateTime对象
    # self.dte4.dateTime().date()       QDate对象
    # self.dte4.dateTime().time()       QTime对象
    #
    #
    def datetimeDateTime(self):
        print(  self.dte4.dateTime().date().year() )
        print(  self.dte4.dateTime().date().month() )
        print(  self.dte4.dateTime().date().day() )
        print(  self.dte4.dateTime().date().dayOfWeek() )#周几
        print(  self.dte4.dateTime().date().daysInMonth())#当天所在月份总天数
        print(  self.dte4.dateTime().date().daysInYear() )#当天所在年份总天数
        print(  self.dte4.dateTime().date().day() )#当月第几日
        print(  self.dte4.dateTime().time().hour())
        print(  self.dte4.dateTime().time().minute())
        print(  self.dte4.dateTime().time().second())

    #############################获取日期时间

    def btn_test(self):
        self.datetimeSetCurrentSection()
        self.datetimeDateTime()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)


    win = MyWindow()
    win.show()
    sys.exit(app.exec_())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值