Adding functionality to menu items

Recall that, at the time of creating our menu using the Framework class, we created empty methods that were linked to our menu items. We will now modify those empty methods to make them functional (see code 6.09.py )

File | New Menu:

The canvas delete method can be used to delete an item, given an item-specifier. Here we use ALL to delete all items from the canvas:

def on_new_file_menu_clicked(self, event=None):
self.start_new_project()

def start_new_project(self):
self.canvas.delete(ALL)
self.canvas.config(bg="#ffffff")
self.root.title('untitled')

File | Save, File | Save As:

Tkinter lets you save canvas objects as a postscript file using the command postscript(). Note, however, that the resulting postscript file cannot save images or any widgets embedded on the canvas. Furthermore, note that the pickling of Tkinter widgets or saving to .jpg or .png formats is not possible. This is one of the major limitations of Tkinter.

Here's the code for the save and save as features (see code 6.09.py): 

def actual_save(self):
self.canvas.postscript(file=self.file_name, colormode='color')
self.root.title(self.file_name)

We do not discuss the Close and About menu as we have coded similar menus in all our previous projects (see code 6.09.py)

Edit | Undo:

Recall that all items added to the canvas are stored in a stack. We can access the stack using the canvas command:

canvas.find("all") 

Using this, we implement a very basic undo operation, which lets us delete the last drawn item on the canvas.

Accordingly, the code for adding the undo feature is as follows (see code 6.09.py):

def on_undo_menu_clicked(self, event=None):
self.undo()

def undo(self):
items_stack = list(self.canvas.find("all"))
try:
last_item_id = items_stack.pop()
except IndexError:
return
self.canvas.delete(last_item_id)

Note that this will not undo any styling changes such as changes in color, width, outline, and so on. In fact, it will only be able to delete the last item from the stack.

We can implement a fully-fledged undo stack by saving all actions in a suitable data structure, but that would be an exercise worth its own chapter.

In addition to the find method we used here, the Canvas widget has a method named:

find_closest(x, y, halo=None, start=None) 

It returns the item handle for the item closest to the given position on the canvas. This means that if there is only one item on the canvas, it will be selected regardless of how near or how far you click from it.

If, on the other hand, you want objects only within a certain area, you can use:

find_overlapping(x1, y1, x2, y2) 

This returns all items that overlap the given rectangle, or that are completely enclosed by it. 

Now that we have a hold on the item to be manipulated, we can proceed to do whatever we want with the item.
For a complete list of canvas methods, please see http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas-methods.html.
View | Zoom in, View | Zoom out:

Finally, we define these two methods using the canvas.scale method. We have already used the scale methods earlier to enlarge and reduce individual items. Here, we simply use the method on the ALL item-specifier, as in the following code (see code 6.09.py)

def canvas_zoom_in(self):
self.canvas.scale("all", 0, 0, 1.2, 1.2)
self.canvas.config(scrollregion=self.canvas.bbox(ALL))

That concludes the iteration and the chapter.

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

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