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(Point(1,1), " 华氏温度:").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),"转换")
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("退出")
# 等待响应鼠标点击,退出程序
win.getMouse()
win.close()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#方块移动

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)
  tk.mainloop()

if __name__ == '__main__':
  main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from graphics import *

def convert(input):
  celsius = eval(input.getText())  # 输入转换
  fahrenheit = 9.0/5.0 * celsius + 32
  return fahrenheit
def colorChange(win,input):
  cnum = eval(input.getText())
  weight = cnum / 100.0
  newcolor = color_rgb(int(255*weight),int(66+150*(1-weight)),int(255*(1-weight)))
  win.setBackground(newcolor)
def main():
  win = GraphWin("摄氏温度转换", 400, 300)
  win.setCoords(0.0, 0.0, 3.0, 4.0)
  # 绘制输入接口
  Text(Point(1,3),
     " 摄氏温度:").draw(win)
  Text(Point(2,2.7),
     " (请输入: 0.0-100.0 )").draw(win)
  Text(Point(1,1),
     "华氏温度:").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),"转换")
  button.draw(win)
  rect = Rectangle(Point(1,1.5), Point(2,2.5))
  rect.draw(win)
  # 等待鼠标点击
  win.getMouse()
  result = convert(input)  # 转换输入
  output.setText(result)  # 显示输出
  # 改变颜色
  colorChange(win,input)
  # 改变按钮字体
  button.setText("退出")
  # 等待点击事件,退出程序
  win.getMouse()
  win.close()

if __name__ == '__main__':
  main()

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

(0)

相关推荐

  • 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交互式图形编程实例(一)

    本文实例为大家分享了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交互式图形编程的实现

    一. 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并发和异步编程实例

    关于并发.并行.同步阻塞.异步非阻塞.线程.进程.协程等这些概念,单纯通过文字恐怕很难有比较深刻的理解,本文就通过代码一步步实现这些并发和异步编程,并进行比较.解释器方面本文选择python3,毕竟python3才是python的未来,并且python3用原生的库实现协程已经非常方便了. 1.准备阶段 下面为所有测试代码所需要的包 #! python3 # coding:utf-8 import socket from concurrent import futures from selecto

  • python socket通信编程实现文件上传代码实例

    这篇文章主要介绍了python socket通信编程实现文件上传代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 写一个file_receive.py和一个file_send.py程序,由file_send.py上传一个文件,file_receive.py接收上传的文件,写到指定的包内 #file_receive.py import socket,subprocess,os BASE_DIR = os.path.dirname(os.pa

  • Python函数式编程实例详解

    本文实例讲述了Python函数式编程.分享给大家供大家参考,具体如下: 函数式编程就是一种抽象程度很高的编程范式,从计算机硬件->汇编语言->C语言->Python抽象程度越高.越贴近于计算,但执行效率也越低.纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用.而允许使用变量的程序设计语言,由于函数内部的变量状态不确定,同样的输入,可能得到不同的输出,因此,这种函数是有副作用的.函数式编程的一个特点就是,允许把函数

随机推荐