【pyqt5】QdateTimeEdit(日期时间)
返回当前日期和时间设置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from PyQt5 import QtCore, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.setWindowModality(QtCore.Qt.WindowModal) Dialog.resize(690, 437) Dialog.setAutoFillBackground(True) Dialog.setSizeGripEnabled(True) self.calendarWidget = QtWidgets.QCalendarWidget(Dialog) self.calendarWidget.setGeometry(QtCore.QRect(230, 30, 431, 291)) self.calendarWidget.setObjectName("calendarWidget") self.dateEdit = QtWidgets.QDateEdit(Dialog) self.dateEdit.setGeometry(QtCore.QRect(30, 80, 171, 22)) self.dateEdit.setObjectName("dateEdit") self.dateTimeEdit = QtWidgets.QDateTimeEdit(Dialog) self.dateTimeEdit.setEnabled(True) self.dateTimeEdit.setGeometry(QtCore.QRect(10, 190, 211, 31)) self.dateTimeEdit.setToolTip("") self.dateTimeEdit.setWrapping(False) self.dateTimeEdit.setFrame(True) #边框可见 self.dateTimeEdit.setReadOnly(False) #设置只读模式 self.dateTimeEdit.setButtonSymbols(QtWidgets.QAbstractSpinBox.UpDownArrows) self.dateTimeEdit.setSpecialValueText("") self.dateTimeEdit.setKeyboardTracking(True) self.dateTimeEdit.setCurrentSection(QtWidgets.QDateTimeEdit.DaySection) self.dateTimeEdit.setCalendarPopup(True) self.dateTimeEdit.setCurrentSectionIndex(3) self.dateTimeEdit.setTimeSpec(QtCore.Qt.LocalTime) self.dateTimeEdit.setObjectName("dateTimeEdit") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())
main文件中设置(将获取当前日期时间的代码放在main文件中,避免QT设计师更改界面时 代码改动)
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets
from Ui_QDataTimeEdit import Ui_Dialog
class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.dateTimeEdit.setDateTime(QDateTime.currentDateTime())
self.dateEdit.setDate(QDate.currentDate())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Dialog()
ui.show()
sys.exit(app.exec_())
一些常用的技巧:
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.widget) self.dateTimeEdit.setFrame(False) #去除边框 self.dateTimeEdit.setDate(QtCore.QDate(2018, 10, 25)) #设置日期 self.dateTimeEdit.setTime(QtCore.QTime(11, 0, 0)) #设置时间11:00 self.dateTimeEdit.setObjectName("dateTimeEdit") #设置对象名称 """ 方式1: from PyQt5.QtCore import QDate,QTime ##设置为当前日期 self.dateTimeEdit.setDateTime(QDate.currentDate()) ##设置为当前时间 self.dateTimeEdit.setDateTime(QTime.currentTime()) 方式2: from PyQt5.QtCore import QDateTime ##设置当前日期和时间 self.dateTimeEdit.setDateTime(QDateTime.currentDateTime()) """ ##同理设置dataEdit是一样的 from PyQt5.QtCore import QDate self.dateEdit.setDate(QDate.currentDate())
打印出不同格式的日期和时间
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt """返回当前日期""" now = QDate.currentDate() print(now) #PyQt5.QtCore.QDate(2018, 12, 3) print(now.toString()) #周一 12月 3 2018 print(now.toString(Qt.ISODate)) #2018-12-03 print(now.toString(Qt.DefaultLocaleLongDate)) #2018年12月3日, 星期一 """返回当前日期和时间""" datetime = QDateTime.currentDateTime() print(datetime) #PyQt5.QtCore.QDateTime(2018, 12, 3, 15, 9, 41, 976) print(datetime.toString()) #周一 12月 3 15:09:41 2018 print(datetime.toString(Qt.ISODate)) #2018-12-03T15:09:41 print(datetime.toString(Qt.DefaultLocaleLongDate)) #2018年12月3日, 星期一 15:09:41 """返回当前时间""" time = QTime.currentTime() print(time) #PyQt5.QtCore.QTime(15, 12, 9, 980) print(time.toString()) #15:12:09 print(time.toString(Qt.ISODate)) #15:12:09 print(time.toString(Qt.DefaultLocaleLongDate)) #15:12:09