Adding toolbar buttons

Next, we need to add 16 buttons to the left toolbar. Furthermore, depending on which button is clicked, different options would show up in the top bar as shown here:

We do not want our code structure to be too bloated by conditional logic to switch among these 16 functions. Therefore, we will call these methods dynamically.

We first begin by defining a tuple of all 16 function names (see code 6.01.py):

tool_bar_functions = (
"draw_line", "draw_oval", "draw_rectangle", "draw_arc",
"draw_triangle", "draw_star", "draw_irregular_line",
"draw_super_shape", "draw_text", "delete_item",
"fill_item", "duplicate_item", "move_to_top",
"drag_item", "enlarge_item_size", "reduce_item_size"
)

Doing so ensures that we do not have to call each method explicitly from our code. We can instead use the index of the tuple to retrieve the method name and call it dynamically using the following:

 getattr(self, self.toolbar_functions[index]) 

This makes sense here because we would eventually add more features to our drawing program by simply extending the toolbar_functions tuple.

We further define an attribute, selected_tool_bar_function, which will keep track of which button was last clicked. We initialize it to the first button ( draw_line ) as follows:

selected_tool_bar_function = tool_bar_functions[0] 

Next, we create a folder named icons and add icons for all these 16 toolbar buttons. The icons have been named the same as the corresponding function name. 

Maintaining this consistency allows us to use the same tuple to loop over and build our toolbar buttons. This style of programming is what you could call programming using conventions over configuration.

We next create the method that makes the actual buttons (see code 6.03.py):

def create_tool_bar_buttons(self):
for index, name in enumerate(self.tool_bar_functions):
icon = tk.PhotoImage(file='icons/' + name + '.gif')
self.button = tk.Button(self.tool_bar, image=icon, command=lambda
index=index: self.on_tool_bar_button_clicked(index))
self.button.grid(row=index // 2, column=1 + index % 2, sticky='nsew')
self.button.image = icon

The preceding code creates all the buttons and adds command callbacks to the buttons as highlighted. We accordingly define the command callback as follows (see code 6.03.py):

def on_tool_bar_button_clicked(self, button_index):
self.selected_tool_bar_function = self.tool_bar_functions[button_index]
self.remove_options_from_top_bar()
self.display_options_in_the_top_bar()

The preceding method sets the value of selected_tool_bar_function. Next, it calls two methods that are defined as follows (see code 6.03.py):

def remove_options_from_top_bar(self):
for child in self.top_bar.winfo_children():
child.destroy()

We need to remove any existing options currently displaying in the top bar before we can display options for the newly selected button. The winfo_children method used just now returns a list of all widgets that are children of this widget.

Now that we have removed all items from the top bar, we define the selected tool icon on the top bar:

def display_options_in_the_top_bar(self):
self.show_selected_tool_icon_in_top_bar(self.selected_tool_bar_function)

Currently, this method only calls one other method to display the selected tool icon in the top bar. We will, however, use this method as the central place for adding options to the top bar later in the chapter.

We do not discuss the show_selected_tool_icon_in_top_bar method here as it simply adds a label with an icon to the top bar (see code 6.03.py):

Now, if you go and run the code 6.03.py, it should display all 16 buttons in the left toolbar. Furthermore, clicking on any one of the buttons should display the selected button in the top bar, as shown in the preceding screenshot.

The winfo_children() method used earlier is an example of widget methods that are available to be called on all widgets. Several useful widget methods are defined in Tkinter.
In addition to the widget methods that are available on all widgets, some methods are only available on the top-level window. You can get a list of all such available methods and their descriptions by typing the following in your Python 3 console:
  • >>> import tkinter
  • >>> help (tkinter.Misc)
  • >>> help (tkinter.Wm)
These are available online at http://effbot.org/tkinterbook/widget.htm and at http://effbot.org/tkinterbook/wm.htm.
You are encouraged to take a look at all these available methods.

Next, we will extend our program to actually draw items on the canvas.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.170.187