python中构建画布的代码_【python基础】Tkinter⼩构件之
canvas画布
学java必备软件
【python之tkinter画布】
本⽂代码来源于机械⼯业出版社的⼀本python书籍.
要画布就要使⽤tkinter的⼩构件,canvas(结构化的图形,⽤于绘制图形,创建图形编辑器以及实现⾃定制的⼩构件类)终极代码在线观看免费完整版
我们先使⽤create_rectangle, create_oval, create_arc, create_polygon, create_line分别绘制矩形,椭圆,圆弧,多边形,线段。创建CanvasDemo.py的⽂件,代码如下:
#-*- coding: utf-8 -*->>>>画布,使⽤Canvas⼩构件>>>####
from Tkinter import *
classCanvasDemo:def __init__(self):
window= Tk() #创建窗⼝
window.title("Canvas Demo") #给窗⼝命名
#在窗⼝画布
self.canvas = Canvas(window, width = 200, height = 100, bg = "white")
self.canvas.pack()#创建frame的框架,窗⼝window为这个框架的⽗容器
swap的同义词frame =Frame(window)
frame.pack()#frame框架作为Button的⽗容器
btRectangle = Button(frame, text="rectangle", command =self.displayRect)
btOval= Button(frame, text = "Oval", command =self.displayOval)
btArc= Button(frame, text = "Arc", command =self.displayArc)
btPolygon= Button(frame, text = "Polygon", command =self.displayPolygon)
btLine= Button(frame, text = "Line", command =self.displayLine)
btString= Button(frame, text = "String", command =self.displayString)
btClear= Button(frame, text = "Clear", command =self.displayClear)#Button在画布上布局
scala开发工程师
window.mainloop()defdisplayRect(self):
ate_rectangle(10,10,190,90,tags = "rect")#fill填充oval的颜⾊
defdisplayOval(self):
ate_oval(10,10,190,90, fill = "red", tags = "oval")#start为开始的度数,extent为要转的度数.全部以逆时针为正⽅向,0为x轴正⽅向
defdisplayArc(self):
ate_arc(10,10,190,90, start = 0, extent = 90, width = 8, fill = "red",tags = "arc")defdisplayPolygon(self):
ate_polygon(10,10,190,90,10,90,tags = "polygon")#arrow表⽰line指向,activefill:当⿏标在line上时出现的特定风格,本例中⿏标移动到第⼆个line上时line变蓝
defdisplayLine(self):
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(self):
ate_text(60,40,text= "hi, i am string", font = "time 10 bold underline", tags = "string")#delete⽅法通过tags参数从画布上删除图形
python代码画图案defdisplayClear(self):
下载ppt模板幻灯片模板免费self.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为纵向(向下为正⽅向), 偷个懒, 图就不改了哈.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论