Page 274 -
P. 274

building a graphical user interface


           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   Identify the function to run when
           buttons, put the code you want to run in its own function and then name the   the button is clicked.
           function in the button code by providing a command parameter:

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




                     Create a function to contain   def button_click():
                     the code that runs when the       print("I've just been clicked!")
                      event occurs.                        The button is clicked...






             Click!




                                                                       ...the event is
                                                                       responded to...
                  ...and the message
                    appears on screen.






















                                                                                       you are here 4    239
   269   270   271   272   273   274   275   276   277   278   279