How to place functions inside tkinter drop down list?

Viewed 157

So, my purpose with this program is to store, whats displayed on the main frame, inside the drop down list, which shows the list of the rappers. So instead of just being displayed on the main frame, I want it to be hidden until the particular name of the artist (to whom the the picture, play button and stop button belongs to) is picked in the list. Please help me with this. Thank you.

This is the screenshot of the program atm. So far there are 4 things displayed on the main frame The list which was made with tkinter, picture, play button and the stop button.

import pygame
from tkinter import *
from PIL import ImageTk, Image

root = Tk()

root.title("Hip-Hop-Flow")
root.iconbitmap("22.ico")
root.geometry("800x600")
pygame.mixer.init()


class Vini:

    def __init__(self):

        self.rappers = ["Vinnie Paz", "Sean Price", "Big L", "Canibus"]
        self.menu = StringVar()
        self.menu.set("List of rappers")

        siis_chamoshla = OptionMenu(root, self.menu, *self.rappers)
        siis_chamoshla.configure(width=30, bg="white")
        siis_chamoshla.pack()

    def vinispicture(self):
        self.vinniephoto = ImageTk.PhotoImage(Image.open("vini.gif"))
        label = Label(image=self.vinniephoto)
        label.pack()

    def play_vinnie(self):
        pygame.mixer.music.load("vinnie flow.mp3")
        pygame.mixer.music.play(loops=0)

    def stop_vinnie(self):
        pygame.mixer.music.stop()

    def design(self):
        play_button = Button(root, text="Play", font=("Helvetica", 20),
                             command=self.play_vinnie)
        play_button.pack(pady=20)
        stop_button = Button(root, text="Stop", font=("Helvetica", 20),
                             command=self.stop_vinnie)
        stop_button.pack(pady=20)


pazienza = Vini()
pazienza.vinispicture(), pazienza.play_vinnie(), pazienza.stop_vinnie(), pazienza.design()
root.mainloop()
1 Answers

You can create a frame to hold the image and the buttons and this frame is initially hidden.

Then associate a function on the OptionMenu via command option. The function will be executed whenever option is selected. Inside that function you can load the corresponding image and song based on the selected artist:

import pygame
import tkinter as tk
from PIL import ImageTk

root = tk.Tk()
root.title("Hip-Hop-Flow")
root.iconbitmap("22.ico")
root.geometry("800x600")

pygame.mixer.init()


class Player:
    def __init__(self, master=None):

        self.rappers = ["Vinnie Paz", "Sean Price", "Big L", "Canibus"]

        self.menu = tk.StringVar(value="List of rappers")
        siis_chamoshla = tk.OptionMenu(master, self.menu, *self.rappers, command=self.on_selected)
        siis_chamoshla.configure(width=30, bg="white")
        siis_chamoshla.pack()

        # play panel, initially hidden
        self.panel = tk.Frame(master)
        self.image_label = tk.Label(self.panel)
        self.image_label.pack()
        tk.Button(self.panel, text="Play", font="Helvetica 20", command=self.play_song).pack(pady=20)
        tk.Button(self.panel, text="Stop", font="Helvetica 20", command=self.stop_song).pack(pady=20)

    def on_selected(self, artist):
        # assume the image name is <artist_name>.gif
        self.photo = ImageTk.PhotoImage(file=artist+".gif")
        self.image_label.configure(image=self.photo)
        # show the panel
        self.panel.pack()
        # assume the song name is <artist_name>.mp3
        pygame.mixer.music.load(artist+".mp3")

    def play_song(self):
        pygame.mixer.music.play(loops=0)

    def stop_song(self):
        pygame.mixer.music.stop()

pazienza = Player(root)

root.mainloop()

Note that I have used import tkinter as tk instead of from tkinter import * because wildcard import is not recommended.

Related