python交互式图形编程
简单图形编程
笑脸代码
from graphics import*win = GraphWin()
face = Circle(Point(100,95),50)
leftEye = Circle(Point(80,80),5)
leftEye.setFill("yellow")
leftEye.setOutline("red")
rightEye = Circle(Point(120,80),5)
rightEye.setFill("yellow")
rightEye.setOutline("red")
mouth = Line(Point(80,110),Point(120,110))face.draw(win)
mouth.draw(win)
leftEye.draw(win)
rightEye.draw(win)input("Press <enter>")
图形界面编程
小栗子1:点击10次图形界面框,控制打印点击的坐标
from graphics import*def main():win = GraphWin("Click Me!")for i in range(10):p = win.getMouse()print("You clicked at:",p.getX(),p.getY())
main()
运行结果
小栗子2:交互式绘制五边形
#coding=gbk
from graphics import*def main():win = GraphWin("Draw a polygon",300,300)win.setCoords(0.0, 0.0, 300.0, 300.0)message = Text(Point(150,20),"Click on five points")message.draw(win)#获得多边形的6个点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)p6 = win.getMouse()p6.draw(win)#使用Polygon对象绘制多边形polygon = Polygon(p1,p2,p3,p4,p5,p6)polygon.setFill("peachpuff")polygon.setOutline("black")polygon.draw(win)#等待响应鼠标事件,退出程序message.setText("Click anywehere to quit.")win.getMouse()
main()
小栗子3:温度转换
#coding=gbk
from graphics import *win = GraphWin("Celsius Converter", 400, 300)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# 绘制接口
Text(Point(1,3), " Celsius Temperature:").draw(win)
Text(Point(1,1), "Fahrenheit Temperature:").draw(win)
input = Entry(Point(2,3), 5)
input.setText("0.0")
input.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"Convert It")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# 等待鼠标点击
win.getMouse()
# 转换输入
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32.0
# 显示输出,改变按钮
output.setText(fahrenheit)
button.setText("Quit")
# 等待响应鼠标点击,退出程序
win.getMouse()
win.close()
使用Tkinter创建GUI
小栗子1:tk库初试
from tkinter import *
tk = Tk()
label = Label(tk,text = "Welcome to Python Tkinter")
button = Button(tk, text = "Click Me")
label.pack()
button.pack()
tk.mainloop()
小栗子2:响应用户事件
#coding=gbk
from tkinter import *#定义回调函数
def processOK():print("OK button is clicked")def processCancel():print("Cancel button is clicked")def main():tk=Tk()btnOK = Button(tk,text = "OK",fg = "red",command = processOK)btnCancel = Button(tk, text = "Cancel", bg = "yellow",command = processCancel)btnOK.pack()btnCancel.pack()tk.mainloop()
main()
小栗子3:画布显示文字、图片或者绘制图形
from tkinter import *def main():tk = Tk()canvas = Canvas(tk,width = 200, height = 200)canvas.pack()canvas.create_text(100,40,text = "Welcom to Tkinter",fill = "blue",font = ("Times",16))myImage = PhotoImage(file = "python_logo.gif")canvas.create_image(10,70,anchor = NW, image = myImage)canvas.create_rectangle(10,70,190,130)tk.mainloop()
main()
小栗子4:上下左右键控制图形移动
from tkinter import *def main(): tk = Tk()canvas = Canvas(tk, width = 400, height = 400)canvas.pack()def moverectangle(event):if event.keysym == "Up":canvas.move(1,0,-5)elif event.keysym == "Down":canvas.move(1,0,5)elif event.keysym == "Left":canvas.move(1,-5,0)elif event.keysym == "Right":canvas.move(1,5,0)canvas.create_rectangle(180,180,220,220,fill="red")canvas.bind_all("<KeyPress-Up>",moverectangle)canvas.bind_all("<KeyPress-Down>",moverectangle)canvas.bind_all("<KeyPress-Left>",moverectangle)canvas.bind_all("<KeyPress-Right>",moverectangle)main()
python交互式图形编程相关推荐
- python的交互式解释器_python3.4.1解释器python交互式图形编程实例(三)
本文实例为大家分享了python交互式图形编程实例的第三部代码,供大家参考,具体内容如下 #!/usr/bin/env python3 # -*- coding: utf-8 -*- #时钟 from ...
- python图形化编程实例,python交互式图形编程实例(三)
本文实例为大家分享了python交互式图形编程实例的第三部代码,供大家参考,具体内容如下 #!/usr/bin/env python3 # -*- coding: utf-8 -*- #时钟 from ...
- Python学习笔记·交互式图形编程
Python学习笔记·交互式图形编程 注:在校计算机学生一名,菜鸟一枚,最近开始学习Python的基础知识希望有什么不对的地方各位大佬能够不令赐教! 课程是在中国大学MOOC上学的,有兴趣的同学可以自 ...
- python学习--交互式图形编程实例四
#!/usr/bin/env python3 # -*- coding: utf-8 -*-#画雪 from turtle import * from random import * def grou ...
- python三维图形编程极坐标_Python – 极地立体图
立体投影引起了一些令人头疼的问题,并且可能是投影,它引起了纸盒多边形变换代码的大多数问题. 以下示例显示了如何使用cartopy生成极坐标立体图. 请注意:即使使用此代码,也可以调整示例数据分辨率,并 ...
- python简单的编程_简单的Python2.7编程初学经验总结
如果你从来没有使用过Python,我强烈建议你阅读Python introduction,因为你需要知道基本的语法和类型. 包管理 Python世界最棒的地方之一,就是大量的第三方程序包.同样,管理这 ...
- 《树莓派Python编程入门与实战》——3.5 关于Python交互式shell
本节书摘来异步社区<树莓派Python编程入门与实战>一书中的第3章,第3.5节,作者:[美]Richard Blum,更多章节内容可以访问云栖社区"异步社区"公众号查 ...
- python图形编程pdf_Python计算机视觉编程
可以很好地理解每种算法及具体思路,对于有python基础的人来说是很好的计算机视觉入门书籍,介绍怎样通过Python进行图形图像处理的一本书,里面包含了很多Python库的介绍和用法,非常好!< ...
- 美观实用!Star 过万,用 Python 做交互式图形的这款工具火了!
点击上方"Datawhale",选择"星标"公众号 第一时间获取价值内容 转自量子位,作者乾明 GitHub 上,一份用 Python 做交互式图形的资源火了. ...
最新文章
- plotly基于dataframe数据绘制股票自定义K线图
- Android 架构组件 - 让天下没有难做的 App
- DELL R740XD 开启 TPM功能
- boost::biconnected_components用法的测试程序
- 07.suggester简述
- linux下GPRS模块的应用程序
- 第二百二十三节,jQuery EasyUI,ComboBox(下拉列表框)组件
- Ubuntu系统下Python虚拟环境构建详解
- mybatis 执行插入操作,insert 返回1,数据库中无数据。数据库中数据的创建时间和插入执行时间不一致。
- sql 孩子兄弟表示法 所有子节点_matlab实现随机+蓄意攻击网络节点(1)
- 动态规划之子序列问题
- [Machine Learning Algorithm] 决策树与迭代决策树(GBDT)
- 安装esxi时候的No Network Adapters报错 解决办法
- 利用阿里云邮件推送免费发邮件,每天免费200封,速度快,还高大上
- 最快倾斜摄影三维建模-台式、便携、多机集群配置推荐
- minmax()函数
- HDU P4699 Editor
- CSP共空间模式详解
- 网页被劫持会造成什么影响
- MiniFly微型四轴学习与开发日志(三)