我正在用Python构建一个简单的音频播放器。但是,当我添加一首歌曲时,数据字段为空,但是当我按下播放按钮时,歌曲就能启动。此外,单选按钮IntVar无论如何都返回0。所以这个问题与StringVars和IntVar有关。我认为这与窗户的范围有关,但即使我不摧毁它,它仍然无法工作。我也尝试使用Global作为变量。任何帮助,将不胜感激。此外,我知道我的代码没有经过优化,但请仅评论我可能遇到问题的原因。
from tkinter import *
import pygame
from MusicPlayer.DoublyCircularLLClass import *
from tkinter import filedialog
from tkinter import messagebox
class songClass:
def __init__(self, songname, songartist, songalbum, songfilename):
self.name = songname
self.artist = songartist
self.album = songalbum
self.filename = songfilename
return
class MusicPlayer:
def __init__(self):
pygame.mixer.init()
self.nodeList = DoublyLinkedList()
root = Tk()
root.title('The Better iTunes')
paused = False
database = open('SongDatabase.txt', 'r')
read = database.readlines()
database.close()
for line in read:
read = line.strip()
tempsong = read.split(',')
self.nodeList.addToRear(songClass(tempsong[0], tempsong[1], tempsong[2], tempsong[3]))
self.currentSong = self.nodeList.firstNode
pygame.mixer.music.load(self.nodeList.firstNode.data.filename)
self.nameLabel = Label(root, text = self.currentSong.data.name)
self.nameLabel.pack(side= LEFT)
self.artistLabel = Label(root, text = self.currentSong.data.artist)
self.artistLabel.pack(side= LEFT)
self.albumLabel = Label(root, text = self.currentSong.data.album)
self.albumLabel.pack(side= LEFT)
#albumCoverLabel = Label(root, text = "")
Button(root, text='Play', command = self.playsong).pack(side= LEFT)
Button(root, text='Stop', command = self.stopsong).pack(side= LEFT)
self.pauseB = Button(root, text='Pause', command = self.pausesong)
self.pauseB.pack(side= LEFT)
Button(root, text='Next', command = lambda direction ="Next": self.updateSong(direction)).pack(side= LEFT)
Button(root, text='Previous', command = lambda direction ="Prev": self.updateSong(direction)).pack(side= LEFT)
Button(root, text='Delete', command = self.delete).pack(side= LEFT)
Button(root, text='Add', command = self.add).pack(side= LEFT)
Button(root, text='Search', command = self.search).pack(side= LEFT)
root.mainloop()
def playsong(self):
pygame.mixer.music.play()
self.pauseB["text"] = "Pause"
def pausesong(self):
global paused
if paused:
pygame.mixer.music.unpause()
self.pauseB["text"] = "Pause"
paused = False
else:
pygame.mixer.music.pause()
self.pauseB["text"] = "Resume"
paused = True
def stopsong(self):
pygame.mixer.music.stop()
self.pauseB["text"] = "Pause"
def delete(self):
self.currentSong = self.currentSong.next
self.nodeList.removeCurrent(self.currentSong.prev)
self.nameLabel["text"] = self.currentSong.data.name
self.artistLabel["text"] = self.currentSong.data.artist
self.albumLabel["text"] = self.currentSong.data.album
self.pauseB["text"] = "Pause"
return
def add(self):
self.addWindow = Toplevel()
self.addWindow.title('Enter Song Info')
addName = StringVar()
addArtist = StringVar()
addAlbum = StringVar()
nameEntry = Entry(self.addWindow, textvariable = addName).pack(side=LEFT)
artistEntry = Entry(self.addWindow, textvariable = addArtist).pack(side=LEFT)
albumEntry = Entry(self.addWindow, textvariable = addAlbum).pack(side=LEFT)
filenameLocation = filedialog.askopenfilename()
Button(self.addWindow, text='Submit', command = lambda name = addName.get(), artist = addArtist.get(), album = addAlbum.get(), filename = filenameLocation: self.updateList(name, artist, album, filename)).pack(side= LEFT)
return
def updateList(self, name, artist, album, filename):
self.nodeList.addToRear(songClass(name, artist, album, filename))
self.addWindow.destroy()
return
def search(self):
self.searchWindow = Toplevel()
self.searchWindow.title('Search Song')
selection = IntVar()
selection.set(0)
radioA = Radiobutton(self.searchWindow, variable=selection, value=0)
radioB = Radiobutton(self.searchWindow, variable=selection, value=1)
radioA.pack()
radioB.pack()
searchInfo = StringVar()
searchEntry = Entry(self.searchWindow, textvariable=searchInfo).pack(side=LEFT)
Button(self.searchWindow, text='Search', command=lambda info=searchInfo.get(), type=selection.get(): self.updateSearch(type, info)).pack(side=LEFT)
return
def updateSearch(self, type, info):
self.searchWindow.destroy()
searchSong = self.nodeList.firstNode
print(type)
if type == 0:
for i in range(self.nodeList.size):
if searchSong.data.name == info:
self.currentSong = searchSong
self.nameLabel["text"] = self.currentSong.data.name
self.artistLabel["text"] = self.currentSong.data.artist
self.albumLabel["text"] = self.currentSong.data.album
self.pauseB["text"] = "Pause"
break
else:
print(searchSong.data.name)
print(info)
searchSong = searchSong.next
messagebox.showinfo('Error', 'Song not found')
else:
for i in range(self.nodeList.size):
if searchSong.data.artist == info:
self.currentSong = searchSong
self.nameLabel["text"] = self.currentSong.data.name
self.artistLabel["text"] = self.currentSong.data.artist
self.albumLabel["text"] = self.currentSong.data.album
self.pauseB["text"] = "Pause"
break
else:
searchSong = searchSong.next
messagebox.showinfo('Error', 'Artist not found')
return
def updateSong(self, direction):
if direction == "Next":
pygame.mixer.music.load(self.currentSong.next.data.filename)
self.currentSong = self.currentSong.next
else:
pygame.mixer.music.load(self.currentSong.prev.data.filename)
self.currentSong = self.currentSong.prev
self.nameLabel["text"] = self.currentSong.data.name
self.artistLabel["text"] = self.currentSong.data.artist
self.albumLabel["text"] = self.currentSong.data.album
self.pauseB["text"] = "Pause"
return
m1 = MusicPlayer()
答案 0 :(得分:2)
Button(self.addWindow, text='Submit', command=lambda name=addName.get(), artist=addArtist.get(), album=addAlbum.get(), filename=filenameLocation: self.updateList(name, artist, album, filename)).pack(side=LEFT)
你的命令是巨大的lambda函数。 lambda函数有很多默认参数。在创建函数时(而不是在调用函数时)会评估这些默认参数。那时,您的变量(addName
,addArtist
等)仍为空。
您需要命令功能仅在单击按钮时评估addName.get()
(因此在lambda函数中,但我建议仅使用常规函数),而不是在默认参数列表中。
附带问题:如果您的条目太短暂,您不需要使用StringVars。您可以直接使用my_entry.get()
。