Trying to create a ID3-Tag editor. TypeError: Missing filename or fileobj argument

Trying to create a ID3-Tag editor. TypeError: Missing filename or fileobj argument

我正在创建一个 tageditor,它以 "before" "after" 样式在不同的文本行中显示 mp3 文件的 ID3 标签。如果没有可用的标签,则不显示任何内容。您还可以编辑 "after" 文本行,对它们所做的任何更改都应保存到文件中,但是当我按下 button2 时,我得到了底部回溯。如何将 lines6-10 保存为新的 "audio["title"]、audio["artist"]" 等? 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 查询。

在方法on_button2_clicked中,谎言对象基本上是None。 要获得正确的,请在 on_button1_clicked 中分配时使用关键字 global。 (你实际上永远不应该!而是创建一个属性来存储它并通过 self.lied 或类似的东西访问它)

此外,由于 self 关键字,我假设这两个函数实际上是 class 方法,而您只是在复制粘贴时弄错了缩进。

基本上是范围引起的错误。