class CanvasDemo:
def __init__(self):
window = Tk()
window.title("画布")
self.canvas = Canvas(window, width=400, height=300, bg="#FFFFFF")
self.canvas.pack()
frame1 = Frame()
frame1.pack()
btnRect = Button(frame1, text="矩形", command=self.drawRect)
btnOval = Button(frame1, text="椭圆", command=self.drawOval)
btnArc = Button(frame1, text="弧形", command=self.drawArc)
btnPoly = Button(frame1, text="多边形", command=self.drawPolygon)
btnLine = Button(frame1, text="线条", command=self.drawLine)
btnStr = Button(frame1, text="字符", command=self.drawString)
btnClear = Button(frame1, text="清空", command=self.clearAll)
btnRect.grid(row=1, column=1)
btnOval.grid(row=1, column=2)
btnArc.grid(row=1, column=3)
btnPoly.grid(row=1, column=4)
btnLine.grid(row=1, column=5)
btnStr.grid(row=1, column=6)
btnClear.grid(row=1, column=7)
window.mainloop()
def drawRect(self):
self.canvas.create_rectangle(10, 10, 190, 90, tag="rect",width=9,fill="yellow")
def drawOval(self):
self.canvas.create_oval(10, 10, 190, 90, fill="red", tag="oval")
def drawArc(self):
self.canvas.create_arc(10, 10, 190, 90, start=0, extent=359, fill="red", tag="arc")
def drawPolygon(self):
self.canvas.create_polygon(10, 10, 190, 90, 30, 50, fill="red", tag="polygon")
def drawLine(self):
self.canvas.create_line(10, 10, 190, 90, 30, 50, tag="line")
self.canvas.create_line(10, 90, 190, 10, 30, 50, width=9, arrow="first", fill="blue", activefill="red",tag="line")
def drawString(self):
self.canvas.create_text(100, 100, text="Hello Tkinter!", font=("华文行楷",20),tag="string")
def clearAll(self):
self.canvas.delete("rect","oval","arc","line","string","polygon")
执行效果