PyQt5 QCalendarWidget 设置开始计时器

  • Post category:Python

先说一下QCalendarWidget是什么。QCalendarWidget是PyQt5中的一个控件,用于显示日历并允许用户选择日期。其继承自QWidget类,可以方便地嵌入到应用程序中。

在PyQt5中,我们可以使用QTimer类来实现开始计时器。可以在QCalendarWidget中的日期选定后,启动一个QTimer对象,使其开始计时。具体步骤如下:

1.导入PyQt5中的QtCore和QtWidgets模块中的QCalendarWidget和QTimer类:

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QCalendarWidget

2.创建一个QCalendarWidget对象,并将其放入主窗口:

calendar = QCalendarWidget(self)
calendar.setGeometry(50, 50, 200, 200)

3.在QCalendarWidget的日期被选取时启动QTimer对象:

def on_date_selection(self):
    self.timer.start(1000)

calendar.selectionChanged.connect(on_date_selection)

这里我们使用了QTimer的start函数来启动计时器并设定了计时间隔为1秒(1000毫秒)。我们还使用了QCalendarWidget的selectionChanged信号,当日期被选定时会触发该信号,从而我们可以启动计时器。

接下来,我们来看两个示例:

1.在QCalendarWidget中添加一个LCD数字显示器,用于显示计时器计数器的数值

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QCalendarWidget, QLabel, QVBoxLayout, QWidget


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.on_timer_timeout)

        self.calendar = QCalendarWidget(self)
        self.calendar.selectionChanged.connect(self.on_calendar_selectionChanged)

        self.lcd = QLabel("0", self)
        self.lcd.setAlignment(Qt.AlignHCenter)
        self.lcd.setGeometry(160, 270, 50, 30)

        layout = QVBoxLayout(self)
        layout.addWidget(self.calendar)

    def on_calendar_selectionChanged(self):
        self.timer.start(1000)

    def on_timer_timeout(self):
        time = int(self.lcd.text()) + 1
        self.lcd.setText(str(time))

if __name__ == '__main__':
    import sys

    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    app.setStyle('fusion')

    mainWindow = MainWindow()
    mainWindow.setGeometry(100, 100, 300, 350)
    mainWindow.setFixedSize(300, 350)
    mainWindow.show()

    sys.exit(app.exec_())

在这个示例中,我们添加了一个QLabel控件来显示计时器的数值。在on_timer_timeout函数中,每次计时器计数器递增1,同时更新QLabel的文本内容。

2.在QCalendarWidget中添加一个进度条,用于显示计时器剩余时间的百分比

from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QCalendarWidget, QProgressBar, QVBoxLayout, QWidget


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.on_timer_timeout)

        self.calendar = QCalendarWidget(self)
        self.calendar.selectionChanged.connect(self.on_calendar_selectionChanged)

        self.progress = QProgressBar(self)
        self.progress.setGeometry(20, 270, 260, 20)
        self.progress.setRange(0, 100)

        layout = QVBoxLayout(self)
        layout.addWidget(self.calendar)

    def on_calendar_selectionChanged(self):
        self.timer.start(1000)

    def on_timer_timeout(self):
        remaining_time = 5 - int(self.timer.remainingTime() / 1000)
        percent = remaining_time / 5 * 100
        self.progress.setValue(percent)

if __name__ == '__main__':
    import sys

    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    app.setStyle('fusion')

    mainWindow = MainWindow()
    mainWindow.setGeometry(100, 100, 300, 350)
    mainWindow.setFixedSize(300, 350)
    mainWindow.show()

    sys.exit(app.exec_())

在这个示例中,我们添加了一个QProgressBar控件来显示计时器剩余时间的百分比。在on_timer_timeout函数中,我们首先通过QTimer的remainingTime()函数获取计时器剩余时间,然后将其转化为剩余时间的百分比,并将该值更新到QProgressBar控件中。