from Tkinter import *
import pygame.mixer

sounds = pygame.mixer
sounds.init()

correct_s = sounds.Sound("Correct.wav")
wrong_s = sounds.Sound("Wrong.wav")

number_correct = 0
number_wrong = 0

def play_correct_sound():
    global number_correct
    number_correct = number_correct + 1
    correct_s.play()
    
def play_wrong_sound():
    global number_wrong
    number_wrong = number_wrong + 1
    wrong_s.play()

app = Tk() # creat a tkinter application window called "app"

app.title("TVN Game Show")
app.geometry("300x100+200+100")

b1 = Button(app, text = "Correct!", width = 10, command = play_correct_sound)
b1.pack(side = "left", padx = 10, pady = 10)

b2 = Button(app, text = "Wrong!", width = 10, command = play_correct_sound)
b2.pack(side = "right", padx = 10, pady = 10)

app.mainloop()

print str(number_correct) + " were correctly answereed."
print str(number_wrong) + " were answered incorrectly."

In the above program I'M having two problems. The first is there is no sound played when I run this code and click on the buttons. The sounds can be found at http://www.headfirstlabs.com/books/hfprog/ under chapter 7.

The second problem I'M having is the last line will only and always display "0" for answered incorrectly.

Thanks for any and all replies.

One of the problems is here:

b1 = Button(app, text = "Correct!", width = 10, command = play_correct_sound)
b1.pack(side = "left", padx = 10, pady = 10)

b2 = Button(app, text = "Wrong!", width = 10, command = play_correct_sound)
b2.pack(side = "right", padx = 10, pady = 10)

where it shoul be:

b1 = Button(app, text = "Correct!", width = 10, command = play_correct_sound)
b1.pack(side = "left", padx = 10, pady = 10)

b2 = Button(app, text = "Wrong!", width = 10, command = [B]play_wrong_sound[/B])
b2.pack(side = "right", padx = 10, pady = 10)

And maybe switching this:

import pygame.mixer

sounds = pygame.mixer

by this:

from pygame import mixer as sounds

but I don't know much about pygame.

Edited 6 Years Ago by Beat_Slayer: n/a

try using:

import pygame
#and
from pygame.locals import *

Also use the

pygame.mixer.get_init()

function to see if the mixer is working

According to what I have read about pygame, it usually isn't compatible with other modules unless they have been specially developed to work with it.

According to what I have read about pygame, it usually is not compatible with other modules unless they have been specially developed to work with it.

yeah, but surely there is no issue, as Tk and pygame are not interacting. I have used both together before.

This article has been dead for over six months. Start a new discussion instead.