我使用qtimeedit编辑时间。但是,当我单击"确定"时,输出为小时:16分钟: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上的输出基础
小时和钟表不代表时间和分钟,而是qtimeedit的各个部分。相反,您应该使用time((方法来获得时间,然后使用后者使用小时((和minute((来获得小时和分钟。
另外,您不应按PYQT指示的QT设计器生成的代码修改代码,而应创建另一个从适当的小部件继承并使用初始类作为接口的类。有关更多信息,我建议阅读此信息。
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)
class Widget(QtWidgets.QWidget, Ui_EditTime):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setupUi(self)
self.OK.clicked.connect(self.onClicked)
@QtCore.pyqtSlot()
def onClicked(self):
t = self.timeEdit.time()
self.HourLCD.display(t.hour())
self.MinuteLCD.display(t.minute())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
显然比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_())