PyQt5 QCalendarWidget – 检查是否有水平标题
在这篇文章中,我们将看到我们如何检查QCalendarWidget是否有水平头。水平头是QCalendarWidget中显示日子名称的地方,默认显示日子的缩写,例如星期一显示为Mon,下面是水平头的表现。
水平头可以通过覆盖QCalendarWidget的结构或者将头的格式值设置为0来移除,因此为了检查是否有头,我们必须得到头的值并与0值进行比较以知道是否有头。
下面是实现的过程
# 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)
# removing horizontal header
calendar.setHorizontalHeaderFormat(0)
# creating a label
label = QLabel(self)
# setting geometry to the label
label.setGeometry(100, 280, 250, 60)
# making label multi line
label.setWordWrap(True)
# getting the horizontal header value
value = calender.horizontalHeaderFormat()
# text
text = "There exist horizontal head"
# comparing value with 0
if value == 0:
text = "There is no horizontal head"
# setting text to the label
label.setText(text)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出。