No file opening with a random music file player

时间:2019-02-24 00:23:02

标签: python python-3.x audio-player

I am finding difficulty in coding a Python music player than plays a random artist. I have the mp3 files in a directory, but choosing them randomly is proving to be difficult. Any help would be appreciated, thank you!

from tkinter import *

import os

import random

master = Tk()
var = IntVar()
var.set(1)

def quit_loop():
    print ("Selection:"),var.get()

    global selection
    selection = var.get()
    master.quit()

Label(master, text = "What is your mood?").grid(row=0, sticky=W)

Radiobutton(master, text = "Sad", variable=var, value = 1).grid(row=1,sticky=W)

Radiobutton(master, text = "Happy", variable=var, value = 2).grid(row=2, sticky=W)

Radiobutton(master, text = "Sleepy or Bored", variable=var, value = 3).grid(row=3, sticky=W)

Radiobutton(master, text = "Scared", variable=var, value = 4).grid(row=4, sticky=W)

Radiobutton(master, text = "Pumped or Angry", variable=var, value = 5).grid(row=5, sticky=W)

Button(master, text = "Show me the music!", command=quit_loop).grid(row=7, sticky=W)


master.mainloop()

if selection == 1:
    print ("My Value is equal to one.")

elif selection == 2:
    print ("My value is equal to two.")

elif selection == 3:
    print ("My value is equal to three.")

elif selection == 4:
    print ("My value is equal to four.")

elif selection == 5:
    random.choice(os.listdir(r"C:\Users\Steven\Desktop\Dony Proj\Mood DJ\Do you like any of these\Beastie Boys"))

2 个答案:

答案 0 :(得分:0)

好吧,您正在呼叫random.choice,但没有将返回值分配给任何东西。

答案 1 :(得分:0)

您似乎已经拥有了它;我在机器上将目录设置为一个带有一些示例音乐文件的目录,然后将文件的随机选择存储到变量中,并添加了一条打印语句以查看其内容:

elif selection == 5:
    song_choice = random.choice(os.listdir(r"E:\music"))
    print(song_choice)

然后从我的音乐目录中打印随机歌曲的名称!