Reputation: 9
I want to make a sound button that deletes itself and plays music, while making a new mute button and vice versa. Python-Turtle what's wrong with my code?
import turtle, random, math
from turtle import*
from tkinter import*
import winsound
def playsound():
winsound.PlaySound('mainmenu.wav',winsound.SND_LOOP + winsound.SND_ASYNC)
bsound.pack()
bsound.place(x=600,y=400)
def stopsound():
winsound.PlaySound(None, winsound.SND_PURGE)
bsound.pack_forget()
bsound.place_forget()
bmute.pack()
bmute.place(x=600,y=400)
bsound=Button(root, image=bsoundpic, command = stopsound)
bmute=Button(root, image=bmutepic, command = playsound)
Upvotes: 0
Views: 43
Reputation: 4662
You're creating bsound
and bmute
buttons and you're also attempting to pack them in the playsound
and stopsound
functions, but the buttons dont exist before those function are run.
Also you are using bsoundpic
and bmutepic
images without defining them
Lets create the buttons first and then pack them or place them in your functions.
import turtle, random, math
from turtle import *
from tkinter import *
import winsound
root = Tk()
bsoundpic = PhotoImage(file="soundpic.gif")
bmutepic = PhotoImage(file="mutepic.gif")
def playsound():
winsound.PlaySound('mainmenu.wav', winsound.SND_LOOP + winsound.SND_ASYNC)
bmute.pack()
bmute.place(x=600, y=400)
bsound.pack_forget()
bsound.place_forget()
def stopsound():
winsound.PlaySound(None, winsound.SND_PURGE)
bsound.pack()
bsound.place(x=600, y=400)
bmute.pack_forget()
bmute.place_forget()
bsound = Button(root, image=bsoundpic, command = stopsound)
bmute = Button(root, image=bmutepic, command = playsound)
bsound.pack()
bsound.place(x=600, y=400)
root.mainloop()
Upvotes: 0