首页 > 解决方案 > AudioSegment.from_file 由于某种原因被覆盖?

问题描述

我在我的程序中使用了 AudioSegment.from_file 函数:sound = AudioSegment.from_file(audio_path, format="m4a")

我还实现了一个我创建的文件:import GUI_log

在这个文件中,我有下一节课:

class TextHandler(logging.Handler):
    """This class allows you to log to a Tkinter scroll_text or Scrolledscroll_text widget"""
    def __init__(self, scroll_text):
        # run the regular Handler __init__
        logging.Handler.__init__(self)

        # Store a reference to the scroll_text it will log to
        self.scroll_text = scroll_text

    def emit(self, record):
        msg = self.format(record)
        tag = ""
        if record.levelno == ACTION:
            tag = "ACTION"
        def append():
            self.scroll_text.configure(state='normal')
            self.scroll_text.insert(Tkinter.END, msg + '\n', tag)
            self.scroll_text.configure(state='disabled')
            # Autoscroll to the bottom
            self.scroll_text.yview(Tkinter.END)     
        # This is necessary because we can't modify the scroll_text from other threads
        self.scroll_text.after(0, append)

出于某种原因,当我执行我提到的第一行(带有 的那一行from_file)时,类emit中的函数TextHandler被执行。

任何人都知道它的原因是什么以及如何解决它?

编辑:我发现我覆盖了这段代码中的某个地方:

# Create Tkinter object and ScrolledText
    root =Tkinter.Tk()
    st = ScrolledText.ScrolledText(root, state='disabled')
    st.configure(font='TkFixedFont')
    st.pack()
    st.tag_config("ACTION", foreground="dark green")

    # Create text handler
    text_handler = TextHandler(st)
    formatter = logging.Formatter('%(asctime)s    IP: %(IP)-15s PORT: %(PORT)-5s    MESSAGE: %(message)s', datefmt='%Y-%m-%d,%H:%M:%S') #.%(msecs)03d
    text_handler.setFormatter(formatter)

    # Add action level
    logging.addLevelName(ACTION, "ACTION")
    def action(self, message, *args, **kws):
        self._log(ACTION, message, args, **kws) 
    logging.Logger.action = action

    # Create logger
    logger = logging.getLogger()
    logger.addHandler(text_handler)
    logger.setLevel(logging.DEBUG)

    return (root, logger)

标签: pythonpython-2.7loggingpydub

解决方案


在互联网上滚动后,我发现 pydub 使用日志记录,我可能会覆盖记录器。为了解决这个问题,我改变了这一行logger = logging.getLogger()logger = logging.getLogger("SOMENAME")


推荐阅读