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

本文实例为大家分享了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()

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

(0)

相关推荐

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

    本文实例为大家分享了python交互式图形编程的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python3# -*- coding: utf-8 -*- #温度转换 from graphics import * win = GraphWin("摄氏温度转换器", 400, 300) win.setCoords(0.0, 0.0, 3.0, 4.0) # 绘制接口 Text(Point(1,3), " 摄氏温度:").draw(win) Text

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

    本文实例为大家分享了python交互式图形编程实例的第三部代码,供大家参考,具体内容如下 #!/usr/bin/env python3 # -*- coding: utf-8 -*- #时钟 from turtle import * from datetime import * def Skip(step): penup() forward(step) pendown() def mkHand(name, length): #注册Turtle形状,建立表针Turtle reset() Skip(

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

    本文实例为大家分享了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.setOut

  • Python交互式图形编程的实现

    一. 1.图形显示 图素法 像素法 图素法---矢量图:以图形对象为基本元素组成的图形,如矩形. 圆形 像素法---标量图:以像素点为基本单位形成图形 2.图形用户界面:Graphical User Interface,GUI Tkinter---Python 标准GUI Graphics---基于Tkinter扩展图形库 Turtle---python内置的图形库. 3.安装graphics库 安装在D:\Python3\Lib\site-packages,网址http://mcsp.wart

  • 关于Python的GPU编程实例近邻表计算的讲解

    目录 技术背景 加速场景 基于Numba的GPU加速 总结概要 技术背景 GPU加速是现代工业各种场景中非常常用的一种技术,这得益于GPU计算的高度并行化.在Python中存在有多种GPU并行优化的解决方案,包括之前的博客中提到的cupy.pycuda和numba.cuda,都是GPU加速的标志性Python库.这里我们重点推numba.cuda这一解决方案,因为cupy的优势在于实现好了的众多的函数,在算法实现的灵活性上还比较欠缺:而pycuda虽然提供了很好的灵活性和相当高的性能,但是这要求

  • Python threading多线程编程实例

    Python 的多线程有两种实现方法: 函数,线程类 1.函数 调用 thread 模块中的 start_new_thread() 函数来创建线程,以线程函数的形式告诉线程该做什么 复制代码 代码如下: # -*- coding: utf-8 -*- import thread def f(name):   #定义线程函数   print "this is " + name   if __name__ == '__main__':   thread.start_new_thread(f

  • 从零学python系列之数据处理编程实例(二)

    在上一节从零学python系列之数据处理编程实例(一)的基础上数据发生了变化,文件中除了学生的成绩外,新增了学生姓名和出生年月的信息,因此将要成变成:分别根据姓名输出每个学生的无重复的前三个最好成绩和出生年月 数据准备:分别建立四个文本文件 james2.txt     James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22 julie2.txt        Julie Jones,2002-8-17,2.59,2.11

  • Python并发编程实例教程之线程的玩法

    目录 一.线程基础以及守护进程 二.线程锁(互斥锁) 三.线程锁(递归锁) 四.死锁 五.队列 六.相关面试题 七.判断数据是否安全 八.进程池 & 线程池 总结 一.线程基础以及守护进程 线程是CPU调度的最小单位 全局解释器锁 全局解释器锁GIL(global interpreter lock) 全局解释器锁的出现主要是为了完成垃圾回收机制的回收机制,对不同线程的引用计数的变化记录的更加精准. 全局解释器锁导致了同一个进程中的多个线程只能有一个线程真正被CPU执行. GIL锁每执行700条指

  • Python面向对象编程(二)

    目录 一.对象的继承 1.类的构造函数继承__init__(): 2.继承关系中,对象查找属性的顺序 二.类的派生 1.派生方法一(类调用) 2.派生方法二(super) 三.类的组合 四.多父类继承问题 1.新式类(MRO)列表 2.super()方法详解 五.抽象类 六.类的封装 1.私有属性:双下划线的方式__x 2.外部使用变形访问:_类名__x 3.在继承中,父类如果不想让子类覆盖自己的方法,可以将方法定义为私有的 七.类的属性(property) 1.装饰器方式 (推荐使用) 2.经

  • Python中max函数用于二维列表的实例

    最近写一个和二维列表有关的算法时候发现的 当用max求二维列表中最大值时,输出的结果是子列表首元素最大的那个列表 测试如下 c=[[1,2,-1],[0,5,6]] a=[[0,3,-1],[1,4,6]] print(max(c),max(a)) 结果是这样的 [1, 2, -1] [1, 4, 6] 以上这篇Python中max函数用于二维列表的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们. 您可能感兴趣的文章: Python中max函数用法实例分析 Py

随机推荐