Python graphics库详解
来源:互联网 发布:手机淘宝详情店铺活动 编辑:程序博客网 时间:2024/05/16 14:51
graphics库可以从http://mcsp.wartburg.edu/zelle/python/graphics.py下载,下载后的graphics.py放到python的安装文件夹下即可
GraphWin对象常用方法
图形对象通用方法
方法名称 方法含义setFill(color) 设置对象内部填充颜色。setOutline(color) 设置对象边框颜色。setWidth(pixels) 设置对象的宽度(对Point类不起用)。draw(aGraphWin) 在指定的窗口中绘制对象。undraw()从窗口中删除该对象。如该对象没有在窗口中画出将会报错。move(dx,dy) 将对象沿x轴和y轴分别移动dx和dy单位长度。clone() 返回该对象的副本。
Point对象方法
方法名称 方法含义Point(x,y) 以指定坐标的值(x, y)构造一点getX() 返回该点的x坐标值getY() 返回该点的y
Line对象方法
方法名称 方法含义Line(point1, point2) 构造一个从点point1到点point2的线段setArrow(string)设置线段的箭头样式。箭头可以绘制在左端,右端,或者两端都有string参
数值为’first’, ’last’, ’both’,或’none’默
认值为’none’。getCenter() 返回线段中点的坐标值。getP1(), getP2() 返回线段相应端点的坐标值。
Circle对象方法:
方法名称 方法含义Circle(centerPoint,radius)根据给定圆心和半径构建圆getCenter() 返回圆心的值getRadius() 返回圆的半径长度getP1(), getP2()返回值为该圆边框对应点, 对应点指的是该圆外接正方形的对角点。
Rectangle对象方法
方法名称 方法含义Rectangle(point1, point2)以point1和point2为对角点创建一个矩形。getCenter() 返回矩形的中心点的克隆值。getP1(), getP2()返回构造矩形的对角点的克隆
值
Oval对象方法
方法名称 方法含义Oval(point1, point2)在点point1和point2指定的边界框中创建一个椭圆。getCenter() 返回椭圆的中心点的坐标值getP1(), getP2() 返回构造椭圆的对角点的坐标值
Polygon 对象方法
方法名称 方法含义Polygon(point1, point2,point3, ...)根据给定的顶点构造一个多边形。 也可以只用一个顶点列表作为参数
getPoints()返回构造多边形的顶点值的列表
Text 对象方法
方法名称 方法含义Text(anchorPoint, string)以anchorPoint点的位置为中心, 构建了一个内容为string的文本对象。setText(string) 设置文本对象的内容getText() 返回当前文本内容。getAnchor() 返回文本显示中间位置点anchor的坐标值。setFace(family)设置文本字体。 family可选值为:’helvetica’,’courier’, ’times
roman’, 以及 ’arial’.setSize(point) 设置字体大小为给定点point的大小。 合法数值为5-36。setStyle(style)设置字体的风格。 可选值为’normal’, ’bold’, ’italic’,以及’bold italic’。setTextColor(color) 设置文本颜色。 与setFill效果相同。
color_rgb(red,green,blue)函数
设定颜色数值获得颜色
三个参数为0-255范围内的整数
返回一个字符串
color_rgb(255,0,0) 亮红色,
color_rgb(130,0,130) 中度洋红色。
graphics库使用:
# -*- coding: utf-8 -*-from math import *from turtle import *from graphics import *def main(): #创建窗口对象,默认为200*200px,(0,0)表示屏幕左上角 win=GraphWin() #画点 p1=Point(100,100) p1.draw(win) #画圆,以p1为圆心,半径为100 circ=Circle(p1,100) circ.draw(win) circ.setOutline("red")#外围轮廓颜色 circ.setFill("yellow")#填充颜色 #画线 line=Line(Point(650,100),Point(250,100)) line.draw(win) #在p1点上显示文字 message=Text(p1,"圆心") message.draw(win) done()main()
Text对象方法:getText(),setText()
Entry对象方法:getText(),setText(),Entry是创建一个对话框
交互式的多边形程序,可由用户点击5个点并生成多边形
# -*- coding: utf-8 -*-from math import *from turtle import *from graphics import *def main(): #创建窗口对象,默认为200*200px,(0,0)表示屏幕左上角 win=GraphWin("Draw a polygon",300,300) message=Text(Point(150,50),"click on five points") message.draw(win) #获得多边形的5个点 p1=win.getMouse()#等待鼠标点击 p1.draw(win) p2=win.getMouse() p2.draw(win) p3=win.getMouse() p3.draw(win) p4=win.getMouse() p4.draw(win) p5=win.getMouse() p5.draw(win) #绘制多边形 polyon=Polygon(p1,p2,p3,p4,p5) polyon.setOutline("green") polyon.setFill("yellow") polyon.draw(win) #等待鼠标响应,退出程序 message.setText("Click anywhere to quit") win.getMouse()main()
用Entry求和实例:
# -*- coding: utf-8 -*-from math import *from turtle import *from graphics import *def main(): win=GraphWin("Calculate the sum of two numbers",300,300) Text(Point(100,50),"数1:").draw(win) input1=Entry(Point(150,50),5) input1.setText(0.0) input1.draw(win) Text(Point(100, 100), "数2:").draw(win) input2=Entry(Point(150,100),5) input2.setText(0.0) input2.draw(win) Text(Point(80, 150), "两数之和:").draw(win) input3 = Entry(Point(150, 150), 5) input3.draw(win) #点击屏幕求和,求和运算必须是点击鼠标之后,否则参加计算的是默认值 win.getMouse() sum = eval(input1.getText()) + eval(input2.getText()) input3.setText(sum) win.getMouse() win.close()main()
阅读全文
0 0
- Python graphics库详解
- Graphics详解
- python的graphics库的简单用法
- C# Graphics类详解
- C# Graphics类详解
- C# Graphics类详解
- Graphics Context详解
- C# Graphics类详解
- C# Graphics类详解
- android.graphics.Matrix详解
- Core Graphics API 详解
- Python中graphics不存在,解决办法
- Core Graphics之CGContext详解
- Core Graphics之CGContext详解
- android.graphics.Paint类详解
- android.graphics.Canvas类详解
- andriod.graphics.BitmapFactory类详解
- Core Graphics之CGContext详解
- 天气预报接口api
- (android 源码下开发应用程序) 如何在 Android 各 level ( 包含 user space 與 kernel space ) 使用dump call stack的方法
- 安卓开发-Activity的显示意图和隐式意图+实例+Activity界面间数据的传递实例
- Android Studio-2.3 打包运行App报错问题
- W5500+网口开发板资料。硬件原理图
- Python graphics库详解
- PPT神技能,如何使用PPT制作炫酷视频?我想你还是不会吧!
- codevs 2622 数字序列 DP 解题报告
- linux 8-9
- mysql解压版安装
- C++ 引用与指针
- PHP CI框架的url路由配置
- Django之ModelForm组件
- ssd准备自己的数据集进行训练遇见错误和解决方法