1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# -*- coding: utf-8 -*-
import sys
from datetime import datetime, timedelta
from PyQt5.QtCore import QTime
from PyQt5.QtWidgets import (QApplication, QWidget, QGridLayout,
QTimeEdit, QAbstractSpinBox)
class RollingTimeEdit(QTimeEdit):
def __init__(self, parent=None):
super().__init__(parent)
def stepBy(self, step):
section = self.currentSection()
old = self.get_python_time()
if section == 0x04:
delta = timedelta(seconds=step)
elif section == 0x08:
delta = timedelta(minutes=step)
else:
delta = timedelta(hours=step)
new = old + delta
self.setTime(QTime(new.hour, new.minute, new.second))
def get_python_time(self):
"""Convert QTime to datetime instance.
"""
time_ = self.time()
return datetime(1970, 1, 1,
hour=time_.hour(),
minute=time_.minute(),
second=time_.second())
def stepEnabled(self):
return (QAbstractSpinBox.StepUpEnabled |
QAbstractSpinBox.StepDownEnabled)
if __name__ == '__main__':
app = QApplication([])
w = QWidget()
l = QGridLayout(w)
r = RollingTimeEdit(w)
l.addWidget(r, 0, 0, 1, 1)
w.show()
t = QTime().currentTime()
r.setTime(t)
sys.exit(app.exec_()) |
Partager