第八篇 -- 日期时间数据

效果图:

ui_date_time.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_date_time.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(683, 391)
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setGeometry(QtCore.QRect(0, 20, 331, 361))
        self.groupBox.setObjectName("groupBox")
        self.btnGetTime = QtWidgets.QPushButton(self.groupBox)
        self.btnGetTime.setGeometry(QtCore.QRect(70, 30, 121, 31))
        font = QtGui.QFont()
        font.setPointSize(8)
        self.btnGetTime.setFont(font)
        self.btnGetTime.setObjectName("btnGetTime")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(30, 90, 31, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.timeEdit = QtWidgets.QTimeEdit(self.groupBox)
        self.timeEdit.setGeometry(QtCore.QRect(70, 90, 121, 22))
        self.timeEdit.setTime(QtCore.QTime(0, 0, 0))
        self.timeEdit.setObjectName("timeEdit")
        self.label_2 = QtWidgets.QLabel(self.groupBox)
        self.label_2.setGeometry(QtCore.QRect(30, 180, 31, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.dateEdit = QtWidgets.QDateEdit(self.groupBox)
        self.dateEdit.setGeometry(QtCore.QRect(70, 180, 121, 22))
        self.dateEdit.setObjectName("dateEdit")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(10, 270, 71, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.editTime = QtWidgets.QLineEdit(self.groupBox)
        self.editTime.setGeometry(QtCore.QRect(210, 90, 111, 20))
        self.editTime.setObjectName("editTime")
        self.btnSetTime = QtWidgets.QPushButton(self.groupBox)
        self.btnSetTime.setGeometry(QtCore.QRect(210, 130, 111, 23))
        self.btnSetTime.setObjectName("btnSetTime")
        self.editDate = QtWidgets.QLineEdit(self.groupBox)
        self.editDate.setGeometry(QtCore.QRect(210, 180, 111, 20))
        self.editDate.setObjectName("editDate")
        self.btnSetDate = QtWidgets.QPushButton(self.groupBox)
        self.btnSetDate.setGeometry(QtCore.QRect(210, 220, 111, 23))
        self.btnSetDate.setObjectName("btnSetDate")
        self.editDateTime = QtWidgets.QLineEdit(self.groupBox)
        self.editDateTime.setGeometry(QtCore.QRect(210, 270, 111, 20))
        self.editDateTime.setObjectName("editDateTime")
        self.btnSetDateTime = QtWidgets.QPushButton(self.groupBox)
        self.btnSetDateTime.setGeometry(QtCore.QRect(210, 310, 111, 23))
        self.btnSetDateTime.setObjectName("btnSetDateTime")
        self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.groupBox)
        self.dateTimeEdit.setGeometry(QtCore.QRect(70, 270, 121, 22))
        self.dateTimeEdit.setObjectName("dateTimeEdit")
        self.groupBox_2 = QtWidgets.QGroupBox(Form)
        self.groupBox_2.setGeometry(QtCore.QRect(340, 20, 331, 361))
        self.groupBox_2.setObjectName("groupBox_2")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.groupBox_2)
        self.calendarWidget.setGeometry(QtCore.QRect(10, 80, 312, 261))
        self.calendarWidget.setObjectName("calendarWidget")
        self.label_4 = QtWidgets.QLabel(self.groupBox_2)
        self.label_4.setGeometry(QtCore.QRect(30, 40, 91, 16))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.editCalendar = QtWidgets.QLineEdit(self.groupBox_2)
        self.editCalendar.setGeometry(QtCore.QRect(140, 40, 171, 20))
        self.editCalendar.setObjectName("editCalendar")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.groupBox.setTitle(_translate("Form", "日期时间"))
        self.btnGetTime.setText(_translate("Form", "读取当前日期时间"))
        self.label.setText(_translate("Form", "时 间"))
        self.timeEdit.setDisplayFormat(_translate("Form", "H:mm:ss"))
        self.label_2.setText(_translate("Form", "日 期"))
        self.label_3.setText(_translate("Form", "日期时间"))
        self.editTime.setInputMask(_translate("Form", "99:99:99;_"))
        self.btnSetTime.setText(_translate("Form", "设置时间"))
        self.editDate.setInputMask(_translate("Form", "9999-99-99"))
        self.btnSetDate.setText(_translate("Form", "设置日期"))
        self.editDateTime.setInputMask(_translate("Form", "9999-99-99 99:99:99"))
        self.btnSetDateTime.setText(_translate("Form", "设置时间日期"))
        self.dateTimeEdit.setDisplayFormat(_translate("Form", "yyyy/M/d H:mm:ss"))
        self.groupBox_2.setTitle(_translate("Form", "日历选择"))
        self.label_4.setText(_translate("Form", "选择的日期:"))
View Code

 

myWidget_time_date.py

#!/usr/bin/env python
# _*_ coding: UTF-8 _*_
"""=================================================
@Project -> File    : Operate-system -> myWidget.py
@IDE     : PyCharm
@Author  : zihan
@Date    : 2020/4/9 15:33
@Desc    :
================================================="""

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QDateTime, QDate, QTime
from ui_date_time import Ui_Form


class QmyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)  # 调用父类构造函数,创建窗体
        self.ui = Ui_Form()  # 创建UI对象
        self.ui.setupUi(self)  # 构造UI

    # # ============由connectSlotByName() 自动关联的槽函数 ======================
    def on_btnGetTime_clicked(self):  # # 读取当前日期时间按钮
        curDateTime = QDateTime.currentDateTime()
        self.ui.timeEdit.setTime(curDateTime.time())
        self.ui.editTime.setText(curDateTime.toString("hh:mm:ss"))
        self.ui.dateEdit.setDate(curDateTime.date())
        self.ui.editDate.setText(curDateTime.toString("yyyy-MM-dd"))
        self.ui.dateTimeEdit.setDateTime(curDateTime)
        self.ui.editDateTime.setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"))

    def on_calendarWidget_selectionChanged(self):
        date = self.ui.calendarWidget.selectedDate()
        self.ui.editCalendar.setText(date.toString("yyyy年M月d日"))

    def on_btnSetTime_clicked(self):  # 设置时间按钮
        tmStr = self.ui.editTime.text()
        tm = QTime.fromString(tmStr, "hh:mm:ss")
        self.ui.timeEdit.setTime(tm)

    def on_btnSetDate_clicked(self):  # 设置日期按钮
        dtStr = self.ui.editDate.text()
        dt = QDate.fromString(dtStr, "yyyy-MM-dd")
        self.ui.dateEdit.setDate(dt)

    def on_btnSetDateTime_clicked(self):
        dttmStr = self.ui.editDateTime.text()
        dttm = QDateTime.fromString(dttmStr, "yyyy-MM-dd hh:mm:ss")
        self.ui.dateTimeEdit.setDateTime(dttm)


if __name__ == "__main__":
    app = QApplication(sys.argv)  # 创建app,用QApplication类
    form = QmyWidget()
    form.show()
    sys.exit(app.exec_())
View Code

 

 

解析:

类:

QTime:时间数据类型,仅表示时间。displayFormat设置为H:mm:ss。如15:21:13

QDate:日期数据类型,仅表示日期。如2018-5-6

QDateTime:日期时间数据类型,表示日期和时间,如2018-05-23 09:12:43

组件:

QTimeEdit:编辑和显示时间的组件

QDateEdit:编辑和显示日期的组件。若calendarPopup属性设置为True,运行时右侧按钮变成下拉按钮,单击按钮时出现一个日历选择框,用于在日历上选择日期。

QDateTimeEdit:编辑和显示日期时间的组件,也有calendarPopup属性。

QCalendarWidget:一个用日历形式选择日期的组件,在日历上点击日期发生变化时发射信号selectionChanged(),可响应此信号读取选择的日期。

 

在将字符串转换为日期时间数据时,需要字符串具有指定的格式,为了限定QLineEdit的输入符合某些格式,可以设置其inputMask属性

组件 inputMask属性 意义
editTime 99:99:99;_ 只能输入0到9的数字,空格用“_”显示
editDate 9999-99-99 只能输入0到9的数字
editDateTime 9999-99-99 99:99:99 只能输入0到9的数字

 

ok.

 

posted @ 2020-04-10 08:56  o云淡风轻o  阅读(283)  评论(0编辑  收藏  举报