2017-06-16 50 views
2

我想從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_()) 
+0

您想要做的是將您輸入的文本分別添加到titleSong和singerLine,並添加到列3和4中的新行。我是對的? – eyllanesc

+0

是的,它是..... –

+0

嘗試使用我的回答 – eyllanesc

回答

0

要添加一個新行,我們必須使用writerow re根據您的要求提供一個列表,應該只填寫第3列和第4列中的數據。爲此,我們用csv創建一個預定義大小的列表並填寫數據。

def addSong(self): 
    warn = QMessageBox() 
    voicer = self.singerLine.text() 
    title = self.titleSong.text() 
    temp = open("musics.csv", mode="r", errors='ignore') # ignore mistakes 

    reader = csv.reader(temp) 
    n_cols = len(next(reader)) #numbers of columns 

    temp = open("musics.csv", "a") 
    writer = csv.writer(temp) 

    if voicer != '' or title != '': 
     data = [""]*n_cols # create a list of empty string 
     data[2] = title # add 3 column 
     data[3] = voicer # add 4 column 
     writer.writerow(data) 
     self.warningLabel.setText("Song is added") 
    else: 
     warn.setWindowTitle("System") 
     warn.setText("Song Details are required") 
     warn.setIcon(QtWidgets.QMessageBox.Warning) 
     warn.exec_() 
+0

由於某種原因,當調試程序到達_temp_時,我的站點停止,在 temp = open(「musics.csv」,「a」) –

+0

我發現它很奇怪,要測試它,您可以共享.csv文件 – eyllanesc

+0

更新我的代碼。請再試一次 – eyllanesc

相關問題