我希望能够将时间戳添加到QListWidget实例并将其保存到文本文件中。以及查看文本文件中已有的项目,以便在程序退出后保存项目。
我现在的代码按照我的意愿将其保存到列表中,但在关闭和重新打开程序之前,我没有看到我添加的项目:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from datetime import datetime
class feedingTime(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
self.feedList = QListWidget()
self.label = QLabel(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),self)
self.button = QPushButton("Add time")
self.info = QLabel("Baby was last fed:")
layout.addWidget(self.label)
layout.addWidget(self.button)
layout.addWidget(self.info)
layout.addWidget(self.feedList)
self.setLayout(layout)
self.timer = QTimer(self.label)
self.timer.setInterval(1000)
self.timer.timeout.connect(self.displayTime)
self.timer.start()
self.button.clicked.connect(self.feedAdd)
def feedAdd(self):
self.feedList.addItem(self.label.text())
def displayTime(self):
self.label.setText(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = feedingTime()
dialog.show()
sys.exit(app.exec_())
是否有简单的方法从文本文件中读取列表以及在按下按钮时附加新的时间戳?添加一个按钮可以在单击时删除“最旧”的时间戳。
我试图为我的妻子制作一个brestfeeding app:)
PyQt noob在这里。谢谢你的帮助。
答案 0 :(得分:0)
将数据保存到log
与PyQt
无关。您所需要的只是在Python中使用I / O的基本知识。我使用了简单的log
文件,该文件必须与脚本位于同一目录中(可以改进为更复杂的东西)。我也实现了所需的delete
按钮,但是,我不确定我是否正确理解了"oldest" timestamp
的含义。
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from datetime import datetime
FILENAME = "history.log"
class feedingTime(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
self.feedList = QListWidget()
self.label = QLabel(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),self)
self.button = QPushButton("Add time")
self.info = QLabel("Baby was last fed:")
self.buttonDelete = QPushButton("Delete oldest")
layout.addWidget(self.label)
layout.addWidget(self.button)
layout.addWidget(self.info)
layout.addWidget(self.feedList)
layout.addWidget(self.buttonDelete)
self.setLayout(layout)
self.timer = QTimer(self.label)
self.timer.setInterval(1000)
self.timer.timeout.connect(self.displayTime)
self.timer.start()
self.button.clicked.connect(self.feedAdd)
self.buttonDelete.clicked.connect(self.deleteOldest)
self.loadOldData()
def deleteOldest(self):
self.feedList.takeItem(self.feedList.count() - 1)
lines = open(FILENAME).readlines()
with open(FILENAME, 'w') as f:
f.writelines(lines[1:])
def feedAdd(self):
date = self.label.text()
self.feedList.addItem(date)
f = open(FILENAME, 'a')
f.write(date + '\n')
f.close()
self.feedList.sortItems(Qt.DescendingOrder) # change to AscendingOrder if want inverted order
def fillData(self, lines):
for line in lines:
self.feedList.addItem(line.rstrip())
def loadOldData(self):
try:
file = open(FILENAME)
lines = file.readlines()
self.fillData(lines)
except IOError:
print "File" + FILENAME + "not found, skipping..."
def displayTime(self):
self.label.setText(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if __name__ == "__main__":
app = QApplication(sys.argv)
dialog = feedingTime()
dialog.show()
sys.exit(app.exec_())