Think Python How to Think Like a Computer Scientist
Chapter 19. Case study: Tkinter
Download 1.04 Mb. Pdf ko'rish
|
thinkpython
190
Chapter 19. Case study: Tkinter def run_text(self): source = self.te_code.get(1.0, END) self.inter.run_code(source, ' 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 mecha- nisms 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 thinkpython. com/code/menubutton_demo.py ): g = Gui() g.la('Select a color:') colors = ['red', 'green', 'blue'] mb = g.mb(text=colors[0]) mb creates the Menubutton. Initially, the text on the button is the name of the default color. The following loop creates one menu item for each color: for color in colors: g.mi(mb, text=color, command=Callable(set_color, color)) The first argument of mi is the Menubutton these items are associated with. The command option is a Callable object, which is something new. So far we have seen functions and bound methods used as callbacks, which works fine if you don’t have to pass any arguments to the function. Otherwise you have to construct a Callable object that contains a function, like set_color , and its arguments, like color. The Callable object stores a reference to the function and the arguments as attributes. Later, when the user clicks on a menu item, the callback calls the function and passes the stored arguments. Here is what set_color might look like: def set_color(color): mb.config(text=color) print color When the user selects a menu item and set_color is called, it configures the Menubutton to display the newly-selected color. It also print the color; if you try this example, you can confirm that set_color is called when you select an item (and not called when you create the Callable object). |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling