使用tkinter实现音乐播放器

使用tkinter实现音乐播放器


python实现

使用tkinter实现音乐播放器可以让用户在图形化界面下方便地播放音乐。以下是一个简单的实现步骤:

import pygame
import tkinter as tk
import os.path
from tkinter import filedialog
import os
class VoiceRecognitionGUI:
    def __init__(self, master):
        self.master = master
        master.geometry('400x200')
        # 控制台输出
        self.label = tk.Label(master, text="请选择音乐文件")
        self.label.place(x=100, y=45)

        self.button = tk.Button(master, text="选择音频",bg="lightblue", command=self.choose_file)
        self.button.place(x=20, y=45)

        self.play_button = tk.Button(master, text="播放", bg="lightblue",command=self.play_music)
        self.play_button.place(x=130, y=80)

        self.stop_button = tk.Button(master, text="停止",bg = "OrangeRed", command=self.stop_music)
        self.stop_button.place(x=290, y=80)

    def choose_file(self):
        # 弹出文件选择窗口
        file_path = filedialog.askopenfilename(
            filetypes=[("Audio files", "*.wav;*.mp3;*.ogg")])
        self.label.config(text="已选择文件: " + f"{os.path.basename(file_path)}")
        self.music_file = file_path

    def play_music(self):
        pygame.mixer.init()
        self.playing = False
        if not self.playing:
            # 载入音乐文件
            pygame.mixer.music.load(self.music_file)
            # 播放音乐
            pygame.mixer.music.play()
            self.playing = True
    def stop_music(self):
        if self.playing:
            # 停止音乐
            pygame.mixer.music.stop()
            self.playing = False


if __name__ == '__main__':
    root = tk.Tk()
    gui = VoiceRecognitionGUI(root)
    root.mainloop()


界面展示
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一寸光阴不可轻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值