凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

PyQt5 时间组件QDateTimeEdit

2019-09-04 python 1981
import sys
from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication, QHBoxLayout, \
    QVBoxLayout, QDesktopWidget, QLabel, QDateTimeEdit, QCheckBox
from PyQt5.QtCore import QObject, QDateTime, Qt, QTime, QDate
import os
import time


class CombWidget(QObject):
    def __init__(self):
        super().__init__()

    def combHBox(self, lst: list):
        h = QHBoxLayout()
        for x, y in lst:
            h.addWidget(x, y)
        wg = QWidget()
        wg.setLayout(h)
        return wg

    def combVBox(self, lst: list):
        h = QVBoxLayout()
        for x, y in lst:
            h.addWidget(x, y)
        wg = QWidget()
        wg.setLayout(h)
        return wg


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        v = QVBoxLayout()
        comb = CombWidget()

        h = QHBoxLayout()

        createTime_lbl = QLabel("时间1")
        self.createTime_txt = QDateTimeEdit(QDate.currentDate())
        self.createTime_txt.setDisplayFormat("yyyy-MM-dd")
        self.createTime_txt.setFocusPolicy(Qt.NoFocus)
        self.createTime_check = QCheckBox()

        modifyTime_lbl = QLabel("时间2")
        self.modifyTime_txt = QDateTimeEdit(QTime.currentTime())
        self.modifyTime_txt.setDisplayFormat("HH:mm:ss")
        self.modifyTime_txt.setFocusPolicy(Qt.NoFocus)
        self.modifyTime_check = QCheckBox()
        self.modifyTime_check.setChecked(True)

        accessTime_lbl = QLabel("时间3")
        self.accessTime_txt = QDateTimeEdit(QDateTime.currentDateTime())
        self.accessTime_txt.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
        self.accessTime_txt.setFocusPolicy(Qt.NoFocus)
        self.accessTime_check = QCheckBox()
        self.accessTime_check.setChecked(True)

        v.addWidget(comb.combHBox(
            [(createTime_lbl, 1), (self.createTime_txt, 2), (modifyTime_lbl, 1),
             (self.modifyTime_txt, 2), (accessTime_lbl, 1), (self.accessTime_txt, 2),
             ]), 1)

        vwg = QWidget()
        vwg.setLayout(v)

        self.setCentralWidget(vwg)

        self.setGeometry(300, 300, 500, 300)
        self.center()
        self.setWindowTitle("时间组件")

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

1.jpg

文章评论

0条评论