I have a few different object tags within a tkinter canvas. I would like the user to be able to delete the current object under the mouse cursor with the tag "token"
.
I do not want all objects with the tag "token"
to be deleted, only the one under the mouse cursor. I would like any object with the tag "token"
to be able to be deleted. I want to prohibit deletion of objects with tags "line"
. I've tried:
self.canvas.delete("current")
But this allows me to delete anything under my mouse cursor (including the line object). When I tried
self.canvas.delete("token")
This allowed me to delete all items with the tag "token"
all at once. Here is an excerpt of the definitions for my line object, "token"
objects, and my delete function:
# create static line
self.canvas.create_line(50,250,200,250, width=7, fill="grey", tags="line")
#Create oval
myoval = self.canvas.create_oval(x0,y0,x1,y1, width=10, outline="black", fill="black",
tags="token")
def on_button_press_deleteoval(self,event):
'''Delete oval with double button 1 click.'''
self.canvas.delete("current")
The Tkinter destroy method has the ability to “destroy” any Tkinter GUI objects. This includes widgets like Entry, Button, Toplevel and even the entire Tkinter application. The syntax is very simple. You simply have to call it as a method on any Tkinter object as shown below.
We can also delete Tkinter widgets permanently by calling the destroy () method in this section. Example 1: Hide and Recover the button in Tkinter using forget_pack () and pack () method. Widget can be any valid widget which is visible. Example 2: Hide and Recover the Label in Tkinter using forget_pack () and pack () method.
Create Normal Tkinter Window 2. Add Listbox using Listbox () method Listbox (root, bg, fg, bd, height, width, font, ..) 3. Remove Selected Item From Listbox Get a List of selected Items from Listbox using the curselection () method. Iterate through all list and remove item using delete () method
Example 1: Hide and Recover the button in Tkinter using forget_pack () and pack () method. Widget can be any valid widget which is visible. Use Up/Down Arrow keys to increase or decrease volume. Example 2: Hide and Recover the Label in Tkinter using forget_pack () and pack () method.
You can either take the long way:
if 'token' in canvas.gettags(canvas.find_withtag('current')):
canvas.delete('current')
or use a shortcut:
canvas.delete('current&&token') #logical operator in tag search expression
# && = AND, || = OR, ! = NOT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With