0

Using Tkinter, os, and, pygame modules im finding all files and making a button to play the file because it will be an mp3 file but every time the function overwrites itself so I want to be able to write a function but inside the command parameter in the Tkinter button so it isn't a function but it operates like one

the code i already have:

import os
import pygame
import tkinter as tk
dir_path = os.path.dirname(os.path.realpath(__file__))
d = dir_path+"\\mp3s"
root = tk.Tk()
frame=tk.Frame(root)
root.title("title")
frame.pack()
for path in os.listdir(d):
    full_path = os.path.join(d, path)
    full_name = os.path.basename(full_path)
    def playsong():
        pygame.init()
        pygame.mixer.music.load(full_path)
        pygame.mixer.music.play()
    play = tk.Button(frame,
                     text=full_name,
                     command=playsong)
    play.pack()
root.mainloop()

the function inside the for statement is getting overwritten and I knew this would happen but I was still going to try this works for one file but I want a bunch of different files inside of the folder named "mp3s"

the rest of the code works this is the part that does not

    def playsong():
       pygame.init()
       pygame.mixer.music.load(full_path)
       pygame.mixer.music.play(
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
william
  • 55
  • 7

1 Answers1

0

I wanted to run multiple functions in a line more specifically than lines and I figured out how to do this using lambda

import tkinter as tk

root = tk.Tk()
frame=tk.frame(root)
button = tk.Button(frame, text="name", command= lambda: [func1(), func2()])
william
  • 55
  • 7