The GUI programs now ready for a screentest

Here's what your GUI program should look like now:

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

Python s global number_correct = number correct + 1

keyword lets correct_s .play() you adjust the value associated with a variable "

created outside the function.

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

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

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

The buttons are now Connected to event-functions.

command = play\correct_sound)

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

app.mainloop()

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

Was this article helpful?

0 0

Post a comment