Python graphics库详解

来源:互联网 发布:手机淘宝详情店铺活动 编辑:程序博客网 时间:2024/05/16 14:51


graphics库可以从http://mcsp.wartburg.edu/zelle/python/graphics.py下载,下载后的graphics.py放到python的安装文件夹下即可

GraphWin对象常用方法
方法名称 方法含义plot(x, y, color)在窗口中(x,y)位置绘制像素。 颜色参数可选, 默认值为黑色。plotPixel(x, y, Color)在 “原 始 ” 位 置 (x,y)处 绘 制 像 素 , 忽 略setCoords()方法设置的坐标变换。setBackground(color) 将窗口背景颜色设为指定颜色, 默认值为灰色。close() 关闭屏幕上的窗口。getMouse()程序等待用户在窗口内点击鼠标, 返回值为点击处的位置, 并以Point对象返回。setCoords(xll, yll, xur,yur)设置窗口的坐标系。 左下是(xll,yll), 右上角是(xur,yur)。 所有后面的绘制都以这个坐标系做参照(plotPexil除外)

图形对象通用方法
方法名称 方法含义setFill(color) 设置对象内部填充颜色。setOutline(color) 设置对象边框颜色。setWidth(pixels) 设置对象的宽度(Point类不起用)draw(aGraphWin) 在指定的窗口中绘制对象。undraw()从窗口中删除该对象。如该对象没有在窗口中画出将会报错。move(dx,dy) 将对象沿x轴和y轴分别移动dxdy单位长度。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)point1point2为对角点创建一个矩形。getCenter() 返回矩形的中心点的克隆值。getP1(), getP2()返回构造矩形的对角点的克隆


Oval对象方法
方法名称 方法含义Oval(point1, point2)在点point1point2指定的边界框中创建一个椭圆。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-36setStyle(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()


原创粉丝点击