tkinter listBox插入遇到类继承问题

时间:2018-12-08 18:45:30

标签: python tkinter audio-player

我目前正在尝试用Python创建一个简单的Jukebox / Music Player,并遇到类继承和tkinter的listBox方法的一些问题。

我必须使用两个类JukeboxGUI和JukeboxContent(内容不完整)。 JukeboxContent类继承了JukeboxGUI:

import os
import pygame
#from PIL import Image
from tkinter.filedialog import askdirectory
from tkinter import *
import eyed3


class JukeboxGUI: 
    def __init__(self, window):
        self.photo = PhotoImage(file = "//Users//nukhbahmajid//Desktop//Jukebox//background2.gif")
        self.canvas = Canvas(width = 1000, height = 1000, bg = "black")
        self.label = Label(self.canvas, text = "Jukebox")
        self.listBox = Listbox(self.canvas)
        self.playPauseButton = Button(self.canvas, text = "Play / Pause")
        self.nextButton = Button(self.canvas, text = "Next Song")
        self.previousButton = Button(self.canvas, text = "Previous Song")
        self.stopButton = Button(self.canvas, text = "Stop")
        self.labelVar = StringVar()
        self.songLabel = Label(self.canvas, textvariable = self.labelVar, width = 20)

    def constructButtons(self):
        self.canvas.pack()
        self.canvas.create_image(0,0, image = self.photo, anchor = NW)
        self.label = self.canvas.create_window(325, 40, anchor = NW, window = self.label)
        self.listBox = self.canvas.create_window(270, 80, anchor = NW, window = self.listBox)
        self.playPauseButton = self.canvas.create_window(110, 120, anchor = NW, window = self.playPauseButton)
        self.nextButton = self.canvas.create_window(500, 120, anchor = NW, window = self.nextButton)
        self.previousButton = self.canvas.create_window(500, 180, anchor = NW, window = self.previousButton)
        self.stopButton = self.canvas.create_window(130, 183, anchor = NW, window = self.stopButton)
        self.songLabel = self.canvas.create_window(268, 268, anchor = NW, window = self.songLabel)


class JukeboxContent(JukeboxGUI):
    def __init__(self, window):
        #JukeboxGUI.__init__(self, window)
        super(JukeboxContent, self).__init__(window)
        listOfSongs = []
        songTitles = []
        num_songs = len(songTitles)
        self.index = 1
        self.listOfSongs = listOfSongs
        self.songTitles = songTitles
        self.directoryAsk = askdirectory()
        self.num_songs = num_songs
        self.Error_NoMP3s = "No \".mp3\" files found."

    def directoryChooser(self):
        self.directoryAsk
        os.chdir(self.directoryAsk)

        for files in os.listdir(self.directoryAsk):
            if files.endswith(".mp3"):
                realdir = os.path.realpath(files)
                audioTag = eyed3.load(realdir)
                self.songTitles.append(audioTag.tag.title)
                self.listOfSongs.append(files)
                print(files) ## comment out or remove later
                print("These are the Song titles:", self.songTitles) #check if the list gets appended

        for items in self.songTitles:
            self.listBox.insert(END, items) ## the list doesn't get inserted into the listbox


        pygame.mixer.init()
        pygame.mixer.music.load(self.listOfSongs[0])
        self.labelVar.set(self.songTitles[0])
        print("The label variable:", self.labelVar) ## the variable isn't set either
        print("The label is accessible:", self.songLabel) ## songLabel doesn't get updated
        pygame.mixer.music.play() # the song doesn't play 


if __name__ == "__main__":
    window = Tk()
    window.geometry("700x500+450+200")
    window.title("Jukebox")
    constructGUI = JukeboxGUI(window)
    constructGUI.constructButtons()
    initiateJukebox = JukeboxContent(window)
    initiateJukebox.directoryChooser()
    window.mainloop()

当我尝试访问父类的属性时,例如将歌曲插入列表框中,这里没有特别的错误,但是列表也未放入框中

第二,当我更新标签变量和songLabel并尝试稍后打印出来以查看是否已实现时,将在终端上打印以下内容:

The label variable: PY_VAR1
The label is accessible: .!canvas2.!label2

这首歌也不会播放。可能是什么问题呢?请帮帮我!如果有帮助,请看下面的界面图片: JukeboxInterface

0 个答案:

没有答案