我一直在尝试使用QCalendarWidget来选择我稍后会使用的日期。 所以我创建了小部件,它显示并且看起来都很好。 然后我在文本浏览器中打印所选日期进行测试,我看到日期格式不好。 这是与此问题相关的代码:
print (dfa)
col
first second
bar y 1
z 4
baz y 5
z 6
foo y 1
z 2
df = dfa.sort_values(['second','col'])
print (df)
col
first second
bar y 1
foo y 1
baz y 5
foo z 2
bar z 4
baz z 6
结果是:
from PyQt5 import QtGui, QtCore, QtWidgets, uic
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.ui = uic.loadUi('rent_creation.ui', self)
#the widgets are called calendarWidget_start_date_2 and calendarWidget_end_date_2
self.ui.activate_thescript.clicked.connect(self.activate_script)
self.show()
def activate_script(self):
global start_date
global end_date
start_date = self.ui.calendarWidget_start_date_2.selectedDate().toString()
end_date = self.ui.calendarWidget_end_date_2.selectedDate().toString()
#print data in text browser
text = "Start date: %s \n End date: %s \n" %(start_date, end_date)
self.ui.textBrowser.setText(text)
start_date = QtCore.QDate.currentDate()
end_date = QtCore.QDate.currentDate()
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
我需要结果中的日期格式为:
Start date: Wed May 9 2018
End date: Tue May 15 2018
感谢您的帮助:)
(我只使用全局变量作为示例,显然我将它们踢出我的真实代码)
答案 0 :(得分:2)
您必须传递toString()
格式"yyyy-MM-dd"
:
...
start_date = self.ui.calendarWidget_start_date_2.selectedDate().toString("yyyy-MM-dd")
end_date = self.ui.calendarWidget_end_date_2.selectedDate().toString("yyyy-MM-dd")
...
或QtCore.Qt.ISODate
:
...
start_date = self.ui.calendarWidget_start_date_2.selectedDate().toString(QtCore.Qt.ISODate)
end_date = self.ui.calendarWidget_end_date_2.selectedDate().toString(QtCore.Qt.ISODate)
...