0

I actually made a small game in Python. Actually, I work on the music and I want to get different music in the main menu in that game.

To test this, I make a list of 1 music in a "music" variable (in the final program I want to have multiple music for the menu and game that's why I use a list).

When the program starts a thread is launched for playing the music when no music is played.

When I click on the "Jouer" button that stops the music and changes the music list.

But the thread doesn't read the new music list I want to know why.

import tkinter as tk
import pygame 
import numpy as np
import random
import threading
import os
musicc = ["music/main/main1.mp3"]
pygame.mixer.init()
def music():
   if pygame.mixer.music.get_busy() == False:
      print(musicc)
      random_music = random.choice(musicc)
      pygame.mixer.music.load(random_music)
      pygame.mixer.music.play(loops=1) 
   threading.Timer(1, music).start() 
   
def affichervaleur():
      pygame.mixer.music.stop()
      musicc = ["music/main/main2.mp3"]
      print('changed')

def leave():
   os.exit()

graph = tk.Tk()
b = threading.Thread(name='music', target=music())
b.start()
tk.Button(graph, text="Quit", command=leave).grid(row=3, column=0, sticky=tk.W, pady=4)

tk.Button(graph, text="Jouer", command=affichervaleur).grid(row=3, column=1, sticky=tk.W, pady=4)



graph.mainloop()

Can you help me with that? (Sorry I don't know how I can make a sandbox here without Pygame and Tkinter)

Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23
Bagou450
  • 93
  • 9
  • first `Thread` needs `target=music` without `()`, and later it will use `()` to start this function. At this moment you run `musinc in current thread. – furas Mar 08 '22 at 20:36
  • in `affichervaleur()` you create local variable `musicc`. You have to add `global musicc` to assign new list to global `musicc` – furas Mar 08 '22 at 20:38

0 Answers0