PyQt5 QCalendarWidget 获取水平头格式

PyQt5 QCalendarWidget 获取水平头格式

在这篇文章中,我们将看到如何获得QCalendarWidget的水平标题格式。水平页眉是QCalendarWidget中显示日子名称的地方,默认情况下,日子的简称被显示出来,例如,星期一被显示为Mon。默认情况下,ShortDayNames是水平标题的格式,尽管我们可以在setHorizontalHeaderFormat方法的帮助下改变它。

水平标题有很多格式,比如SingleLetterDayNames,它的值是1,这种格式只显示日子名称的第一个字母;ShortDayNames,它是默认格式,它的值是2,这种格式只显示日子名称的缩写。而LongDayNames的值是3,这种格式显示整个日期的名称。

为了做到这一点,我们使用QCalendarWidget对象的horizontalHeaderFormat方法

语法: calendar.horizontalHeaderFormat()

参数: 它不需要参数

返回: 它返回水平头格式对象,但当打印时,它显示与它们相关的值

下面是实现方法

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class Window(QMainWindow):

    def __init__(self):
        super().__init__()

        # setting title
        self.setWindowTitle("Python ")

        # setting geometry
        self.setGeometry(100, 100, 600, 400)

        # calling method
        self.UiComponents()

        # showing all the widgets
        self.show()

    # method for components
    def UiComponents(self):

        # creating a QCalendarWidget object
        calendar = QCalendarWidget(self)

        # setting geometry to the calendar
        calendar.setGeometry(10, 10, 400, 250)

        # setting calendar horizontal header format
        calendar.setHorizontalHeaderFormat(QCalendarWidget.LongDayNames)

        # creating a label
        label = QLabel(self)

        # setting geometry to the label
        label.setGeometry(100, 300, 250, 60)

        # making label multi line
        label.setWordWrap(True)

        # getting the horizontal header format
        value = calendar.horizontalHeaderFormat()

        # setting text to the label
        label.setText("Horizontal Header format value : " + str(value))



# create pyqt5 app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())

输出。

PyQt5 QCalendarWidget - 获取水平头格式

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

PyQt5 日历控件QCalendarWidget