如何显示从QTimeEdit到相应的LCD的小时,分​​钟值?

时间:2019-05-23 08:06:52

标签: python pyqt pyqt5 qlcdnumber qtimeedit

我使用QTimeEdit编辑时间。但是,当我单击“确定”时,输出是HOUR:16 MINUTE:8,无论我在QTimeEdit中输入多少。因此,我想知道我必须添加什么功能来更正QTimeEdit中的设置时间。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_EditTime(object):
    def setupUi(self, EditTime):
        EditTime.setObjectName("EditTime")
        EditTime.resize(392, 293)
        EditTime.setAutoFillBackground(False)
        self.timeEdit = QtWidgets.QTimeEdit(EditTime)
        self.timeEdit.setGeometry(QtCore.QRect(140, 160, 118, 22))
        self.timeEdit.setMaximumDateTime(QtCore.QDateTime(QtCore.QDate(1999, 12, 31), QtCore.QTime(23, 59, 59)))
        self.timeEdit.setMaximumDate(QtCore.QDate(1999, 12, 31))
        self.timeEdit.setMinimumDate(QtCore.QDate(1999, 12, 31))
        self.timeEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
        self.timeEdit.setMinimumTime(QtCore.QTime(00, 00, 00))
        self.timeEdit.setCurrentSection(QtWidgets.QDateTimeEdit.HourSection)
        self.timeEdit.setTimeSpec(QtCore.Qt.OffsetFromUTC)
        self.timeEdit.setTime(QtCore.QTime(24,0,0))
        self.timeEdit.setObjectName("timeEdit")
        self.HourLCD = QtWidgets.QLCDNumber(EditTime)
        self.HourLCD.setGeometry(QtCore.QRect(60, 70, 111, 51))
        self.HourLCD.setObjectName("HourLCD")
        self.MinuteLCD = QtWidgets.QLCDNumber(EditTime)
        self.MinuteLCD.setGeometry(QtCore.QRect(200, 70, 111, 51))
        self.MinuteLCD.setObjectName("MinuteLCD")
        self.OK = QtWidgets.QPushButton(EditTime)
        self.OK.setGeometry(QtCore.QRect(160, 230, 75, 23))
        self.OK.setObjectName("OK")
        self.label = QtWidgets.QLabel(EditTime)
        self.label.setGeometry(QtCore.QRect(90, 46, 31, 20))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(EditTime)
        self.label_2.setGeometry(QtCore.QRect(230, 50, 47, 13))
        self.label_2.setObjectName("label_2")

        self.retranslateUi(EditTime)
        QtCore.QMetaObject.connectSlotsByName(EditTime)
        self.OK.clicked.connect(self.connect)

    def connect(self):
        self.HourLCD.display(self.timeEdit.HourSection)
        self.MinuteLCD.display(self.timeEdit.MinuteSection)

我希望输出基于QTimeEdit

2 个答案:

答案 0 :(得分:1)

HourSection和MinuteSection不代表时间和分钟,而是代表QTimeEdit的sections。相反,您应该使用time()方法获取时间,而使用后者的hour()minute()分别获取小时和分钟。

此外,您不应该按照PyQt的指示修改Qt Designer生成的代码,而应该创建另一个类,该类继承自适当的小部件并使用初始类作为接口。有关更多信息,我建议阅读this

Update

答案 1 :(得分:0)

显然,它比eyllanesc慢一些,并且差异很大,无论如何都可以发布它。备注相同。

我个人更喜欢PEP文档建议的导入语句,以及对在自己的功能中建立联系的监督。

    import sys, time

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QApplication

    class Ui_EditTime(object):

        def setupUi(self, EditTime):
            EditTime.setObjectName("EditTime")
            EditTime.resize(392, 293)
            EditTime.setAutoFillBackground(False)

            self.timeEdit = QtWidgets.QTimeEdit(EditTime)
            self.timeEdit.setGeometry(QtCore.QRect(140, 160, 118, 22))
            self.timeEdit.setMaximumDateTime(QtCore.QDateTime(QtCore.QDate(1999, 12, 31), QtCore.QTime(23, 59, 59)))
            self.timeEdit.setMaximumDate(QtCore.QDate(1999, 12, 31))
            self.timeEdit.setMinimumDate(QtCore.QDate(1999, 12, 31))
            self.timeEdit.setMaximumTime(QtCore.QTime(23, 59, 59))
            self.timeEdit.setMinimumTime(QtCore.QTime(00, 00, 00))
            self.timeEdit.setCurrentSection(QtWidgets.QDateTimeEdit.HourSection)
            self.timeEdit.setTimeSpec(QtCore.Qt.OffsetFromUTC)
            self.timeEdit.setTime(QtCore.QTime(24,0,0))
            self.timeEdit.setObjectName("timeEdit")

            self.HourLCD = QtWidgets.QLCDNumber(EditTime)
            self.HourLCD.setGeometry(QtCore.QRect(60, 70, 111, 51))
            self.HourLCD.setObjectName("HourLCD")

            self.MinuteLCD = QtWidgets.QLCDNumber(EditTime)
            self.MinuteLCD.setGeometry(QtCore.QRect(200, 70, 111, 51))
            self.MinuteLCD.setObjectName("MinuteLCD")

            self.OK = QtWidgets.QPushButton(EditTime)
            self.OK.setGeometry(QtCore.QRect(160, 230, 75, 23))
            self.OK.setObjectName("OK")

            self.label = QtWidgets.QLabel(EditTime)
            self.label.setGeometry(QtCore.QRect(90, 46, 31, 20))
            self.label.setObjectName("label")

            self.label_2 = QtWidgets.QLabel(EditTime)
            self.label_2.setGeometry(QtCore.QRect(230, 50, 47, 13))
            self.label_2.setObjectName("label_2")

            self.retranslateUi(EditTime)
            QtCore.QMetaObject.connectSlotsByName(EditTime)

        def retranslateUi(self, EditTime):
            _translate = QtCore.QCoreApplication.translate

    class EditTime(QtWidgets.QWidget, Ui_EditTime):

        def __init__(self, parent=None):

            super(EditTime, self).__init__(parent) # make flexible MainWindow class.

            self.setupUi(self)
            self.connections()   # oversight off all your connections there are made in your application.

        def connections(self):
            self.OK.clicked.connect(self.connect)

        def connect(self):
            t_mod = self.timeEdit.time()
            self.HourLCD.display(t_mod.hour())
            self.MinuteLCD.display(t_mod.minute())

    if __name__ == '__main__':

        print ('Starting QTimeEdit application')
        sys.stdout.flush()   # shows output directly in an editor.


        app = QApplication(sys.argv)
        window = EditTime()
        window.show()

        sys.exit(app.exec_())