我有一个简单的PyQt GUI,它提供了我希望变成日期时间格式的值。
目前正在打印
201011
我想要打印
2010,1,1
这是PyQt代码,
*timer.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(510, 129)
self.gridLayout = QtGui.QGridLayout(Dialog)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.frame = QtGui.QFrame(Dialog)
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName(_fromUtf8("frame"))
self.gridLayout_2 = QtGui.QGridLayout(self.frame)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.yearlabel = QtGui.QLabel(self.frame)
self.yearlabel.setObjectName(_fromUtf8("yearlabel"))
self.gridLayout_2.addWidget(self.yearlabel, 0, 0, 1, 1)
self.monthfromcomboBox = QtGui.QComboBox(self.frame)
self.monthfromcomboBox.setObjectName(_fromUtf8("monthfromcomboBox"))
self.gridLayout_2.addWidget(self.monthfromcomboBox, 1, 2, 1, 2)
self.label_2 = QtGui.QLabel(self.frame)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.gridLayout_2.addWidget(self.label_2, 0, 4, 1, 1)
self.SearchButton = QtGui.QPushButton(self.frame)
self.SearchButton.setObjectName(_fromUtf8("SearchButton"))
self.gridLayout_2.addWidget(self.SearchButton, 2, 4, 1, 2)
self.yearfromcomboBox = QtGui.QComboBox(self.frame)
self.yearfromcomboBox.setObjectName(_fromUtf8("yearfromcomboBox"))
self.gridLayout_2.addWidget(self.yearfromcomboBox, 1, 0, 1, 2)
self.dayfromcomboBox = QtGui.QComboBox(self.frame)
self.dayfromcomboBox.setObjectName(_fromUtf8("dayfromcomboBox"))
self.gridLayout_2.addWidget(self.dayfromcomboBox, 1, 4, 1, 2)
self.label = QtGui.QLabel(self.frame)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout_2.addWidget(self.label, 0, 2, 1, 1)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
Dialog.setTabOrder(self.yearfromcomboBox, self.monthfromcomboBox)
Dialog.setTabOrder(self.monthfromcomboBox, self.dayfromcomboBox)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.yearlabel.setText(_translate("Dialog", "Year", None))
self.label_2.setText(_translate("Dialog", "Day", None))
self.SearchButton.setText(_translate("Dialog", "Go", None))
self.label.setText(_translate("Dialog", "Month", None))
相应的python代码是,
import sys
import datetime
from PyQt4 import QtCore, QtGui
from timer import *
from PyQt4.QtGui import (QApplication, QTabWidget, QWidget,
QStyle, QStyleFactory)
class Window(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
style = QStyleFactory.create('Cleanlooks')
app.setStyle(style)
QtCore.QObject.connect(self.ui.SearchButton, QtCore.SIGNAL('clicked()'), self.search)
months = range(1,13)
for iitem in months:
Months = str(iitem)
self.ui.monthfromcomboBox.addItem(Months)
days = range(1,32)
for iitem in days:
Days = str(iitem)
self.ui.dayfromcomboBox.addItem(Days)
years = range(2010, 2017)
for iitem in years:
Years = str(iitem)
self.ui.yearfromcomboBox.addItem(Years)
def search(self):
nowyear = int(self.ui.yearfromcomboBox.currentText())
nowmonth = int(self.ui.monthfromcomboBox.currentText())
nowday = int(self.ui.dayfromcomboBox.currentText())
nowdate = int('%d%d%d' %(nowyear,nowmonth,nowday))
print nowdate
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
viewer = Window()
viewer.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
这是我的解决方案:
dateNum = 201011
dateNum = str(dateNum)
year = dateNum[:4]
monthDay = dateNum[4:]
if len(monthDay) == 2:
day = monthDay[1:]
month = monthDay[:1]
print(year + "," + month + "," + day)
这将输出:
2010,1,1
此代码的唯一问题是您无法确定日期,例如:
2014111应该是11月1日,但也可能是1月11日
有什么方法可以更改格式以包含前导零? (例如2014年11月1日的20141101)
也许其他人有办法确定这一点。但是我认为逻辑上的格式使得无法确定其中一种。
重新定义此代码以处理前导零:
dateNum = 20100101
dateNum = str(dateNum)
year = dateNum[:4]
monthDay = dateNum[4:]
day = monthDay[2:]
month = monthDay[:2]
print(year + "," + month + "," + day)
2010,01,01
答案 1 :(得分:0)
我建议使用datetime.datetime.strptime
:
from datetime import datetime
date_int = 201011
date = datetime.strptime(str(date_int), '%Y%m%d')
print '{},{},{}'.format(date.year, date.month, date.day)
2010,1,1
答案 2 :(得分:0)
没有理由将日期部分转换为字符串,然后转换为单个整数,如果实际上并不需要该整数。摆脱这一行:
nowdate = int('%d%d%d' %(nowyear,nowmonth,nowday))
而是使用您想要的个性化日期组件。它可以像print nowyear, nowmonth, nowday
一样简单(它将用空格分隔它们),或者你可以做一些更复杂的事情,比如使用它们来创建datetime
模块中你可以使用的东西(或打印)各种各样的格式。)
要获得您特别请求的输出('2010,1,1'
),您可以将其保持在与现有代码非常接近的位置。只需使用print '%d,%d,%d' % (nowyear, nowmonth, nowday)
,或者如果您想使用较新的str.format
语法print '{:d},{:d},{:d}'.format(nowyear, nowmonth, nowday)
。如果您希望单个数字的月份或天数使用两个字符(因此它们的宽度始终相同),您可以在格式字符串中使用%02d
或{:02d}
来请求填充零宽度为2。