我想删除画布上的所有内容。我认为canvas.delete(tk.ALL)
是个不错的选择。但是它不起作用。为什么?加速器也不起作用。
import tkinter as tk
class Graphy(tk.Frame):
def __init__(self,parent):
tk.Frame.__init__(self,parent)
self.parent = parent
self.parent.title("Graphy 0.1")
self.initialize()
def initialize(self):
#Toolbar
toolbar = tk.Frame(self.parent)
toolbar.pack(side=tk.TOP, fill=tk.X)
knotbtn=tk.Button(toolbar, text="o")
knotbtn.pack(side=tk.LEFT, padx=2, pady=2)
edgebtn=tk.Button(toolbar, text="/")
edgebtn.pack(side=tk.LEFT, padx=2, pady=2)
#Canvas
self.canvas = tk.Canvas(self.parent, height=600, width=600, bg="white")
self.canvas.pack()
#Menue
menu = tk.Menu(self.parent)
self.parent.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="Datei", menu=filemenu)
filemenu.add_command(label= "Neu", command=self.canvas.delete(tk.ALL), accelerator="Strg+N")
filemenu.add_separator()
filemenu.add_command(label="Beenden", command=root.quit, accelerator="Strg+Q")
sightmenu = tk.Menu(menu)
menu.add_cascade(label="Ansicht", menu=sightmenu)
sightmenu.add_command(label="Farbe waehlen")
helpmenu = tk.Menu(menu)
menu.add_cascade(label="Hilfe", menu=helpmenu)
helpmenu.add_command(label="Ueber")
root.bind_all("<Ctrl-N>",self.canvas.delete(tk.ALL))
root.bind_all("<Ctrl-Q>",root.quit())
self.state = 0
self.selected = None
self.start_x = 0
self.start_y = 0
def newKnotornewEdge(event):
#if button "o" is pressed, every click on the canvas results in a circle
if self.state == 0:
self.canvas.create_oval(event.x-25,event.y-25,event.x+25, event.y+25, fill="blue")
self.canvas.create_text(event.x, event.y, text="A", fill="white")
#if button "/" is pressed, and two circles are selected, draw a line between them
elif self.state == 1:
if self.canvas.itemcget(self.canvas.find_overlapping(event.x, event.y, event.x, event.y), "fill") == "blue" and self.selected == None:
self.selected = 1
self.start_x = event.x
self.start_y = event.y
elif self.canvas.itemcget(self.canvas.find_overlapping(event.x, event.y, event.x, event.y), "fill") == "blue" and self.selected == 1:
self.canvas.create_line(self.start_x, self.start_y, event.x, event.y)
self.selected = None
self.start_x = 0
self.start_y = 0
elif self.canvas.itemcget(self.canvas.find_overlapping(event.x, event.y, event.x, event.y), "fill") != "blue" and self.selected == 1:
self.selected = None
self.start_x = 0
self.start_y = 0
self.canvas.bind("<Button -1>", newKnotornewEdge)
def knotbtnclick(event):
knotbtn.config(relief = tk.SUNKEN, state=tk.DISABLED)
edgebtn.config(relief = tk.RAISED, state=tk.ACTIVE)
self.state = 0
knotbtn.bind("<Button-1>", knotbtnclick)
def edgebtnclick(event):
knotbtn.config(relief = tk.RAISED, state=tk.ACTIVE)
edgebtn.config(relief = tk.SUNKEN, state=tk.DISABLED)
self.state = 1
edgebtn.bind("<Button-1>", edgebtnclick)
root = tk.Tk()
app = Graphy(root)
root.mainloop()