python pyqt5 QDateTimeEdit 常用

setDisplayFormat() yyyy MM dd HH mm ss
setMinimumDate() setMaximumDate()
time() 返回編輯的時(shí)間 date() 返回編輯的日期


image.png
image.png

-- coding: utf-8 --

'''
【簡(jiǎn)介】
PyQt5中 DateTimeEdit 例子

'''

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDate, QDateTime, QTime

class DateTimeEditDemo(QWidget):
def init(self):
super(DateTimeEditDemo, self).init()
self.initUI()

def initUI(self):
    self.setWindowTitle('QDateTimeEdit例子')
    self.resize(300, 90)

    vlayout = QVBoxLayout()
    self.dateEdit = QDateTimeEdit(QDateTime.currentDateTime(), self)
    self.dateEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
    # 設(shè)置最小日期
    self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365))
    # 設(shè)置最大日期
    self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365))
    self.dateEdit.setCalendarPopup(True)

    self.dateEdit.dateChanged.connect(self.onDateChanged)
    self.dateEdit.dateTimeChanged.connect(self.onDateTimeChanged)
    self.dateEdit.timeChanged.connect(self.onTimeChanged)

    self.btn = QPushButton('獲得日期和時(shí)間')
    self.btn.clicked.connect(self.onButtonClick)

    vlayout.addWidget(self.dateEdit)
    vlayout.addWidget(self.btn)
    self.setLayout(vlayout)

# 日期發(fā)生改變時(shí)執(zhí)行
def onDateChanged(self, date):
    print(date)

# 無(wú)論日期還是時(shí)間發(fā)生改變,都會(huì)執(zhí)行
def onDateTimeChanged(self, dateTime):
    print(dateTime)

# 時(shí)間發(fā)生改變時(shí)執(zhí)行
def onTimeChanged(self, time):
    print(time)

def onButtonClick(self):
    dateTime = self.dateEdit.dateTime()
    # 最大日期
    maxDate = self.dateEdit.maximumDate()
    # 最大日期時(shí)間
    maxDateTime = self.dateEdit.maximumDateTime()
    # 最大時(shí)間
    maxTime = self.dateEdit.maximumTime()
    # 最小日期
    minDate = self.dateEdit.minimumDate()
    # 最小日期時(shí)間
    minDateTime = self.dateEdit.minimumDateTime()
    # 最小時(shí)間
    minTime = self.dateEdit.minimumTime()

    print('\n選擇日期時(shí)間')
    print('dateTime=%s' % str(dateTime))
    print('maxDate=%s' % str(maxDate))
    print('maxDateTime=%s' % str(maxDateTime))
    print('maxTime=%s' % str(maxTime))
    print('minDate=%s' % str(minDate))
    print('minDateTime=%s' % str(minDateTime))
    print('minTime=%s' % str(minTime))

if name == 'main':
app = QApplication(sys.argv)
demo = DateTimeEditDemo()
demo.show()
sys.exit(app.exec_())

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容