您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > AI人工智能 > Python 语言程序设计8
第八周交互式图形编程8.1图形编程、图形对象、交互式GUI等基本介绍(1)图形显示:图素法—以图形对象为基本元素,如矩形、圆形(矢量图—放大不失真)像素法—以像素点为基本单位(标量图—放大失真)图形用户界面(GraphicalUserInterface,GUI)Python图形工具包:tkinker(tk接口)—标准GUIGraphics—基于tk拓展turtle—内置图形库步骤:①导入图形模块importgraphics(可简化为fromgraphicsimport*)②创建图形窗口win=graphics.GraphWin()(可简化为win=GraphWin())③关闭窗口win.close()点对象Point:图形模块中最简单的对象,通过参考坐标系定位,坐标(x,y)参考坐标系:Graphics\Tkinter:原点表示屏幕左上角X轴正方向向右,Y轴正方向向下,默认窗口大小200*200像素【例】窗口中交互绘制点fromgraphicsimport*p1=Point(100,100)p1.getX()100p2=Point(150,80)win=GraphWin()p1.draw(win)p2.draw(win)(2)图形对象的概念类的实例:类定义了实例包含的属性和方法如两个独立的Point实例p1,p2属性:x,y值方法:getX()和draw()不同的实例可以有不同的属性值创建点对象:Point类的构造函数需要两个参数x,y,存储在对象实例变量p中大多数类的属性都有并在创建对象时设为默认值如:p=Point(50,80)调用方法:无参数方法:一般用于访问对象的内部属性如:p.getX()50有参数方法:通过改变对象的属性值来改变对象状态,参数可以为复杂对象,如:circ=circle(Point(100,100),30)win=GraphWin()circ.draw(win)【注意】两个不同变量可能表示同一个对象,改变一个对象内容的时候,另外一个也会变化如:[python]viewplaincopy1.fromgraphicsimport*2.win=GraphWin()3.leftEye=Circle(Point(80,80),5)4.leftEye.setFill(“yellow”)5.leftEye.setOutline(“red”)6.rightEye=leftEye7.rightEye.move(40,0)8.Circle(Point(120,80),5)9.leftEye.draw(win)10.rightEye.draw(win)解决方法:为两只眼睛创建不同的对象(3)交互式图形用户接口(图形用户界面)图形用户界面:图形方式计算机操作界面,用于程序的输入和输出—事件驱动事件:移动鼠标,点击按钮,键盘输入事件驱动程序需要编程人员知道任何指定的时刻“谁在负责”1.Graphics模块:隐藏了低层事件的处理机制,获得用户在窗口中的输入【例8.1.1】交互式绘制五边形[python]viewplaincopy1.#程序8.1.12.fromgraphicsimport*3.defmain():4.spanstyle=white-space:pre/spanwin=GraphWin(“Drawapolygon”,300,300)5.spanstyle=white-space:pre/spanwin.setCoords(0.0,0.0,300.0,300.0)6.spanstyle=white-space:pre/spanmessage=Text(Point(150,20),“Clickonfivepoints”)7.spanstyle=white-space:pre/spanmessage.draw(win)8.spanstyle=white-space:pre/span#获得多边形的五个点9.spanstyle=white-space:pre/spanp1=win.getMouse()10.spanstyle=white-space:pre/spanp1.draw(win)11.spanstyle=white-space:pre/spanp2=win.getMouse()12.spanstyle=white-space:pre/spanp2.draw(win)13.spanstyle=white-space:pre/spanp3=win.getMouse()14.spanstyle=white-space:pre/spanp3.draw(win)15.spanstyle=white-space:pre/spanp4=win.getMouse()16.spanstyle=white-space:pre/spanp4.draw(win)17.spanstyle=white-space:pre/spanp5=win.getMouse()18.spanstyle=white-space:pre/spanp5.draw(win)19.spanstyle=white-space:pre/span#使用Polygon对象绘制多边形20.spanstyle=white-space:pre/spanPolygon=Ploygon(p1,p2,p3,p4,p5)21.spanstyle=white-space:pre/spanPolygon.setFill(“peachpuff”)22.spanstyle=white-space:pre/spanPolygon.setoutline(“black”)23.spanstyle=white-space:pre/spanPolygon.draw(win)24.spanstyle=white-space:pre/span#等待响应鼠标事件,退出程序25.spanstyle=white-space:pre/spanmessage.setTest(“Clickanywheretoquit”)26.spanstyle=white-space:pre/spanwin.getMouse()27.if__name__==‘__main__’:28.spanstyle=white-space:pre/spanmain()Text对象:setText()和getText()—不可被用户修改Entry对象:setText()和getText()—可被用户修改2.Tkinter创建GUI程序的基本步骤①导入Tk模块②创建GUI应用程序的主窗口③添加控件或GUI应用程序④进入主事件循环,等待响应用户触发事件15钟常用的Tk控件:①共同属性:Dimensions:尺寸Colors:颜色Fonts:字体Anchors:锚(文本相对应位置参考点)Reliefstyles:浮雕式Bitmaps:显示位图Cursors:光标外形②特有属性界面布局:Tk三种几何管理方法:pack()、grid()、place()使用Tk库创建GUI应用程序窗口的代码模板:[python]viewplaincopy1.fromtkinterimport*2.tk=Tk()3.#控件代码4.tk.mainloop()【例8.1.2】画布显示文字,图片和绘制图形[python]viewplaincopy1.#程序8.1.22.fromtkinterimport*3.defmain():4.tk=Tk()5.canvas=Canvas(tk,width=200,height=200)6.canvas.pack()7.canvas.create_text(100,40,text=“WelcometoTkinter”,8.fill=“blue”,font=(“Times”,18))9.myImage=PhotoImage(file=“python_logo.gif”)10.canvas.create_Image(10,70,anchor=NW,image=myImage)11.canvas.create_rectangle(10,70,190,170)12.tk.mainloop()13.if__name__==‘__main__’:14.main()【例8.1.3】上下左右键控制图形移动[python]viewplaincopy1.#程序8.1.32.fromtkinterimport*3.defmain():4.tk=Tk()5.canvas=Canvas(tk,width=400,height=400)6.canvas.pack()7.defmoverectangle(event):8.ifevent.keysym==Up:9.canvas.move(1,0,-5)10.elifevent.keysym==Down:11.canvas.move(1,0,5)12.elifevent.keysym==Left:13.canvas.move(1,-5,0)14.elifevent.keysym==Right:15.canvas.move(1,5,0)16.canvas.create_rectangle(180,180,220,220,fill=red)17.canvas.bind_all(KeyPress-Up,moverectangle)18.canvas.bind_all(KeyPress-Down,moverectangle)19.canvas.bind_all(KeyPress-Left,moverectangle)20.canvas.bind_all(KeyPress-Right,moverectangle)21.if__name__=='__main__':22.main()8.2图形库的应用方法(1)GraphWin对象:程序可以通过GraphWin()方法定义任意数量的窗体默认标题是“GraphicsWindow”,大小200*200像素GraphWin对象的常用方法:方法名称方法含义plot(x,y,color)在窗口中(x,y)位置绘制像素。颜色参数可选,默认值为黑色。plotPixel(x,y,color)在“原始”位置(x,y)处绘制像素,忽略setCoords()方法设置的坐标变换。setBackground(color)将窗口背景颜色设为指定颜色,默认值为灰色。close()关闭屏幕上的窗口。getMouse()程序等待用户在窗口内点击鼠标,返回值为点击处的位置,并以Point对象返回。getCoords(xll,yll,xur,yur)设置窗口的坐标系。左下角是(xll,yll),右上角是(xur,yur)。所有后面的绘制都以这个坐标系做参照(plotPixel除外)。(2)图形对象:点、线段、圆、椭圆、矩形、多边形以及文本默认初始化:黑色边框,无填充图形对象通用方法:方法名称方法含义setFill(color)设置对象内部填充颜色。setOutline(color)设置对象边框颜色。setWidth(pixels)设置对象的宽度(对Point类不起作用)。draw(aGraphWin)在指定的窗口中绘制对象undrawn()从窗口中删除该对象。如该对象没有在窗口中画出将会报错。move(dx,dy)将对象沿x轴和y轴分别移动dx和dy单位长度。clone()返回对象的副本Point对象方法:方法名称方法含义Point(X,Y)以指定坐标的值(X,Y)构造一点getX()返回该点的x坐标值getY()返回该点的y坐标值Line对象方法:方法名称方法含义Line(point1,point2)构造一个从点point1到点poin
本文标题:Python 语言程序设计8
链接地址:https://www.777doc.com/doc-4210001 .html