PyQt5 控件学习(一个一个学习之QCalendarWidget)

QCalendarWidget 的继承图:

QCalendarWidget 的描述:

它的区域划分:

 

 

 

QCalendarWidget 的继承:

它继承自QWidget  

 

QCalendarWidget 的功能作用:

QCalendarWidget 的功能作用之构造函数:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui() 


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

这里的不是弹出来的,它是直接放到window 父控件里面的!

 

 

QCalendarWidget 的功能作用之日期范围:

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)

        # calendarWidget.setSelectedDate(QDate(-9999,1,1))

        # calendarWidget.setMinimumDate(QDate(1949,10,1))
        # calendarWidget.setMaximumDate(QDate(2049,10,1))
        calendarWidget.setDateRange(QDate(1949,1,1),QDate(2049 ,1,1))



if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

QCalendarWidget 的功能作用之日期编辑:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)


        calendarWidget.setDateRange(QDate(1949,1,1),QDate(2049 ,1,1))

        # calendarWidget.setDateEditEnabled(False)  #这样就不能在日期上直接改了
        calendarWidget.setDateEditAcceptDelay(3000)  #3s 编辑结束3s才会跳转过去


if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

 

 

QCalendarWidget 的功能作用之日期获取:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)


        calendarWidget.setDateRange(QDate(1949,1,1),QDate(2049 ,1,1))

        # calendarWidget.setDateEditEnabled(False)  #这样就不能在日期上直接改了
        calendarWidget.setDateEditAcceptDelay(3000)  #3s 编辑结束3s才会跳转过去
        
        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)

        btn.clicked.connect(lambda :print(calendarWidget.yearShown()))
        btn.clicked.connect(lambda :print(calendarWidget.monthShown()))
        btn.clicked.connect(lambda :print(calendarWidget.selectedDate()))



if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

使用最多的是最后一个

 

 

 

QCalendarWidget 的功能作用之格式外观:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)



        #设置导航条
        # calendarWidget.setNavigationBarVisible(False)

        # calendarWidget.setFirstDayOfWeek(Qt.Monday)

        # calendarWidget.setGridVisible(True)





if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)



        #设置文本格式
        textCharFormat = QTextCharFormat()
        textCharFormat.setFontFamily("隶书")
        textCharFormat.setFontPointSize(16)
        textCharFormat.setFontUnderline(True)

        calendarWidget.setHeaderTextFormat(textCharFormat)

        # calendarWidget.setHorizontalHeaderFormat(QCalendarWidget.LongDayNames)
        # calendarWidget.setHorizontalHeaderFormat(QCalendarWidget.NoHorizontalHeader)
        # calendarWidget.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader)


        calendarWidget.setWeekdayTextFormat(Qt.Tuesday,textCharFormat)

        calendarWidget.setDateTextFormat(QDate(2019,8,22),textCharFormat)




if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

 

 

QCalendarWidget 的功能作用之选中:

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)

        #使鼠标不能点
        calendarWidget.setSelectionMode(QCalendarWidget.NoSelection)


        # #
        # calendarWidget.setSelectedDate(QDate(2019,11,11))




if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

QCalendarWidget 的功能作用之常用的方法:

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)
        self.calendarWidget = calendarWidget

        btn = QPushButton(self)
        btn.setText("按钮")
        btn.move(0,300)
        btn.clicked.connect(self.btn_clicked_slot)


    def btn_clicked_slot(self):
        # self.calendarWidget.showToday()
        # self.calendarWidget.showSelectedDate()
        # self.calendarWidget.showNextYear()
        # self.calendarWidget.showNextMonth()
        # self.calendarWidget.showPreviousMonth()
        # self.calendarWidget.showPreviousMonth()
        self.calendarWidget.setCurrentPage(2008,8)

        self.calendarWidget.setFocus()




if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

 

 

QCalendarWidget 的信号:

 

from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QCalendarWidget 的学习")
        self.resize(400,400)
        self.set_ui()


    def set_ui(self):
        calendarWidget = QCalendarWidget(self)
        self.calendarWidget = calendarWidget

        #信号
        # calendarWidget.activated.connect(lambda val:print(val))
        # calendarWidget.clicked.connect(lambda val:print(val))
        # calendarWidget.currentPageChanged.connect(lambda y,m:print(y,m))
        calendarWidget.selectionChanged.connect(lambda :print("选中日期改变",calendarWidget.selectedDate()))




if __name__ == '__main__':
    app =QApplication(sys.argv)

    window = Window()
    window.show()

    sys.exit(app.exec_())
View Code

 

总结:

到此,所有的输入控件都已经说完了,

输入控件大汇总:

下面是展示控件:

我们这里首先看的是QLabel 控件:https://www.cnblogs.com/zach0812/p/11395402.html 

 

转载于:https://www.cnblogs.com/zach0812/p/11394594.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PyQt5中的QCalendarWidget件支持以下事件: 1. selectionChanged:当用户选择日期时触发该事件。 2. activated:当用户双击日期或按Enter键时触发该事件。 3. clicked:当用户单击日期时触发该事件。 4. currentPageChanged:当用户导航到日历的不同页面时触发该事件。 5. customContextMenuRequested:当用户右键单击日历时触发该事件。 您可以使用QCalendarWidget的connect方法将这些事件与自定义槽函数连接起来。例如,以下代码演示了如何将selectionChanged事件与名为handleSelectionChanged的自定义槽函数连接起来: ``` from PyQt5.QtWidgets import QCalendarWidget, QApplication app = QApplication([]) calendar = QCalendarWidget() def handleSelectionChanged(): selected_date = calendar.selectedDate() print("Selected date:", selected_date.toString()) calendar.selectionChanged.connect(handleSelectionChanged) calendar.show() app.exec_() ``` 当用户选择日期时,将调用handleSelectionChanged函数,并输出所选日期。 ### 回答2: 在使用PyQt5中的QCalendarWidget件时,可以通过注册事件来响应其不同的行为。 1. QCalendarWidget的changeDate事件:当用户选择一个日期时,该事件会触发。可以通过重写changeDate()方法来捕捉该事件,然后根据用户选择的日期进行相应的操作,例如更新界面上的其他相关信息。 2. QCalendarWidget的clicked事件:当用户点击某个日期时,该事件会触发。可以通过重写clicked()方法来捕捉该事件,然后根据用户点击的日期进行相应的操作,例如弹出一个消息框显示选中的日期信息。 3. QCalendarWidget的activated事件:当用户选中某个日期并回车确认时,该事件会触发。可以通过重写activated()方法来捕捉该事件,然后根据用户选中的日期进行相应的操作,例如在制台打印选中日期的信息。 4. QCalendarWidget的currentPageChanged事件:当用户切换月份时,该事件会触发。可以通过重写currentPageChanged()方法来捕捉该事件,然后根据用户切换的月份进行相应的操作,例如更新界面上显示的月份信息。 通过捕捉这些事件,我们可以根据用户的操作来动态地进行处理,从而提供更好的用户体验和交互。 ### 回答3: PyQt5的QCalendarWidget是一个日历件,它提供了多种事件可以监听。 1. clicked 事件:当用户点击了日历的某个日期时,会触发该事件。我们可以通过连接到该事件来执行相应的操作,比如在点击日期后更新界面或执行特定的功能。 2. activated 事件:当用户在日历上选中某个日期并确认选择时,会触发该事件。我们可以通过连接到该事件来获取用户所选日期的信息,比如获取选中日期的年、月、日等。 3. selectionChanged 事件:当用户在日历上改变了选择的日期范围时,会触发该事件。我们可以通过连接到该事件来获取选择日期范围的开始日期和结束日期,比如在选择日期范围后更新界面或执行相应操作。 4. currentChanged 事件:当用户在日历上切换了当前显示的月份时,会触发该事件。我们可以通过连接到该事件来获取当前显示的月份的信息,比如获取当前显示月份的年、月等。 5. selectionModeChanged 事件:当用户改变了日历的选择模式时,比如从单选模式切换到多选模式,会触发该事件。我们可以通过连接到该事件来获取选择模式的改变信息,比如获取新的选择模式。 通过连接这些事件,我们可以根据用户的操作做出相应的响应和处理。比如,我们可以根据选择的日期在其他部件中显示相应的信息,或者在选择日期后执行一些计算或逻辑操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值