我的问题是:如何在PYQT4中使用qtimeEdit来循环播放&更新,因此它始终显示当前时间。从本质上讲,我希望它的功能与Windows任务栏的时间和日期相同。
例如:
from PyQt4 import QtCore, QtGui
from PyQt4 import phonon
import sys
import os
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setWindowFlags(QtCore.Qt.WindowContextHelpButtonHint)
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(885, 450)
MainWindow.setMinimumSize(QtCore.QSize(885, 450))
MainWindow.setMaximumSize(QtCore.QSize(885, 491))
MainWindow.setMouseTracking(False) self.timeEdit = QtGui.QTimeEdit(self.splitter)
self.timeEdit.setCursor(QtGui.QCursor(QtCore.Qt.BusyCursor))
self.timeEdit.setAutoFillBackground(True)
self.timeEdit.setWrapping(True)
self.timeEdit.setFrame(True)
self.timeEdit.setAlignment(QtCore.Qt.AlignCenter)
self.timeEdit.setReadOnly(True)
self.timeEdit.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.timeEdit.setKeyboardTracking(True)
self.timeEdit.setObjectName(_fromUtf8("timeEdit"))
def retranslateUi(self, MainWindow):
Today = QtCore.QDate.currentDate()
self.dateEdit.setDate(Today)
self.ui.timeEdit = QtGui.QTimeEdit(self)
self.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.timeEdit.setTime(current.time())
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ControlMainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
event.ignore()
if result == QtGui.QMessageBox.Yes:
event.accept()
def VoluemChanger(self, audioOutput):
self.ui.volumeSlider.setAudioOutput(self.ui.videoPlayer.audioOutput())
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mySW = ControlMainWindow()
mySW.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
尝试此代码,实现一个每秒触发的计时器,此时读取和更新时间。
import sys
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
# Uncomment if you want to change the language
# self.setLocale(QtCore.QLocale(QtCore.QLocale.Spanish, QtCore.QLocale.Peru))
self.verticalLayout = QtGui.QVBoxLayout(self)
self.dateEdit = QtGui.QDateEdit(self)
self.dateEdit.setDisplayFormat("MMM dd yyyy")
self.verticalLayout.addWidget(self.dateEdit)
self.timeEdit = QtGui.QTimeEdit(self)
self.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.verticalLayout.addWidget(self.timeEdit)
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.dateEdit.setDate(current.date())
self.timeEdit.setTime(current.time())
def main():
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
输出:
如果是@JeremyStiefel:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(113, 120)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.timeEdit = QtGui.QTimeEdit(self.centralwidget)
self.timeEdit.setObjectName(_fromUtf8("timeEdit"))
self.timeEdit.setCursor(QtGui.QCursor(QtCore.Qt.BusyCursor))
self.timeEdit.setAutoFillBackground(True)
self.timeEdit.setWrapping(True)
self.timeEdit.setFrame(True)
self.timeEdit.setAlignment(QtCore.Qt.AlignCenter)
self.timeEdit.setReadOnly(True)
self.timeEdit.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.timeEdit.setKeyboardTracking(True)
self.timeEdit.setObjectName(_fromUtf8("timeEdit"))
self.verticalLayout.addWidget(self.timeEdit)
self.dateEdit = QtGui.QDateEdit(self.centralwidget)
self.dateEdit.setObjectName(_fromUtf8("dateEdit"))
self.dateEdit.setCursor(QtGui.QCursor(QtCore.Qt.BusyCursor))
self.dateEdit.setAutoFillBackground(True)
self.dateEdit.setWrapping(True)
self.dateEdit.setFrame(True)
self.dateEdit.setAlignment(QtCore.Qt.AlignCenter)
self.dateEdit.setReadOnly(True)
self.dateEdit.setButtonSymbols(QtGui.QAbstractSpinBox.NoButtons)
self.dateEdit.setKeyboardTracking(True)
self.dateEdit.setObjectName(_fromUtf8("timeEdit"))
self.verticalLayout.addWidget(self.dateEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 113, 22))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
class ControlMainWindow(QtGui.QMainWindow):
def __init__(self):
super(ControlMainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Uncomment if you want to change the language
# self.setLocale(QtCore.QLocale(QtCore.QLocale.Spanish, QtCore.QLocale.Peru))
self.ui.dateEdit.setDisplayFormat("MMM dd yyyy")
self.ui.timeEdit.setDisplayFormat("hh:mm:ss AP")
self.updateTime()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.updateTime)
self.timer.start(1000)
def updateTime(self):
current = QtCore.QDateTime.currentDateTime()
self.ui.dateEdit.setDate(current.date())
self.ui.timeEdit.setTime(current.time())
def closeEvent(self, event):
result = QtGui.QMessageBox.critical(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if result == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main():
app = QtGui.QApplication(sys.argv)
w = ControlMainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
输出:
删除
def VoluemChanger(self, audioOutput):
self.ui.volumeSlider.setAudioOutput(self.ui.videoPlayer.audioOutput())
因为我没有在你的代码中找到变量的定义