0

I am making a music player application using both tkinter and pygame. I found online some ways to implement these two modules (https://mail.python.org/pipermail/tutor/2012-September/091417.html, Using pygame features in Tkinter), but my buttons are not visible. The pygame screen shows up fine, but I cannot get the buttons to show up.

Here is shortened version of my code:

from tkinter import *
import tkinter.filedialog as tk
import tkinter.messagebox as tk2
import os
import pygame

playlist = ['song1.mp3', 'song2.mp3', 'song3.mp3', 'song4.mp3']

class Application(Frame):

    def __init__(self,master):
        super(Application, self).__init__(master)

        #create widgets
        self.playlistbox = Listbox(self, selectmode = SINGLE) #listbox
        for song in playlist:
            self.playlistbox.insert(END, song)
        self.playlistbox.grid(row = 1)
        self.playButton = Button(self, text = 'Play', command = self.play)  #button
        self.playButton.grid(row=1, column = 1)

        #pygame init
        embed = tk.Frame(root, width = 200 , height = 200)
        embed.pack()
        embed.pack(side = LEFT)
        os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
        os.environ['SDL_VIDEODRIVER'] = 'windib'
        pygame.display.init() 
        windowSurface = pygame.display.set_mode((200,100))
        pygame.mouse.set_visible(True)

    def play(self):   #play song function
        pygame.mixer.music.stop()
        selectedSongs = self.playlistbox.curselection()
        pygame.mixer.music.load(self.playlistbox.get(0))
        pygame.mixer.music.play(0, 0.0)

root = Tk()
root.title('Text Editor')
root.geometry('700x600')
app = Application(root)
app.mainloop()
Cœur
  • 37,241
  • 25
  • 195
  • 267
TheEyesHaveIt
  • 980
  • 5
  • 16
  • 33
  • 1
    Amr Ayman's answer below is probably good advice, but if you are wondering your problem is that you forgot to include a `self.pack()` (not the embedded frame but the main tkinter frame) right after you initialize the listbox and buttons. – KSab Jul 22 '14 at 18:51

1 Answers1

1

What you're doing here is really an overkill ..
I coded a music player using the exact same things you're using here .. python, pygame and tkinter ..

You won't need initializing pygame's display module, becuase believe me, you don't need it ..
I'll tell you what I did ..

I used pygame's music module using the Sound class to play the tracks ..
I created a class called SoundPanel which inherits from the Frame class to group: a volume scale, a play/stop checkbutton, and a label to view the first 23 letters of the track name and add some dots if the track name is larger than that ..
And to make that usable, I added an open file(s) button to add track(s) (with a file dialog using tkinter) to the view (i.e. Create a new SoundPanel object that plays the track) and a open folder button to add all files with a certain extension (defined by the user using a dialog containing a text box) to the view ..

I don't think you need any more than that ..
I don't say you have to mimic my solution but I think yours is too complicated ..
By the way, the widgets aren't displaying because you have to pack the frame when you're done creating the object ..

Amr Ayman
  • 1,129
  • 1
  • 8
  • 24
  • Thank you for your answer! I am using pygame's display module because I plan on doing some graphics/design work on the application, but you're right it's overkill to use it just for audio. – TheEyesHaveIt Jul 22 '14 at 18:53