【问题标题】:Trying to create a ID3-Tag editor. TypeError: Missing filename or fileobj argument尝试创建 ID3-Tag 编辑器。 TypeError:缺少文件名或文件对象参数
【发布时间】:2018-11-08 14:52:44
【问题描述】:

我正在创建一个标签编辑器,它在不同的 textLines 中以“之前”“之后”样式显示 mp3 文件的 ID3 标记。如果没有可用的标签,则不显示任何内容。您还可以编辑“之后”文本行,对它们所做的任何更改都应保存到文件中,但是当我按下 button2 时,我得到了底部回溯。如何将第 6-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 查询。

【问题讨论】:

    标签: python typeerror id3 musicbrainz mutagen


    【解决方案1】:

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

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

    基本上是由于范围造成的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      相关资源
      最近更新 更多