python交互式图形编程实例(二)


Posted in Python onNovember 17, 2017

本文实例为大家分享了python交互式图形编程的第二部分代码,供大家参考,具体内容如下

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#画个笑脸

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)
win.mainloop()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#鼠标点击,返回其坐标值
from graphics import *
def main():
  win = GraphWin("Click Me!")
  for i in range(10):
    p = win.getMouse()
    print("你点击的位置:", p.getX(), p.getY())

if __name__ == '__main__':
  main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#鼠标点击,返回其坐标值
from graphics import *

win = GraphWin("画一个多边形", 300,300)
win.setCoords(0.0,0.0,300.0,300.0)
message = Text(Point(150, 20),"点击五次")
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)

#使用Polygon对象绘制多边形
polygon = Polygon(p1,p2,p3,p4,p5)
polygon.setFill("black")
polygon.setOutline("red")
polygon.draw(win)

#等待响应鼠标事件,退出程序
message.setText("点击任何地方退出")
win.getMouse()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 画几何图形
import turtle
 
def main():
  turtle.pensize(3)
  turtle.penup()
  turtle.goto(-200,-50)
  turtle.pendown()
  turtle.begin_fill()
  turtle.color("red")
  turtle.circle(40, steps=3)
  turtle.end_fill()
 
 
  turtle.penup()
  turtle.goto(-100,-50)
  turtle.pendown()
  turtle.begin_fill()
  turtle.color("blue")
  turtle.circle(40, steps=4)
  turtle.end_fill()
 
  turtle.penup()
  turtle.goto(0,-50)
  turtle.pendown()
  turtle.begin_fill()
  turtle.color("green")
  turtle.circle(40, steps=5)
  turtle.end_fill()
 
  turtle.penup()
  turtle.goto(100,-50)
  turtle.pendown()
  turtle.begin_fill()
  turtle.color("yellow")
  turtle.circle(40, steps=6)
  turtle.end_fill()
 
  turtle.penup()
  turtle.goto(200,-50)
  turtle.pendown()
  turtle.begin_fill()
  turtle.color("purple")
  turtle.circle(40)
  turtle.end_fill()
 
  turtle.color("green")
  turtle.penup()
  turtle.goto(-100,50)
  turtle.pendown()
  turtle.write(("Cool Colorful shapes"),
    font = ("Times", 18, "bold"))
  turtle.hideturtle()
 
  turtle.done()
 
if __name__ == '__main__':
  main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#模拟聊天框
from tkinter import *
import time
 
def main():
 
 def sendMsg():#发送消息
  strMsg = '我:' + time.strftime("%Y-%m-%d %H:%M:%S",
                 time.localtime()) + '\n '
  txtMsgList.insert(END, strMsg, 'greencolor')
  txtMsgList.insert(END, txtMsg.get('0.0', END))
  txtMsg.delete('0.0', END)
   
 def cancelMsg():#取消消息
  txtMsg.delete('0.0', END)
 
 def sendMsgEvent(event): #发送消息事件
  if event.keysym == "Up":
   sendMsg()
 
 #创建窗口 
 t = Tk()
 t.title('与python聊天中')
    
 #创建frame容器
 frmLT = Frame(width=500, height=320, bg='white')
 frmLC = Frame(width=500, height=150, bg='white')
 frmLB = Frame(width=500, height=30)
 frmRT = Frame(width=200, height=500)
  
 #创建控件
 txtMsgList = Text(frmLT)
 txtMsgList.tag_config('greencolor', foreground='#008C00')#创建tag
 txtMsg = Text(frmLC);
 txtMsg.bind("<KeyPress-Up>", sendMsgEvent)
 btnSend = Button(frmLB, text='发 送', width = 8, command=sendMsg)
 btnCancel = Button(frmLB, text='取消', width = 8, command=cancelMsg)
 imgInfo = PhotoImage(file = "python.gif")
 lblImage = Label(frmRT, image = imgInfo)
 lblImage.image = imgInfo
 
 #窗口布局
 frmLT.grid(row=0, column=0, columnspan=2, padx=1, pady=3)
 frmLC.grid(row=1, column=0, columnspan=2, padx=1, pady=3)
 frmLB.grid(row=2, column=0, columnspan=2)
 frmRT.grid(row=0, column=2, rowspan=3, padx=2, pady=3)
 #固定大小
 frmLT.grid_propagate(0)
 frmLC.grid_propagate(0)
 frmLB.grid_propagate(0)
 frmRT.grid_propagate(0)
  
 btnSend.grid(row=2, column=0)
 btnCancel.grid(row=2, column=1)
 lblImage.grid()
 txtMsgList.grid()
 txtMsg.grid()
 
 #主事件循环
 t.mainloop()
 
if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网络编程学习笔记(一)
Jun 09 Python
python进阶教程之循环对象
Aug 30 Python
python 读取txt中每行数据,并且保存到excel中的实例
Apr 29 Python
Python检查和同步本地时间(北京时间)的实现方法
Dec 03 Python
python运行时强制刷新缓冲区的方法
Jan 14 Python
Python登录系统界面实现详解
Jun 25 Python
python迭代器常见用法实例分析
Nov 22 Python
python实现tail实时查看服务器日志示例
Dec 24 Python
Python参数传递实现过程及原理详解
May 14 Python
Python 列表推导式需要注意的地方
Oct 23 Python
python遍历路径破解表单的示例
Nov 21 Python
Python的轻量级ORM框架peewee使用教程
Feb 05 Python
python交互式图形编程实例(一)
Nov 17 #Python
Python金融数据可视化汇总
Nov 17 #Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
Nov 17 #Python
Python中super函数的用法
Nov 17 #Python
python使用正则表达式替换匹配成功的组
Nov 17 #Python
python定时利用QQ邮件发送天气预报的实例
Nov 17 #Python
详解python eval函数的妙用
Nov 16 #Python
You might like
php 删除无限级目录与文件代码共享
2008/11/22 PHP
php5.3不能连接mssql数据库的解决方法
2014/12/27 PHP
PHP空值检测函数与方法汇总
2017/11/19 PHP
tp5框架前台无限极导航菜单类实现方法分析
2020/03/29 PHP
javascript是怎么继承的介绍
2012/01/05 Javascript
jquery选择器的选择使用及性能介绍
2013/01/16 Javascript
提高jQuery性能的十个诀窍
2013/11/14 Javascript
关于js内存泄露的一个好例子
2013/12/09 Javascript
JavaScript使用slice函数获取数组部分元素的方法
2015/04/06 Javascript
JS实现仿google、百度搜索框输入信息智能提示的实现方法
2015/04/20 Javascript
JavaScript编写点击查看大图的页面半透明遮罩层效果实例
2016/05/09 Javascript
基于javascript实现表格的简单操作
2016/05/21 Javascript
聊一聊JS中的prototype
2016/09/29 Javascript
微信js-sdk地理位置接口用法示例
2016/10/12 Javascript
微信小程序联网请求的轮播图
2017/07/07 Javascript
requireJS模块化实现返回顶部功能的方法详解
2017/10/16 Javascript
利用vue.js实现被选中状态的改变方法
2018/02/08 Javascript
Vue.js进阶知识点总结
2018/04/01 Javascript
微信小程序日历组件使用方法详解
2018/12/29 Javascript
微信小程序自定义弹出层效果
2020/05/26 Javascript
浅谈vue使用axios的回调函数中this不指向vue实例,为undefined
2020/09/21 Javascript
Python入门学习之字符串与比较运算符
2015/10/12 Python
Python中断言Assertion的一些改进方案
2016/10/27 Python
Python subprocess模块功能与常见用法实例详解
2018/06/28 Python
pyqt远程批量执行Linux命令程序的方法
2019/02/14 Python
python等差数列求和公式前 100 项的和实例
2020/02/25 Python
django实现日志按日期分割
2020/05/21 Python
谈谈python垃圾回收机制
2020/09/27 Python
世界上最大的巴士旅游观光公司:Big Bus Tours
2016/10/20 全球购物
Max&Co官网:意大利年轻女性时尚品牌
2017/05/16 全球购物
不开辟用于交换数据的临时空间,如何完成字符串的逆序
2012/12/02 面试题
应届生求职信写作技巧
2013/10/24 职场文书
专项法律服务方案
2014/06/11 职场文书
2019年最新证婚词精选集!
2019/06/28 职场文书
HTML基础-标签分类(闭合标签,空标签,块级元素,行内元素,行级块元素,可替换元素)
2021/03/31 HTML / CSS
Django项目如何正确配置日志(logging)
2021/04/29 Python