PyQt5 QCalendarWidget 启用接受拖放功能


发布日期 : 2023-06-10 15:55:22 UTC

访问量: 10 次浏览

PyQt5 QCalendarWidget 设置接受掉落的属性

在这篇文章中,我们将看到如何设置QCalendarWidget的接受下降属性。我们知道可以使用日历对象的 setDragEnabled 方法使拖动成为可能,但是拖动文本有什么用呢,我们不能把它放到任何地方。允许接受拖放意味着日历现在有一个属性可以接受其他日历小部件的拖放文本。接受下降是用来设置年份的,因为它是作为子对象的旋转盒。

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

语法

语法: calendar.setAcceptDrops(True)

参数: 它以 bool 作为参数

执行的动作: 它返回 None

示例

下面是实现方法:

# 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)

        # accepting drops
        calendar.setAcceptDrops(True)


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

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

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