Page 402 -
P. 402

custom widgets and classes


           A class has methods that define behavior


           The SoundPanel() class creates a new kind of tkinter Frame(), and you
           can specify this relationship using the code like this:  This is the class name.
                   This says that we are
                   defining a new CLASS.                                    Indicate that the
                                           class SoundPanel(Frame):         new class is a type of
                                                                            Frame.
                                                                             The METHODs of the class go here.
                                                                             The word “method” is used in OOP
                                                                             instead of the word “function.”

          As well as the what (it’s a frame), you also have to worry about the how, which
           will define the behavior of your new widgets. To do this, you need to add
           methods inside the class. To understand how this works, imagine you have
           created an alarm button object from a class. The alarm button will need to
           know what to do when somebody hits it:


                   Uh-oh, someone just
                   hit me. I‛d better ask
                   the class that made me
                   what I should do.


                                                                        class AlarmButton(Frame):
           This will be a method                                            def button_hit(self):
           inside the alarm
           button's class.                                                      klaxon.hoot_loudly()
                                                                    The method tells the button how to
                                                                     behave when something happens.



                                                You need to create some methods for the new SoundPanel()
                                                class. Which of the following behaviors do you think you need to
                                                create methods for? Draw a circle around each one:


                 Someone moves the volume slider.                        Someone clicks the checkbox.


                 The computer starts up.                                You get to the end of the track.


                 Create the interface.



                                                                                      you are here 4    367
   397   398   399   400   401   402   403   404   405   406   407