我想從singerLine和titleSong一定的信息以CSV添加到某列我的數據庫看起來像這樣的csv IMG我要添加兩個lineEdit信息是爲csv中某列在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_())
您想要做的是將您輸入的文本分別添加到titleSong和singerLine,並添加到列3和4中的新行。我是對的? – eyllanesc
是的,它是..... –
嘗試使用我的回答 – eyllanesc