我正在创建一个tageditor,在""之前显示一个mp3文件的ID3-Tags。 "后"不同textLines中的样式。如果没有可用的标签,则不显示任何内容。你也可以编辑""" textLines和对它们所做的任何更改都应该保存到文件中,但当我按下button2时,我会得到底部的回溯。如何将第6-10行保存为新的"音频["标题"],音频["艺术家"]"等等? This is the GUI
import sys
import easygui
import mutagen
from mutagen.easyid3 import EasyID3
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QLineEdit
lied = None
error = "No ID3-Tag available."
class TrackTag1(QDialog):
def __init__(self):
super(TrackTag1,self).__init__()
loadUi("TrackTag1.ui",self)
self.setWindowTitle("TrackTag")
@pyqtSlot()
def on_button1_clicked(self):
#root.fileName = filedialog.askopenfilename( filetypes = ( ("Musik Dateien", "*.mp3"), ("Alle Dateien", "*.*") ) )
#print(easygui.fileopenbox('MP3 Dateien','', '*.MP3'))
lied = easygui.fileopenbox('MP3 Dateien','', '*.MP3')
audio = EasyID3(lied)
self.line0.setText(lied) #printing filepath to line0
try:
self.line1.setText(str(audio["title"]).strip("[']")) #printing the ID3 tags after they've been stripped of "['']"
self.line6.setText(str(audio["title"]).strip("[']"))
except:
KeyError
self.line1.setText(error)
try:
self.line2.setText(str(audio["album"]).strip("[']"))
self.line7.setText(str(audio["album"]).strip("[']"))
except:
KeyError
self.line2.setText(error)
try:
self.line3.setText(str(audio["date"]).strip("[']"))
self.line8.setText(str(audio["date"]).strip("[']"))
except:
KeyError
self.line3.setText(error)
try:
self.line4.setText(str(audio["artist"]).strip("[']"))
self.line9.setText(str(audio["artist"]).strip("[']"))
except:
KeyError
self.line4.setText(error)
try:
self.line5.setText(str(audio["genre"]).strip("[']"))
self.line10.setText(str(audio["genre"]).strip("[']"))
except:
KeyError
self.line5.setText(error)
def on_button2_clicked(self):
audio = EasyID3(lied)
audio["title"] = self.line6.text()
audio.save()
app=QApplication(sys.argv)
widget=TrackTag1()
widget.show()
sys.exit(app.exec_())
app=QApplication(sys.argv)
widget=TrackTag1()
widget.show()
sys.exit(app.exec_())
当我按下保存更改按钮时,我收到了此追溯:
Traceback (most recent call last):
File "<string>", line 69, in on_button2_clicked
File "h:\program files (x86)\python\lib\site-packages\mutagen\_util.py", line 139, in wrapper
writable, create) as h:
File "h:\program files (x86)\python\lib\contextlib.py", line 59, in __enter__
return next(self.gen)
File "h:\program files (x86)\python\lib\site-packages\mutagen\_util.py", line 270, in _openfile
raise TypeError("Missing filename or fileobj argument")
TypeError: Missing filename or fileobj argument
目前,您应该只能编辑代码,但我打算尽快实施MusicBrainz查询。
答案 0 :(得分:0)
在on_button2_clicked方法中,lied对象基本上是None。 要获得正确的,请在on_button1_clicked中分配关键字global。 (你实际上从来不应该这样做!而是创建一个属性来存储它并通过self.lied或类似的方式访问它)
另外,我假设由于self关键字而导致这两个函数是infact类方法,并且在复制粘贴时你只是缩进了缩进。
基本上是由于范围引起的错误。