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


Posted in Python onNovember 17, 2017

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

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

Python 相关文章推荐
Python 获取新浪微博的最新公共微博实例分享
Jul 03 Python
Python常用知识点汇总
May 08 Python
python3获取两个日期之间所有日期,以及比较大小的实例
Apr 08 Python
Python3.6日志Logging模块简单用法示例
Jun 14 Python
Django中日期处理注意事项与自定义时间格式转换详解
Aug 06 Python
django获取from表单multiple-select的value和id的方法
Jul 19 Python
Python json转字典字符方法实例解析
Apr 13 Python
Python xml、字典、json、类四种数据类型如何实现互相转换
May 27 Python
Tensorflow与Keras自适应使用显存方式
Jun 22 Python
Python通过类的组合模拟街道红绿灯
Sep 16 Python
matplotlib设置颜色、标记、线条,让你的图像更加丰富(推荐)
Sep 25 Python
python输出国际象棋棋盘的实例分享
Nov 26 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
Python算法之图的遍历
Nov 16 #Python
You might like
PHP学习 变量使用总结
2011/03/24 PHP
PHP缓冲区用法总结
2016/02/14 PHP
php递归函数怎么用才有效
2018/02/24 PHP
JS中完美兼容各大浏览器的scrolltop方法
2015/04/17 Javascript
纯javascript判断查询日期是否为有效日期
2015/08/24 Javascript
javascript实现在指定元素中垂直水平居中
2015/09/13 Javascript
javascript点击按钮实现隐藏显示切换效果
2016/02/03 Javascript
js实现分割上传大文件
2016/03/09 Javascript
JavaScript拖动层Div代码
2017/03/01 Javascript
iOS + node.js使用Socket.IO框架进行实时通信示例
2017/04/14 Javascript
AngularJS双向数据绑定原理之$watch、$apply和$digest的应用
2018/01/30 Javascript
使用node打造自己的命令行工具方法教程
2018/03/26 Javascript
React Form组件的实现封装杂谈
2018/05/07 Javascript
echarts实现折线图的拖拽效果
2019/12/19 Javascript
vue+iview使用树形控件的具体使用
2020/11/02 Javascript
编写简单的Python程序来判断文本的语种
2015/04/07 Python
自动化Nginx服务器的反向代理的配置方法
2015/06/28 Python
使用Python的PIL模块来进行图片对比
2016/02/18 Python
python执行系统命令后获取返回值的几种方式集合
2018/05/12 Python
python中的&amp;&amp;及||的实现示例
2019/08/07 Python
python集合的创建、添加及删除操作示例
2019/10/08 Python
Python编程快速上手——strip()函数的正则表达式实现方法分析
2020/02/29 Python
CSS3自定义滚动条样式 ::webkit-scrollbar的示例代码详解
2020/06/01 HTML / CSS
HTML5 drag和drop具体使用详解
2021/01/18 HTML / CSS
美体小铺瑞典官方网站:The Body Shop瑞典
2018/01/27 全球购物
EJB的角色和三个对象
2015/12/31 面试题
副厂长岗位职责
2014/02/02 职场文书
cf战队收人口号
2014/06/21 职场文书
开发房地产协议书
2014/09/14 职场文书
2015年党员自评材料
2014/12/17 职场文书
2015年办公室文秘工作总结
2015/04/30 职场文书
在职证明格式样本
2015/06/15 职场文书
水浒传读书笔记
2015/06/25 职场文书
婚庆公司开业主持词
2015/06/30 职场文书
InterProcessMutex实现zookeeper分布式锁原理
2022/03/21 Java/Android
Python日志模块logging用法
2022/06/05 Python