Jump to content


Photo

Chapter 10 Page 357


  • Please log in to reply
No replies to this topic

#1 @$!-!i$!-!

@$!-!i$!-!

    Active Member

  • Members
  • PipPip
  • 13 posts
  • Gender:Male

Posted 18 March 2015 - 07:03 PM

Hi, 

 

page 357 of the book says

"Each widget needs its own event handler"

"if you are creating several checkboxes, you are going to need a separate event handler for each of them"

 

I get the point, but don't understand why we need a separate event handler for each widget if the behaviour of the event handler is the same.

 

I tried the following code and it works correctly.

sound_panel1.py:

from tkinter import *
import pygame.mixer
def create_gui(app, mixer,sound_file):
    
    track = mixer.Sound(sound_file)
    track_playing = IntVar()

    Checkbutton(app,variable=track_playing,command=track_toggle,
                text=sound_file).pack(side = LEFT)

    volume = DoubleVar()
    volume.set(track.get_volume())
    Scale(app,variable = volume,command = change_volume,from_ = 0.1,to = 0.9,
          resolution = 0.1,label = "Volume",orient = HORIZONTAL).pack(side = RIGHT)

dj.pyw:

from tkinter import *
from sound_panel1 import *
import pygame.mixer

app = Tk()
app.title("Head First Mix")

mixer = pygame.mixer
mixer.init()

def change_volume(v):
    track.set_volume(volume.get())
    
def track_toggle():
    if track_playing.get() == 1:
        track.play(loops = -1)
    else:
        track.stop()
        
create_gui(app,mixer,"50459_M_RED_Nephlimizer.wav")
create_gui(app,mixer,"49119_M_RED_HardBouncer.wav")

def shutdown():
    mixer.stop()
    app.destroy()

app.protocol("WM_DELETE_WINDOW",shutdown)
app.mainloop()

Can someone please elaborate more on this?

 

Thanks in advance :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users