Connect code to your button events

When you click on a button, the tkinter event loop captures the event and looks for something to do with it. The trouble is, as your program stands, you have not detailed what that something to do is. Your buttons have no code associated with them, so the events occur but go unnoticed. To connect code to buttons, put the code you want to run in its own function and then name the function in the button code by providing a command parameter:

Identify the function to run when the button is clicked.

b = Button(app, text = "Click on me!", width = 15, command = button_click) b.pack(padx = 10, pady = 10) J

def button_click():

print("I've just been clicked!")

Create a function to contain the code that runs when the event occurs.

def button_click():

print("I've just been clicked!")

Click or me

Click or me

npp_ t t t"L r ( " T " i rV n- rvf?H ) app.qeomfitry [ " 300Jt 1000-»-100"

npp_ t t t"L r ( " T " i rV n- rvf?H ) app.qeomfitry [ " 300Jt 1000-»-100"

print("I've juat been clickcd!")

b - Button(app, tcHt - "Clic]< on mcl", width - 15, command — button_click)

print("I've juat been clickcd!")

b - Button(app, tcHt - "Clic]< on mcl", width - 15, command — button_click)

I n. 14 Col; ft making connections

The code from the nongraphical version of the TVN program is on this and the facing page. Take your pencil and mark the parts of the code that you would extract and turn into functions so that you can then connect the functions to the buttons in your GUI. Mark the other parts of this program that also need to be added to the GUI version.

The nature of the interface provided by the GUI means that some of the program's requirement* have changed.

Note: Don't worry about prompting the host to ask a question in the GUI. But do worry about maintaining a count of the number of questions answered correctly and incorrectly. (The total count is not important, either.)

How many functions do you think you need? Write their names here:

import pygame.mixer def wait finish(channel):

while channel.get busy(): pass sounds = pygame.mixer sounds.init()

correct s = sounds.Sound("correct.wav") wrong s = sounds.Sound("wrong.wav")

prompt = "Press 1 for Correct, 2 for Wrong, or 0 to Quit: "

number asked = 0 number_correct = 0 number wrong = 0

choice = input(prompt) while choice != '0': if choice == '1':

number asked = number asked + 1 number correct = number correct + 1 wait finish(correct s.play()) if choice == '2':

number asked = number asked + 1 number wrong = number wrong + 1 wait finish(wrong s.play()) choice = input(prompt)

print("You asked " + str(number asked) + " questions.") print(str(number correct) + " were correctly answered.") print(str(number wrong) + " were answered incorrectly.")

With the code you need identified, take the time to update your GUI application with the new functions and whatever other code you've extracted from the non-GUI program.

Produce a new program that is a combination of your existing GUI code and the extracted code from this program.

page goal header page goal header

You were to take your pencil and mark the parts of the code on the previous (and facing) page to identify the code you would extract and turn into functions so that you can then connect the functions to the buttons in your GUI. You were also to mark the other parts of this program that also need to be added to the GUI version:

You were to think about how many functions you might need. You were to write their names here:

play_correct_sound()

You need a -function to play_wrong_sound(^ -and another function play a sound when -the to play a sound when the answer is wrong-

import pygame.mixer

The GUI program still needs to use pygame-

import pygame.mixer

The GUI program still needs to use pygame-

def wait finish(channel):

while channel.get busy(): pass sounds = pygame.mixer sounds.init()

correct s = sounds.Sound("correct.wav") wrong s = sounds.Sound("wrong.wav")

prompt = "Press 1 for Correct, 2 for Wrong, or 0 to Quit: "

niimher_askeH _= 0

lumber correct = 0 number wrong = 0

You still need to maintain these counters-

You still need to maintain these counters-

choice = input(prompt) while choice != '0': if choice == '1':

number asked = number asked +_1

number correct = number correct + lit finish(correct s.play()) if choice == '2':

number asked = number asked + 1 number wrong = number wrong + 1 lit finish(wrong s.play()) choice = inpu

^—Turn this Code into the

Kp|ay_dor*-ect_soundO

-function.

print("You asked " + str(number asked) + " gneg-Monq") print(str(number correct) + " were correctly answered.") print(str(number wrong) + " were answered incorrectly.")

Is;— Displaying the summary remains a requirement, too-

With the code you need identified, you were to take the time to update your GUI application with the new functions and whatever other code you've extracted from the non-GUI program.

You were asked to produce a new program that is a combination of your existing GUI code and the extracted code from this program.

the page the updated ¿ode solution...

button press rehearsal

Was this article helpful?

0 0

Post a comment