python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5日期時(shí)間控件QDateTimeEdit詳細(xì)使用方法與實(shí)例
PyQt5日期時(shí)間控件QDateTimeEdit介紹
QDateTimeEdit是一個(gè)允許用戶(hù)編輯日期時(shí)間的控件,可以使用鍵盤(pán)上的上下鍵頭按鈕來(lái)增加或減少日期的時(shí)間值,QDateTimeEdit通過(guò)setDisplayFormat()函數(shù)來(lái)設(shè)置顯示的日期時(shí)間格式
QDateTimeEdit類(lèi)中常用方法
| 方法 | 描述 |
|---|---|
| setDisplayFormat | 設(shè)置日期的時(shí)間格式 |
| yyyy:代表年份,用4為數(shù)表示 | |
| MM:代表月份,取值范圍01-12 | |
| dd:代表日,取值范圍01-31 | |
| HH:代表小時(shí),取值范圍00-23 | |
| mm:代表分鐘,取值范圍00-59 | |
| ss:代表秒,取值范圍00-59 | |
| setMinimumDate() | 設(shè)置控件的最小日期 |
| setMaximumDate() | 設(shè)置控件的最大日期 |
| time() | 返回編輯的時(shí)間 |
| date() | 返回編輯的日期 |
PyQt5日期時(shí)間控件QDateTimeEdit實(shí)例一
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):
#設(shè)置窗口的標(biāo)題與初始大小
self.setWindowTitle('QDateTimeEdit例子')
self.resize(300, 90)
#垂直布局
vlayout = QVBoxLayout()
#實(shí)例化編輯時(shí)間日期的控件
#默認(rèn)下,不指定日期的時(shí)間,系統(tǒng)會(huì)設(shè)置一個(gè)和本地相同的日期時(shí)間格式,時(shí)間默認(rèn)2000年1月1日0時(shí)0分0秒
dateTimeEdit = QDateTimeEdit(self)
#指定當(dāng)前日期時(shí)間為控件的日期時(shí)間
dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime(), self)
#指定當(dāng)前地日期為控件的日期,注意沒(méi)有指定時(shí)間
dateEdit = QDateTimeEdit(QDate.currentDate(), self)
#指定當(dāng)前地時(shí)間為控件的時(shí)間,注意沒(méi)有指定日期
timeEdit = QDateTimeEdit(QTime.currentTime(), self)
# 設(shè)置日期時(shí)間格式,可以選擇/ . : -等符號(hào)自定義數(shù)據(jù)連接符
dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
dateTimeEdit2.setDisplayFormat("yyyy/MM/dd HH-mm-ss")
dateEdit.setDisplayFormat("yyyy.MM.dd")
timeEdit.setDisplayFormat("HH:mm:ss")
#布局控件添加,設(shè)置主窗口的布局
vlayout.addWidget( dateTimeEdit )
vlayout.addWidget( dateTimeEdit2)
vlayout.addWidget( dateEdit )
vlayout.addWidget( timeEdit )
self.setLayout(vlayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = DateTimeEditDemo()
demo.show()
sys.exit(app.exec_())
PyQt5日期時(shí)間控件實(shí)例一代碼解析:
在默認(rèn)情況下,如果QDateTimeEdit類(lèi)構(gòu)造時(shí)不指定日期時(shí)間,那么系統(tǒng)會(huì)為其設(shè)置一個(gè)和本地相同的日期時(shí)間格式,并且值為2000年1月1日0時(shí)0分0秒,也可以手動(dòng)指定控件顯示的日期時(shí)間
#默認(rèn)下,不指定日期的時(shí)間,系統(tǒng)會(huì)設(shè)置一個(gè)和本地相同的日期時(shí)間格式,時(shí)間默認(rèn)2000年1月1日0時(shí)0分0秒
dateTimeEdit = QDateTimeEdit(self)
#指定當(dāng)前日期時(shí)間為控件的日期時(shí)間
dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime(), self)
#指定當(dāng)前地日期為控件的日期,注意沒(méi)有指定時(shí)間
dateEdit = QDateTimeEdit(QDate.currentDate(), self)
#指定當(dāng)前地時(shí)間為控件的時(shí)間,注意沒(méi)有指定日期
timeEdit = QDateTimeEdit(QTime.currentTime(), self)
效果如下圖

設(shè)置日期時(shí)間格式,如果不想使用系統(tǒng)默認(rèn)的格式,可以通過(guò)setDisplayFormat()來(lái)定義日期時(shí)間格式
# 設(shè)置日期時(shí)間格式,可以選擇/ . : -等符號(hào)自定義數(shù)據(jù)連接符
dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
dateTimeEdit2.setDisplayFormat("yyyy/MM/dd HH-mm-ss")
dateEdit.setDisplayFormat("yyyy.MM.dd")
timeEdit.setDisplayFormat("HH:mm:ss")
顯示效果如圖

PyQt5日期時(shí)間控件QDateTimeEdit實(shí)例二
import sys
from PyQt5.QtCore import QDate,QDateTime,QTime
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class DateTimeEditDemo(QWidget):
def __init__(self):
super(DateTimeEditDemo, self).__init__()
self.initUI()
def initUI(self):
#設(shè)置標(biāo)題與初始大小
self.setWindowTitle('QDateTimeEdit 例子')
self.resize(300,90)
#垂直布局
layout=QVBoxLayout()
#創(chuàng)建日期時(shí)間空間,并把當(dāng)前日期時(shí)間賦值,。并修改顯示格式
self.dateEdit=QDateTimeEdit(QDateTime.currentDateTime(),self)
self.dateEdit.setDisplayFormat('yyyy-MM-dd HH:mm:ss')
#設(shè)置日期最大值與最小值,在當(dāng)前日期的基礎(chǔ)上,后一年與前一年
self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365))
self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365))
#設(shè)置日歷控件允許彈出
self.dateEdit.setCalendarPopup(True)
#當(dāng)日期改變時(shí)觸發(fā)槽函數(shù)
self.dateEdit.dateChanged.connect(self.onDateChanged)
#當(dāng)日期時(shí)間改變時(shí)觸發(fā)槽函數(shù)
self.dateEdit.dateTimeChanged.connect(self.onDateTimeChanged)
#當(dāng)時(shí)間改變時(shí)觸發(fā)槽函數(shù)
self.dateEdit.timeChanged.connect(self.onTimeChanged)
#創(chuàng)建按鈕并綁定一個(gè)自定義槽函數(shù)
self.btn=QPushButton('獲得日期和時(shí)間')
self.btn.clicked.connect(self.onButtonClick)
#布局控件的加載與設(shè)置
layout.addWidget(self.dateEdit)
layout.addWidget(self.btn)
self.setLayout(layout)
#日期發(fā)生改變時(shí)執(zhí)行
def onDateChanged(self,date):
#輸出改變的日期
print(date)
#無(wú)論是日期還是時(shí)間改變都執(zhí)行
def onDateTimeChanged(self,dateTime):
#輸出改變的日期時(shí)間
print(dateTime)
#時(shí)間發(fā)生改變執(zhí)行
def onTimeChanged(self,time):
#輸出改變的時(shí)間
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('日期時(shí)間=%s' %str(dateTime))
print('最大日期=%s'%str(maxDate))
print('最大日期時(shí)間=%s'%str(maxDateTime))
print('最大時(shí)間=%s'%str(maxTime))
print('最小日期=%s'%str(minDate))
print('最小日期時(shí)間=%s'%str(minDateTime))
print('最小時(shí)間=%s'%str(minTime))
if __name__ == '__main__':
app=QApplication(sys.argv)
demo=DateTimeEditDemo()
demo.show()
sys.exit(app.exec_())
效果圖如下

PyQt5日期時(shí)間控件QDateTimeEdit實(shí)例二代碼分析
QDateEdit和QTimeEdit均繼承自QDateTimeEdit類(lèi),他們的許多特性和功能都有QDateTimeEdit類(lèi)提供,設(shè)置格式是要注意:
QDateEdit用來(lái)編輯控件的日期,年月日
QTimeEdit用來(lái)編輯控件的時(shí)間,時(shí)分秒
如果要同時(shí)操作日期時(shí)間,請(qǐng)使用QDateTimeEdit
設(shè)置彈出日歷時(shí)要注意:用來(lái)彈出日歷的類(lèi)只有QDateTimeEdit和QDateEdit,而QTimeEdit類(lèi)雖然在語(yǔ)法上可以設(shè)置彈出日歷,但是不起作用………………
#設(shè)置日歷控件允許彈出 self.dateEdit.setCalendarPopup(True)
設(shè)置日期時(shí)間范圍,設(shè)置日期時(shí)間為今天,日歷游戲范圍為:【今天-365,今天+365】
#設(shè)置日期最大值與最小值,在當(dāng)前日期的基礎(chǔ)上,后一年與前一年 self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365)) self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365))
獲取日期時(shí)間
可以通過(guò)date(),datetime()等方法來(lái)獲取日期時(shí)間對(duì)象,如果要獲取年月日等信息,則可以調(diào)用QDate的year(),month(),day()等函數(shù)
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('日期時(shí)間=%s' %str(dateTime))
print('最大日期=%s'%str(maxDate))
print('最大日期時(shí)間=%s'%str(maxDateTime))
print('最大時(shí)間=%s'%str(maxTime))
print('最小日期=%'%str(minDate))
print('最小日期時(shí)間=%s'%str(minDateTime))
print('最小時(shí)間=%s'%str(minTime))
信號(hào)與槽函數(shù)
QDateTimeEdit控件常用的信號(hào)是dateChanged,dateTimeChanged,TimeChanged,分別在改變?nèi)掌?,日期時(shí)間,時(shí)間時(shí)發(fā)射
通過(guò)以下代碼設(shè)置控件的信號(hào)連接槽函數(shù)
#當(dāng)日期改變時(shí)觸發(fā)槽函數(shù)
self.dateEdit.dateChanged.connect(self.onDateChanged)
#當(dāng)日期時(shí)間改變時(shí)觸發(fā)槽函數(shù)
self.dateEdit.dateTimeChanged.connect(self.onDateTimeChanged)
#當(dāng)時(shí)間改變時(shí)觸發(fā)槽函數(shù)
self.dateEdit.timeChanged.connect(self.onTimeChanged)
槽函數(shù)如下
#日期發(fā)生改變時(shí)執(zhí)行
def onDateChanged(self,date):
#輸出改變的日期
print(date)
#無(wú)論是日期還是時(shí)間改變都執(zhí)行
def onDateTimeChanged(self,dateTime):
#輸出改變的日期時(shí)間
print(dateTime)
#時(shí)間發(fā)生改變執(zhí)行
def onTimeChanged(self,time):
#輸出改變的時(shí)間
print(time)
本文詳細(xì)介紹PyQt5日期時(shí)間控件QDateTimeEdit詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5日期時(shí)間控件的使用方法請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
Python獲取服務(wù)器信息的最簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Python獲取服務(wù)器信息的最簡(jiǎn)單實(shí)現(xiàn)方法,涉及Python中urllib2庫(kù)的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
Python使用os.listdir和os.walk獲取文件路徑
這篇文章主要介紹了Python使用os.listdir和os.walk獲取文件路徑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
OpenCV Python實(shí)現(xiàn)拼圖小游戲
這篇文章主要為大家詳細(xì)介紹了OpenCV Python實(shí)現(xiàn)拼圖版小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Python?Pexpect庫(kù)自動(dòng)化交互式進(jìn)程控制的expect_list方法解析
Pexpect是一個(gè)Python庫(kù),為自動(dòng)化和交互式進(jìn)程控制提供了豐富的功能,而expect_list方法是其功能強(qiáng)大且靈活的一部分,將詳細(xì)探討如何使用這一方法,并提供多個(gè)示例來(lái)說(shuō)明其應(yīng)用場(chǎng)景和功能2024-01-01
如何將numpy二維數(shù)組中的np.nan值替換為指定的值
這篇文章主要介紹了將numpy二維數(shù)組中的np.nan值替換為指定的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python用戶(hù)評(píng)論標(biāo)簽匹配的解決方法
這篇文章主要為大家詳細(xì)介紹了python用戶(hù)評(píng)論標(biāo)簽匹配的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

