返回当前日期和时间设置

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

UI文件

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

最新文章

  1. sizzle分析记录:属性选择器
  2. 安装 SciPy 和 scikit-learn 升级pip 及pip基本命令表
  3. 如何从oc中去获取一个私有的变量.....
  4. 常用的phpstorm设置
  5. CentOS 6.0 设置IP地址、网关、DNS
  6. js清空前后空格
  7. 机房收费系统之vb报表的模板的制作(一)
  8. java中线程机制
  9. zepto.js的基本介绍与使用
  10. vue-router+elelment-ui,实现导航栏激活高亮
  11. jQuery-3.事件篇---自定义事件
  12. 什么是车辆识别代码(VIN)
  13. Dell Venue 8 Pro启动盘UEFI模式32位启动,备份系统
  14. mybatis BindingException: Invalid bound statement (not found)
  15. WEBBASE篇: 第五篇, CSS知识3
  16. jquery.form.js实现将form提交转为ajax方式提交的方法
  17. 一个.net程序客户端更新方案
  18. ArcGIS Desktop水文计算
  19. Android 搭建ssh服务
  20. AIX解压ZIP文件

热门文章

  1. 解决16bit压缩贴图失真问题
  2. U3D调用7z解压文件
  3. 使用js是想防止表单重复提交的效果
  4. 【H5】使用h5实现复制粘贴功能
  5. 【转】C/C++函数调用过程分析
  6. 【大数据系列】hadoop集群设置官方文档翻译
  7. 法律&道德
  8. centos6上使用xfs文件系统
  9. 为Docker容器中运行的gitlab添加ssh的一些问题记录
  10. 23种设计模式之组合模式(Composite)