【问题标题】:How to fix the AttributeError that occurs when no music file is added to the pygame mixer?pygame混音器中未添加音乐文件时出现的AttributeError问题如何解决?
【发布时间】:2021-09-19 12:05:56
【问题描述】:

这是用于使用 tkinter 从 .WAV 文件播放音乐,但是当我按下播放按钮而不导入任何音频文件时,会出现以下错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Python3/lib/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Files/Stack Overflow/no-music-file-error.py", line 23, in <lambda>
    file_play.config(text="Play", command=lambda:[play_file()])
  File "/Files/Stack Overflow/no-music-file-error.py", line 30, in play_file
    pygame.mixer.music.load(window.filename)
  File "/Python3/lib/tkinter/__init__.py", line 2354, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'filename'

程序停止。

我希望它产生一条消息,告诉它在按下按钮时没有导入音频文件时它不起作用。

from tkinter import *
import tkinter as tk
from tkinter.ttk import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
import os
import pygame

#Variable

#function
def start():
    global x
    x = 1
    pygame.mixer.init()
    file_select.place(x=200, y=10)
    file_play.place(x=280, y=10)
    volume_S.place(x=150, y=35)
    file_select.config(text="Open FIle", command=lambda:[open_file()])
    file_play.config(text="Play", command=lambda:[play_file()])

def open_file():
    window.filename = filedialog.askopenfilename(initialdir = "/", title = "Select file",filetypes = (("all files","*.*"), ("jpeg files","*.jpg")))
    print(window.filename)

def play_file():
    pygame.mixer.music.load(window.filename)
    pygame.mixer.music.play()
    pygame.mixer.music.set_volume(0)

def volumeset(self):
    pvolume=float(volume_S.get())
    pygame.mixer.music.set_volume(pvolume*0.1)

#File_location
print (os.path.dirname(os.path.realpath(__file__)) )

#Windowm
window = tk.Tk()
window.geometry("796x448")
window.resizable(0,0)

#Widgets_setup
file_select = tk.Button(window, font=("Calbri", 10))
file_play = tk.Button(window, font=("Calbri", 10))
volume_S = tk.Scale(window, variable=int, command=volumeset, orient="horizontal", showvalue=False, tickinterval=1, to=10, length=200)

#Window_mainloop
start()
window.mainloop()

【问题讨论】:

  • '"发生错误..."* - 错误信息是什么?
  • 如果您想在出现 filenotfounderror 时输出,请尝试阅读链接。
  • 如果您不edit,您的问题可能会被关闭,并尽可能添加错误消息的副本,包括对导致问题的语句的完整回溯。
  • 那家伙没有回答,无论如何谢谢你看看它。@martineau

标签: python tkinter


【解决方案1】:

为了避免你得到AttributeError,你需要改变play_file()函数来处理用户没有先打开文件的情况。当然,最好的方法是使用用户友好的错误消息——tkinter 有几个内置的(这里有一些关于其Standard Dialogs 的文档)。

from tkinter.messagebox import showerror  # Add this near top of file with other imports.

然后像这样修改函数:

def play_file():
    try:
        audio_filename = window.filename
    except AttributeError:
        showerror(title='File playing error', message="Please open a file to play first!")
        return
    pygame.mixer.music.load(audio_filename)
    pygame.mixer.music.play()

在没有先打开音频文件的情况下按下播放按钮的新结果:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多