PyQt5 QCalendarWidget 设置自定义快捷键到特定月份

PyQt5 QCalendarWidget 设置自定义快捷键到特定月份

在这篇文章中,我们将看到如何为QCalendarWidget设置自定义快捷键来进入特定的月份,例如,当用户按下’O’键时,日历应该显示10月,其他月份也是如此。对于像1月、6月和7月这样的月份,如果它们的首字母相同,那么当’J’键被按下时,它应该是当前月份的下一个月,例如,如果当前月份是6月,’J’键被按下,它应该显示7月,如果当前月份是7月,它应该显示1月。

实施步骤:
1.创建一个主窗口
2.创建一个QCalendarWidget
3.为日历设置各种属性
4.抓住日历的键盘,这样默认的键盘功能就不能发生了
5.覆盖keyPressEvent
6.在覆盖方法中创建一个所有月份的按键列表
7.获取日历的当前月份和年份
8.检查’J’键是否被按下,然后检查当前月份是否是一月,然后将当前月份设置为六月,如果当前月份是六月,则将当前月份设置为七月,否则将当前月份设置为一月
9.如果按下’M’键,则检查当前月份是否为3月,然后将当前月份设为5月,否则将当前月份设为3月
10 同样,如果按下’A’键,则检查当前月份是否为4月,将当前月份设为8月,否则将当前月份设为4月
11.如果任何其他键被按下,检查该键是否存在于月份列表中,然后得到该键在列表中的索引,然后将当前月份设置为索引+1。

下面是实现的过程

# 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, 650, 400)

        # calling method
        self.UiComponents()

        # showing all the widgets
        self.show()

    # method for components
    def UiComponents(self):

        # creating a QCalendarWidget object
        self.calendar = QCalendarWidget(self)

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

        # setting cursor
        self.calendar.setCursor(Qt.PointingHandCursor)

        # grabbing the keyboard for the calendar
        # to stop the predefined action
        self.calendar.grabKeyboard()


    # overriding key release event
    def keyPressEvent(self, e):


        # creating a key list according to the month
        keylist = [Qt.Key_J, Qt.Key_F, Qt.Key_M, Qt.Key_A, Qt.Key_M, Qt.Key_J,
                   Qt.Key_J, Qt.Key_A, Qt.Key_S, Qt.Key_O, Qt.Key_N, Qt.Key_D]

        # getting current month
        month = self.calendar.monthShown()

        # getting current year
        year = self.calendar.yearShown()

        # when J key is pressed
        if e.key() == keylist[0]:
            print("J Key Pressed")

            # if current month is January
            if month == 1:

                # set month as June
                self.calendar.setCurrentPage(year, 6)

            # if current month is June
            elif month == 6:

                # set month as July
                self.calendar.setCurrentPage(year, 7)

            # if current month is not june or july
            else:

                # set month as January
                self.calendar.setCurrentPage(year, 1)

        # if M key is pressed
        elif e.key() == keylist[2]:
            print("M key pressed")

            # if current month is March
            if month == 3:

                # set month as May
                self.calendar.setCurrentPage(year, 5)

            # if current month is not May
            else:
                # set month as March
                self.calendar.setCurrentPage(year, 3)

        # if A key is pressed
        elif e.key() == keylist[3]:
            print("A key pressed")

            # if current month is April
            if month == 4:

                # set month as August
                self.calendar.setCurrentPage(year, 8)

            # if current month is not August
            else:
                # set month as April
                self.calendar.setCurrentPage(year, 4)

        # if any other key is pressed
        elif e.key() in keylist:

            # get the position of the key in the list
            index = keylist.index(e.key())
            print(str(index + 1) + " Month Key Pressed")

            # set the month
            self.calendar.setCurrentPage(year, index + 1 )




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

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

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

输出

A key pressed
A key pressed
J Key Pressed
J Key Pressed
J Key Pressed
10 Month Key Pressed
12 Month Key Pressed
2 Month Key Pressed
11 Month Key Pressed
J Key Pressed
A key pressed
J Key Pressed
A key pressed
J Key Pressed
10 Month Key Pressed

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

PyQt5 日历控件QCalendarWidget