Not all events are generated by button clicks

Your GUI program processes lots of events, not just the events generated by your user when, for instance, buttons are clicked.

Your operating system can send events to your GUI program, too. Some of these events are commonly handled by the graphical programming technology you are working with. For most of the operating system's events, tkinter very kindly handles them for you. When you click the close box on your GUI, this generates a Window Manager event for your code to handle. If your code doesn't handle a Window Manager event, tkinter handles it for you in a default way, too.

Your user generates a button-click event in the

GUI.

Wait for another event.

"The Window Manager Senerates an event when the user ¿kks o» the close box.

Wait for another event.

R™ the event-handling ¿ode associated with whatever event occurred^or perform some default action.

"The Window Manager Senerates an event when the user ¿kks o» the close box.

If the default event handling isn't what you want, you have to capture the event before it gets to tkinter and is handled in the default way. At the moment, the click on the close box is being handled for you by tkinter and the default behavior is to close the window.

Let's take control of this default behavior.

the cost of interactive loveliness

Frank: What's up now?

Joe: Well, it looks like I have to worry about lots of other GUI events, not just my own.

Jim: Yeah. It appears the operating system and this Window Manager thing can give the GUI application work to do.

Frank: Yes, that's right. All that interactive loveliness comes at a cost.

Frank: No, not that sort of cost. You sometimes need to write a little extra code to interact with the Window Manager when and where necessary... that's what I mean by "cost."

Frank: It's something built into every GUI application that handles the management of your application's windows. Python's GUI, tkinter, has a Window Manager, as do all the other GUI toolkits.

Joe: So, how do I work with the events generated by the Window Manager?

Frank: You create a function with the code you want to run and then connect the function to the event.

Joe: OK, I get that. But which event do I connect up to? It's not like the Window Manager has a button to click on, is it?

Jim: That's a good question... Frank?

Frank: Well spotted. What happens with the Window Manager is that there's a set of protocol properties that you can interact with as and when required.

Joe: Protocol what? Properties?

Jim: Yeah, you've lost me there, Frank.

Frank: Yes, protocol properties... they really are not as scary as they sound. Remember: with GUIs, it's all just code.

Jim & Joe: Where have we heard that before... ?

You see, GUIs might look nice and easy, but they are actually a pretty complicated beast under the hood. There's lots of different events to worry about.

Study the three tkinter properties presented below and see if you can match up the properties to the correct description:

WM TAKE FOCUS

WM SAVE YOURSELF

A message sent to your main window when the operating system is shutting down

A message sent to your main window when the close box has been clicked

WM DELETE WINDOW

A message sent to your main window when the window has been selected after a mouse click

Which of these protocol messages do you think you need to capture?

The tkinter library provides a mechanism to react to an event that is associated with the GUI window. These are known as Window Manager protocol properties. Think of the event as a protocol event.

capture the event

You were to study the three tkinter properties presented below and see if you could match up the properties to the correct description:

A message sent to your main window when the operating system is shutting down

A message sent to your main window when the close-box has been clicked

A message sent to your main window when the window has been selected after a mouse click

You were to identify which of these protocol messages you think you need to capture:

WM delete window

WM TAKE FOCUS

WM SAVE YOURSELF

WM DELETE WINDOW

WM TAKE FOCUS

WM SAVE YOURSELF

WM DELETE WINDOW

OK... here comes a click on the close box. What's my protocol? Ah, yes... it hasn't been captured, so I'll just execute the default behavior and close that sucker!

Controlling the Window Manager

To capture the event before it gets to tkinter, call your app's protocol () method and identify the function that should be called instead of executing the default behavior:

Be sure to call "protocoló" BEFORE WinloopO".

You'll have to create the "shutdown" function.

app.protocol("WM DELETE WINDOW", shutdown)

Associate the event-handling function with the property.

Now that you know about window manager properties and how to capture them, write the code for the shutdown () function:

fav-

from tkinter import * import pygame.mixer app = Tk()

app.title("Head First Mix") app.geometry('250x100+200+100')

sound_file = "50459_M_RED_Nephlimizer.wav"

mixer = pygame.mixer mixer.init()

def track_start():

def track_stop(): track.stop()

Put the h

"shutdown'

track = mixer.Sound(sound_file)

start_button = Button(app, command = track_start, text = "Start") start_button.pack(side = LEFT)

stop_button = Button(app, command = track_stop, text = "Stop") stop_button.pack(side = RIGHT)

VVhat needs__^

app.mainloop()

shutdown function

Now that you know about window manager properties and how to capture them, you were asked to write the code for the shutdown () function:

from tkinter import * import pygame.mixer app = Tk()

app.title("Head First Mix") app.geometry('250x100+200+100')

sound_file = "50459_M_RED_Nephlimizer.wav"

mixer = pygame.mixer mixer.init()

def track_start():

def track_stop(): track.stop()

def shutdown(): Simply arrange for track.sW)

track to stop playing

when the window closes.

track = mixer.Sound(sound_file)

start_button = Button(app, command = track_start, text = "Start") start_button.pack(side = LEFT)

stop_button = Button(app, command = track_stop, text = "Stop") stop_button.pack(side = RIGHT)

Tall "app.pvotoi.ol0

So« ti tall to -—y ap.p.protocol("WM_DELETE_WINDOW";. shutdown)

app.mainloop()

Was this article helpful?

0 0

Post a comment