Page 284 -
P. 284

building a graphical user interface


           Label it


           When it comes to adding a label to your GUI, use the tkinter Label widget.   Create a new label, attach
          You create it in code not unlike the way you create a button. Here’s the code   it to the main window, give
           to add a label to an existing GUI application. The label simply displays a   it some text, and adjust the
           string:                                                              label's height.


                    l = Label(app, text='When you are ready, click on the buttons!', height = 3)
                    l.pack()

               Don't forget to pack() the widget.






                                Put the label code before
                                the code for the buttons
                                 and the GUI will then look
                                 like this.


          Another variation replaces the text parameter with textvariable. If
           you assign a special tkinter variable to this parameter, the label will change
           whenever the value of the variable changes, automatically:

         Create an “IntVar”.


                   num_good = IntVar()
                   num_good.set(0)
        Associate
        the “IntVar”   l1 = Label(app, textvariable = num_good)
       with the
       label.      l1.pack(side = 'left')


                           ...

                   num_good.set(100)




                                    Use the “set()” method to adjust the
                                    value of the “IntVar”, and the GUI
                                    updates, as if by magic.
                                                                                       you are here 4    249
   279   280   281   282   283   284   285   286   287   288   289