PyQt5 QCalendarWidget 通过禁用停止功能

  • Post category:Python

下面是 PyQT5 中使用 QCalendarWidget 控件通过禁用停止功能的完整使用攻略:

禁用停止功能

QCalendarWidget 控件默认包含停止按钮,该按钮允许用户单击并返回到当前日期。如果您想禁用此功能,则可以使用 setNavigationBarVisible() 方法。

from PyQt5.QtWidgets import QApplication, QMainWindow, QCalendarWidget

class CalendarWidget(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Calendar Widget")
        self.setGeometry(100, 100, 800, 500)

        #创建一个 QCalendarWidget 对象
        self.calendar_widget = QCalendarWidget(self)

        # 禁用导航栏的停止按钮
        self.calendar_widget.setNavigationBarVisible(False)  

        self.setCentralWidget(self.calendar_widget)

if __name__ == '__main__':
    app = QApplication([])
    window = CalendarWidget()
    window.show()
    app.exec_()

在上述代码中,我们使用 setNavigationBarVisible() 方法,将导航栏的停止按钮隐藏。

示例1:获取选择的日期

from PyQt5.QtWidgets import QApplication, QMainWindow, QCalendarWidget, QLabel, QVBoxLayout, QWidget

class CalendarWidget(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Calendar Widget")
        self.setGeometry(100, 100, 800, 500)
        self.layout = QVBoxLayout()

        #创建一个 QCalendarWidget 对象
        self.calendar_widget = QCalendarWidget(self)

        # 禁用导航栏的停止按钮
        self.calendar_widget.setNavigationBarVisible(False)

        #添加信号处理程序以获取选择的日期
        self.calendar_widget.clicked[QtCore.QDate].connect(self.show_selected_date)

        self.layout.addWidget(self.calendar_widget)
        self.label = QLabel("")
        self.layout.addWidget(self.label)

        #将QWidget添加到主窗口中
        widget = QWidget()
        widget.setLayout(self.layout)
        self.setCentralWidget(widget)

    def show_selected_date(self, date):
        # 更新选定日期的文本标签
        self.label.setText(date.toString())

if __name__ == '__main__':
    app = QApplication([])
    window = CalendarWidget()
    window.show()
    app.exec_()

上述示例展示了如何获取选择的日期。在这里,我们使用了 QCalendarWidget 控件中的 clicked 信号,该信号在用户单击日期选择器中的某一日期时发出。我们将这个信号连接到函数 show_selected_date(),在该函数中,我们更新标签的文本以显示所选日期。

示例2:设置默认日期

from PyQt5.QtWidgets import QApplication, QMainWindow, QCalendarWidget, QLabel, QVBoxLayout, QWidget
from PyQt5 import QtCore


class CalendarWidget(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Calendar Widget")
        self.setGeometry(100, 100, 800, 500)
        self.layout = QVBoxLayout()

        #创建一个 QCalendarWidget 对象
        self.calendar_widget = QCalendarWidget(self)

        # 禁用导航栏的停止按钮
        self.calendar_widget.setNavigationBarVisible(False)

        #设置默认日期
        default_date = QtCore.QDate(2021, 8, 28)  
        self.calendar_widget.setSelectedDate(default_date)

        #添加信号处理程序以获取选择的日期
        self.calendar_widget.clicked[QtCore.QDate].connect(self.show_selected_date)

        self.layout.addWidget(self.calendar_widget)
        self.label = QLabel("")
        self.layout.addWidget(self.label)

        #将QWidget添加到主窗口中
        widget = QWidget()
        widget.setLayout(self.layout)
        self.setCentralWidget(widget)

    def show_selected_date(self, date):
        # 更新选定日期的文本标签
        self.label.setText(date.toString())

if __name__ == '__main__':
    app = QApplication([])
    window = CalendarWidget()
    window.show()
    app.exec_()

在上述示例中,我们使用 setSelectedDate() 方法设置默认日期。此设置将导致初始选定日期显示在 QCalendarWidget 控件中。在这里,我们设置默认日期为 2021 年 8 月 28 日。