datetime:2019/5/23 13:44
author:nzb

日历

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QDialog, QCalendarWidget, QVBoxLayout, QLabel
import sys


class Window(QDialog):
    def __init__(self):
        super().__init__()

        self.title = "PyQt5 QCalender"
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300

        self.InitWindow()

    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.Calender()
        self.show()

    def Calender(self):
        vbox = QVBoxLayout()
        self.calender = QCalendarWidget()
        self.calender.selectionChanged.connect(self.onSelectionChanged)
        self.calender.setGridVisible(True)

        vbox.addWidget(self.calender)

        self.label = QLabel()

        self.label.setFont(QtGui.QFont("Sanserif", 15))
        self.label.setStyleSheet('color:green')

        vbox.addWidget(self.label)

        self.setLayout(vbox)

    def onSelectionChanged(self):
        ca = self.calender.selectedDate()
        self.label.setText(str(ca))


App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

日历中时间的格式化

from PyQt5.QtCore import QDateTime, QDate,QTime,Qt

datetime = QDateTime.currentDateTime()

print(datetime.toString())
print(datetime.toString(Qt.ISODate))
print(datetime.toString(Qt.DefaultLocaleLongDate))

# 周四 5月 23 13:46:48 2019
# 2019-05-23T13:46:48
# 2019年5月23日 13:46:48


date = QDate.currentDate()
print(date.toString())
print(date.toString(Qt.ISODate))
print(date.toString(Qt.DefaultLocaleLongDate))

# 周四 5月 23 2019
# 2019-05-23
# 2019年5月23日


time = QTime.currentTime()

print(time.toString())
print(time.toString(Qt.DefaultLocaleLongDate))

# 13:46:48
# 13:46:48

results matching ""

    No results matching ""