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脚本对Linux服务器进行监控的教程
Apr 02 Python
状态机的概念和在Python下使用状态机的教程
Apr 11 Python
Python绘制3D图形
May 03 Python
使用Python监控文件内容变化代码实例
Jun 04 Python
数据清洗--DataFrame中的空值处理方法
Jul 03 Python
使用Python实现在Windows下安装Django
Oct 17 Python
Python使用itchat 功能分析微信好友性别和位置
Aug 05 Python
python实现大量图片重命名
Mar 23 Python
python创建n行m列数组示例
Dec 02 Python
使用pyqt 实现重复打开多个相同界面
Dec 13 Python
python爬虫实现POST request payload形式的请求
Apr 30 Python
python基于机器学习预测股票交易信号
May 25 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
FCKeditor添加自定义按钮
2008/03/27 PHP
PHP iconv 解决utf-8和gb2312编码转换问题
2010/04/12 PHP
生成ubuntu自动切换壁纸xml文件的php代码
2010/07/17 PHP
用php简单实现加减乘除计算器
2014/01/06 PHP
php微信公众号开发(3)php实现简单微信文本通讯
2016/12/15 PHP
浅谈PHP表单提交(POST&amp;GET&amp;URL编/解码)
2017/04/03 PHP
php 类中的常量、静态属性、非静态属性的区别
2017/04/09 PHP
Jquery实现无刷新DropDownList联动实现代码
2010/03/08 Javascript
JQuery实现带排序功能的权限选择实例
2015/05/18 Javascript
JavaScript 实现的checkbox经典实例分享
2016/10/16 Javascript
微信小程序 简单DEMO布局,逻辑,样式的练习
2016/11/30 Javascript
angular或者js怎么确定选中ul中的哪几个li
2017/08/16 Javascript
vue中本地静态图片路径写法
2018/03/06 Javascript
详解React Native 屏幕适配(炒鸡简单的方法)
2018/06/11 Javascript
Vuejs 实现简易 todoList 功能 与 组件实例代码
2018/09/10 Javascript
微信小程序组件传值图示过程详解
2019/07/31 Javascript
Python中给List添加元素的4种方法分享
2014/11/28 Python
python利用datetime模块计算时间差
2015/08/04 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
浅谈Python在pycharm中的调试(debug)
2018/11/29 Python
Python不同目录间进行模块调用的实现方法
2019/01/29 Python
利用Python库Scapy解析pcap文件的方法
2019/07/23 Python
python中单下划线(_)和双下划线(__)的特殊用法
2019/08/29 Python
Python3 虚拟开发环境搭建过程(图文详解)
2020/01/06 Python
英国奢侈皮具品牌:Aspinal of London
2018/09/02 全球购物
社会保险接收函
2014/01/12 职场文书
学校安全检查制度
2014/01/27 职场文书
《真想变成大大的荷叶》教学反思
2014/04/14 职场文书
广告学专业求职信
2014/06/19 职场文书
小学假期安全广播稿
2014/09/28 职场文书
ktv服务员岗位职责
2015/02/09 职场文书
生日宴会家属答谢词
2015/09/29 职场文书
干部作风纪律整顿心得体会
2016/01/23 职场文书
python学习之panda数据分析核心支持库
2021/05/07 Python
Python列表删除重复元素与图像相似度判断及删除实例代码
2021/05/07 Python
Nginx限流和黑名单配置
2022/05/20 Servers