To add the capability to draw irregular lines, we just need to define the method named draw_irregular_line. To specify options that appear in the top bar, we need to define the method named draw_irregular_line_options.
We define the draw_irregular_line method as follows (see code 6.07.py):
def draw_irregular_line(self):
self.current_item = self.canvas.create_line(
self.start_x, self.start_y, self.end_x, self.end_y, fill=self.fill,
width=self.width)
self.canvas.bind("<B1-Motion>", self.draw_irregular_line_update_x_y)
def draw_irregular_line_update_x_y(self, event=None):
self.start_x, self.start_y = self.end_x, self.end_y
self.end_x, self.end_y = event.x, event.y
self.draw_irregular_line()
The preceding code is similar to the code for draw_line, except that it adds an extra line that binds mouse-clicked movements to a new method that replaces the start x, y coordinates with the end x, y coordinates and again calls back the draw_irregular_line method, thereby drawing in a continuous manner.
The options that show in the top bar are defined using the following method (see code 6.07.py):
def draw_irregular_line_options(self):
self.create_fill_options_combobox()
self.create_width_options_combobox()
Now we can draw irregular lines on the canvas. However, since we have modified the mouse binding, all other methods will also start to draw in a continuous manner.
We, therefore, need to rebind the buttons back to their original bindings. We do that by modifying on_tool_bar_button_clicked to call bind_mouse, which then restores the mouse binding to its original behavior.
Alternatively, you can use add="+" as an additional argument to keep more than one binding to the same event, as follows:
mywidget.bind("<SomeEvent>", method1, add="+")
mywidget.bind("<SameEvent>", method2, add="+")
This will bind the same event to method1 and method2.