InterviewSolution
| 1. |
How Do I Pass Extra Data To A Signal Handler? |
|
Answer» The signal connection methods connect() and connect_after() TAKE an optional third parameter that can be used to supply extra data to a signal handler callback. Any Python object can be passed so you can use a list, tuple or dictionary if you want to pass multiple extra values. Since the signal handler function will receive the widget that emitted the signal as its first parameter, often it's convenient to pass a second related widget as the extra data. Suppose you have a DIALOG with an entry widget. If you want the dialog response handler to do something with the entry value, you could connect to the response signal like this dialog.connect('response', on_dialog_response, entry) This lets you EASILY access methods of the entry widget in the signal handler, perhaps to get the text in the entry def on_dialog_response(widget, response, entry): Something that might trip you up especially if you are new to Python PROGRAMMING is that you PROBABLY don't want to try to pass the entry text to the signal handler like this # probably wrong! The get_text() method is executed when the signal is connected so this would pass a constant value as the extra data. Usually you will want the signal handler to see the current state of the entry widget. The signal connection methods connect() and connect_after() take an optional third parameter that can be used to supply extra data to a signal handler callback. Any Python object can be passed so you can use a list, tuple or dictionary if you want to pass multiple extra values. Since the signal handler function will receive the widget that emitted the signal as its first parameter, often it's convenient to pass a second related widget as the extra data. Suppose you have a dialog with an entry widget. If you want the dialog response handler to do something with the entry value, you could connect to the response signal like this dialog.connect('response', on_dialog_response, entry) This lets you easily access methods of the entry widget in the signal handler, perhaps to get the text in the entry def on_dialog_response(widget, response, entry): Something that might trip you up especially if you are new to Python programming is that you probably don't want to try to pass the entry text to the signal handler like this # probably wrong! The get_text() method is executed when the signal is connected so this would pass a constant value as the extra data. Usually you will want the signal handler to see the current state of the entry widget. |
|