Page 207 - thinkpython
P. 207

19.7. Menus and Callables                                                   185

                           endrow ends this row of widgets, so subsequent widgets are packed in the column Frame.
                           Gui.py keeps a stack of Frames:

                              • When you use row, col or gr to create a Frame, it goes on top of the stack and becomes
                                the current Frame.

                              • When you use endrow , endcol or endgr to close a Frame, it gets popped off the stack
                                and the previous Frame on the stack becomes the current Frame.

                           The method run_file reads the contents of the Entry, uses it as a filename, reads the con-
                           tents and passes it to run_code . self.inter is an Interpreter object that knows how to take
                           a string and execute it as Python code.
                               def run_file(self):
                                   filename = self.en_file.get()
                                   fp = open(filename)
                                   source = fp.read()
                                   self.inter.run_code(source, filename)
                           The last two widgets are a Text widget and a Button:
                                   self.te_code = self.te(width=25, height=10)
                                   self.te_code.insert(END,  'world.clear()\n  ')
                                   self.te_code.insert(END,  'bob = Turtle(world)\n  ')

                                   self.bu(text=  'Run code ', command=self.run_text)
                           run_text is similar to run_file except that it takes the code from the Text widget instead
                           of from a file:
                               def run_text(self):
                                   source = self.te_code.get(1.0, END)
                                   self.inter.run_code(source,   '<user-provided code>  ')
                           Unfortunately, the details of widget layout are different in other languages, and in different
                           Python modules. Tkinter alone provides three different mechanisms for arranging widgets.
                           These mechanisms are called geometry managers. The one I demonstrated in this section
                           is the “grid” geometry manager; the others are called “pack” and “place”.
                           Fortunately, most of the concepts in this section apply to other GUI modules and other
                           languages.



                           19.7 Menus and Callables

                           A Menubutton is a widget that looks like a button, but when pressed it pops up a menu.
                           After the user selects an item, the menu disappears.
                           Here is code that creates a color selection Menubutton (you can download it from http:
                           //thinkpython.com/code/menubutton_demo.py  ):
                           g = Gui()
                           g.la( 'Select a color:  ')
                           colors = [ 'red ',  'green ',  'blue ']
                           mb = g.mb(text=colors[0])
   202   203   204   205   206   207   208   209   210   211   212