我想在csv的特定列中添加来自singerLine和titleSong的信息。我的数据库看起来像这个csv图片所示。
当点击“添加歌曲”按钮时,我想将这些数据添加到第3列(歌曲)和第4列(歌手)。这是我部分的代码(我知道它还不正确,而且我对pyqt很陌生)。
当点击“添加歌曲”按钮时,我想将这些数据添加到第3列(歌曲)和第4列(歌手)。这是我部分的代码(我知道它还不正确,而且我对pyqt很陌生)。
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
import sys
import csv
class QuitMessage(QtWidgets.QMessageBox):
def __init__(self):
QtWidgets.QMessageBox.__init__(self)
self.setText("Really want to quit?")
self.addButton(self.No)
self.addButton(self.Yes)
class musicAdminUI(object):
def setupUi(self, musicAdmin):
musicAdmin.setObjectName("musicAdmin")
musicAdmin.resize(734, 601)
musicAdmin.setMinimumSize(QtCore.QSize(734, 601))
musicAdmin.setMaximumSize(QtCore.QSize(734, 601))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("musicIcon.gif"), QtGui.QIcon.Normal,
QtGui.QIcon.Off)
musicAdmin.setWindowIcon(icon)
self.exitButton = QtWidgets.QPushButton(musicAdmin)
self.exitButton.setGeometry(QtCore.QRect(650, 565, 75, 23))
self.exitButton.setObjectName("exitButton")
self.titleSong = QtWidgets.QLineEdit(musicAdmin)
self.titleSong.setGeometry(QtCore.QRect(460, 240, 181, 20))
self.titleSong.setObjectName("titleSong")
self.singerLine = QtWidgets.QLineEdit(musicAdmin)
self.singerLine.setGeometry(QtCore.QRect(460, 270, 181, 20))
self.singerLine.setObjectName("singerLine")
self.singerLabel = QtWidgets.QLabel(musicAdmin)
self.singerLabel.setGeometry(QtCore.QRect(370, 270, 47, 13))
self.singerLabel.setObjectName("singerLabel")
self.titleLabel = QtWidgets.QLabel(musicAdmin)
self.titleLabel.setGeometry(QtCore.QRect(370, 240, 47, 13))
self.titleLabel.setObjectName("titleLabel")
self.availableSongs = QtWidgets.QPushButton(musicAdmin)
self.availableSongs.setGeometry(QtCore.QRect(390, 320, 261, 51))
self.availableSongs.setObjectName("availableSongs")
self.warningLabel = QtWidgets.QLabel(musicAdmin)
self.warningLabel.setGeometry(QtCore.QRect(410, 210, 261, 20))
self.warningLabel.setText("")
self.warningLabel.setObjectName("warningLabel")
self.exitButton.clicked.connect(self.CloseThis)
self.availableSongs.clicked.connect(self.addSong)
self.retranslateUi(musicAdmin)
QtCore.QMetaObject.connectSlotsByName(musicAdmin)
def retranslateUi(self, musicAdmin):
_translate = QtCore.QCoreApplication.translate
musicAdmin.setWindowTitle(_translate("musicAdmin", "Music Library"))
self.singerLabel.setText(_translate("musicAdmin", "Singer:"))
self.titleLabel.setText(_translate("musicAdmin", "Title:"))
self.exitButton.setText(_translate("AdminUI", "Exit"))
self.availableSongs.setText(_translate("musicAdmin", "Add Song"))
def addSong(self):
warn = QMessageBox()
voicer = self.singerLine.text()
title = self.titleSong.text()
temp = open("musics.csv", "a")
tempRead = csv.reader(temp)
song = voicer + ',' + title
songs = voicer + title
if songs != '': # if song has string
if voicer != '' or title != '':
temp.write(song + '\n')
self.warningLabel.setText("Song is added")
else:
warn.setWindowTitle("System")
warn.setText("Song Details are required")
warn.setIcon(QtWidgets.QMessageBox.Warning)
warn.exec_()
else:
warn.setWindowTitle("System")
warn.setText("Song Details are required")
warn.setIcon(QtWidgets.QMessageBox.Warning)
warn.exec_()
def CloseThis(self, event):
reply = QuitMessage().exec_()
if reply == QtWidgets.QMessageBox.Yes:
self.musicAdmin.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
musicAdmin = QtWidgets.QDialog()
ui = musicAdminUI()
ui.setupUi(musicAdmin)
musicAdmin.show()
sys.exit(app.exec_())