Python——交互式图形编程,,1、图形显示图素法像
Python——交互式图形编程,,1、图形显示图素法像
1、图形显示
图素法
像素法
图素法---矢量图:以图形对象为基本元素组成的图形,如矩形、 圆形
像素法---标量图:以像素点为基本单位形成图形
2、图形用户界面:Graphical User Interface,GUI
Tkinter---Python 标准GUI
Graphics---基于Tkinter扩展图形库
Turtle---python内置的图形库。
3、安装graphics库
安装在D:\Python3\Lib\site-packages,网址http://mcsp.wartburg.edu/zelle/python/graphics.py
4、graphics库
(1)创建图形窗口
图形窗口
点(像素)的集合
GraphWin对象尺寸默认值:高200像素,宽200像素。
参考坐标系
? Graphics\Tkinter
? 点(0,0)表示屏幕左上角
? X轴正方向为从左到右
? Y轴正方向为从上到下。
? 默认窗口大小为200*200
简洁形式
(2)点
移动点
move(x,y)方法
清除原来点的图像,并在新位置重新绘制
两个数字参数:x,y
(2)圆
1 from graphics import * 2 win=GraphWin() 3 leftEye=Circle(Point(80,80),5) 4 leftEye.setFill("yellow") 5 leftEye.setOutline("red") 6 rightEye=leftEye 7 rightEye.move(40,0) 8 9 leftEye.draw(win)10 rightEye.draw(win)
左眼右眼重叠了,说明,移动后原来的图就不存在了。
from graphics import *win=GraphWin()leftEye=Circle(Point(80,80),5)leftEye.setFill("yellow")leftEye.setOutline("red")rightEye=Circle(Point(120,80),5)rightEye.setFill("yellow")rightEye.setOutline("red")leftEye.draw(win)rightEye.draw(win)
(3)face
1 from graphics import * 2 3 win=GraphWin() 4 face=Circle(Point(100,95),50) 5 leftEye=Circle(Point(80,80),5) 6 leftEye.setFill("yellow") 7 leftEye.setOutline("red") 8 rightEye=Circle(Point(120,80),5) 9 rightEye.setFill("yellow")10 rightEye.setOutline("red")11 mouth=Line(Point(80,110),Point(120,110))12 13 face.draw(win)14 mouth.draw(win)15 leftEye.draw(win)16 rightEye.draw(win)
5、交互式图形接口
图形用户界面(图形用户接口),
? 采用图形方式显示的计算机操作用户界面
? 用于程序的输入和输出
? 事件驱动
Graphics模块
? 隐藏了底层事件的处理机制,
? 提供了获得用户在窗口中的输入
? 捕捉鼠标点击
? 处理文本输入
(1)捕捉鼠标点击
1 from graphics import * 2 3 def main(): 4 win=GraphWin("Click me")#标题栏名 5 for i in range(10): 6 p=win.getMouse() 7 print("you click at:",p.getX(),p.getY()) 8 if __name__=="__main__": 9 main()10
(2)四边形
1 from graphics import * 2 3 def main(): 4 win=GraphWin("draw a polygon",500,500)#标题栏名 5 win.setCoords(0,0,500,500)#变换坐标,左下角和右上角 6 message=Text(Point(250,50),"click on four points")#下面中心位置 7 message.draw(win) 8 9 #获取四个点10 p1=win.getMouse()11 p1.draw(win)12 p2=win.getMouse()13 p2.draw(win)14 p3=win.getMouse()15 p3.draw(win)16 p4=win.getMouse()17 p4.draw(win)18 19 #顺次画出闭合图形20 polygon=Polygon(p1,p2,p3,p4)21 polygon.setFill("red")22 polygon.setOutline(‘black‘)23 polygon.draw(win)24 25 message.setText(‘click anywhere to quit‘)26 win.getMouse()27 28 if __name__=="__main__":29 main()30
6、温度转换界面
(1)输入窗口
1 from graphics import * 2 3 win = GraphWin("Celsius Converter", 400, 300)#载入界面,标题栏 4 win.setCoords(0.0, 0.0, 3.0, 4.0)#按比例转换坐标 5 6 # 绘制接口 7 Text(Point(1,3), " Celsius Temperature:").draw(win)#输入文字 8 Text(Point(1,1), "Fahrenheit Temperature:").draw(win) 9 10 input= Entry(Point(2,3),5)#前面是位置,后面是宽度,可以写数字11 input.setText("0.0")12 input.draw(win)
Entry输入可以让用户自己输入内容,setText()是填充入内容,用户可以修改。
(2)完整代码
1 from graphics import * 2 3 win = GraphWin("Celsius Converter", 400, 300)#载入界面,标题栏 4 win.setCoords(0.0, 0.0, 3.0, 4.0)#按比例转换坐标 5 6 # 绘制接口 7 Text(Point(1,3), " Celsius Temperature:").draw(win)#输入文字 8 Text(Point(1,1), "Fahrenheit Temperature:").draw(win) 9 10 input= Entry(Point(2,3),5)#前面是位置,后面是宽度,可以写数字11 input.setText("0.0")12 input.draw(win)13 14 output = Text(Point(2,1),"")#确定输出位置15 output.draw(win)16 17 button = Text(Point(1.5,2.0),"Convert It")#按钮字样18 button.draw(win)19 Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)#长方形20 21 # 等待鼠标点击22 win.getMouse()23 # 转换输入24 celsius = eval(input.getText())#得到你的输入值,getText()25 26 fahrenheit = 9.0/5.0 * celsius + 32.027 # 显示输出,改变按钮28 output.setText(fahrenheit)#输出温度值,setText()29 button.setText("Quit")30 # 等待响应鼠标点击,退出程序31 win.getMouse()32 win.close()
体会Text和Entry的区别,前者只能由程序输入内容,后者可以在图形界面输入内容;两者都是用getText()获取内容,用setText()展示内容。
Python——交互式图形编程
相关内容
- 练习:python基础+字符串,,1、执行python
- python中检测mysql的主键唯一性异常,,有两种方法:1.直
- python,tensorflow,CNN实现mnist数据集的训练与验证正确率,
- Python 基础之模块之os os.path 及os与shutil对比,,一:os 对系
- python:for循环,,for循环可以遍历任
- Python3基础 str find+index 是否存在指定字符串,有则返回
- 使用 python -m SimpleHTTPServer 快速搭建http服务 测试真实访
- Python虚拟环境导出包安装到另一台电脑的方法,,环境:
- python里读写excel等数据文件的几种常用方式,,python处理
- python中try else语句的使用,,try: 语句
评论关闭