#-*- coding: utf-8 -*->>>>画布,使⽤Canvas⼩构件>>>####from Tkinter import *
classCanvasDemo:def __init__(lf):
window= Tk() #创建窗⼝
window.title("Canvas Demo") #给窗⼝命名
#在窗⼝画布
lf.canvas = Canvas(window, width = 200, height = 100, bg = "white")
lf.canvas.pack()#创建frame的框架,窗⼝window为这个框架的⽗容器
frame =Frame(window)
frame.pack()#frame框架作为Button的⽗容器
广州潜水学校btRectangle = Button(frame, text="rectangle", command =lf.displayRect)
btOval= Button(frame, text = "Oval", command =lf.displayOval)
btArc= Button(frame, text = "Arc", command =lf.displayArc)
好歹>唯美背影图片btPolygon= Button(frame, text = "Polygon", command =lf.displayPolygon)
列日要塞btLine= Button(frame, text = "Line", command =lf.displayLine)
btString= Button(frame, text = "String", command =lf.displayString)
btClear= Button(frame, text = "Clear", command =lf.displayClear)#Button在画布上布局
叶公好龙是什么意思id(row= 1, column = 4)
window.mainloop()defdisplayRect(lf):
ate_rectangle(10,10,190,90,tags = "rect")#fill填充oval的颜⾊
defdisplayOval(lf):
ate_oval(10,10,190,90, fill = "red", tags = "oval")#start为开始的度数,extent为要转的度数.全部以逆时针为正⽅向,0为x轴正⽅向
defdisplayArc(lf):
ate_arc(10,10,190,90, start = 0, extent = 90, width = 8, fill = "red",tags = "arc")defdisplayPolygon(lf):
ate_polygon(10,10,190,90,10,90,tags = "polygon")#arrow表⽰line指向,activefill:当⿏标在line上时出现的特定风格,本例中⿏标移动到第⼆个line上时line变蓝
defdisplayLine(lf):
ate_line(10,10,190,90,fill = "red",tags = "line")
ate_line(10,90,190,10,width = 9,arrow = "first",activefill = "blue", tags = "line")#font定义字体(字体名,⼤⼩,风格)
defdisplayString(lf):
ate_text(60,40,text= "hi, i am string", font = "time 10 bold underline", tags = "string")#delete⽅法通过tags参数从画布上删除图形
defdisplayClear(lf):
lf.canvas.delete("rect","oval","arc","polygon","line","string")
CanvasDemo()
运⾏程序即可。
可能你不知道create_xx()⾥的数字意思,其实是坐标(x1,y1),(x2,y2),(x3,y3)create_xx(x1,y1,x2,y2,x3,y3)
Tkinter的坐标系是这样的:
2018.4.25更新: 经过Pangolin2 的提醒上图错了, 在python3.6中x为横向(向右为正⽅向), y为纵向(向下为正⽅向), 偷个懒, 图就不改了哈.