python交互式图形编程实例(一)
作者:hayden__wang 发布时间:2022-11-12 14:44:53
标签: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()
来源:http://www.cnblogs.com/hayden1106/p/7844488.html


猜你喜欢
- 环境准备python3.5.4windows redispip install celerypip install rediswindows
- 本文实例为大家分享了python实现人机五子棋的具体代码,供大家参考,具体内容如下图形界面引用PyQt5,还有socket通信。可以局域网对
- JavaScript由于同源策略的限制,跨域通信一直是棘手的问题。当然解决方案也有很多: 1.document.domain+iframe的
- 背景:今天才申请了腾讯云+校园计划的1元服务器,安装了Centos7.0,在安装MySQL的时候,使用yum list | grep mys
- numpy.ndarray添加元素平常使用的比较多的是list,在list后面添加元素直接是data_list = []data_list.
- echarts legend点击事件首先,明确本篇文章的重点,主要有三个:1. 给legend添加点击事件2. 禁用legend点击事件的默
- 1、由于国外网站太慢,所以这里使用的是阿里的镜像https://mirrors.aliyun.com/pypi/simple/2、去官网查看
- 今天刚接触python,查看了一些环境建立的文章,可能是年代久远很多都不适用,现在mac搭建python环境变得更简单。大神勿喷。首先去py
- 前言本文主要给大家介绍了关于python指定时间调用函数的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:在前面的一
- CREATETABLE`users`(`id`int(10)NOTNULLAUTO_INCREMENT,`name`char(50)NOTN
- 当感觉mysql性能出现问题时,通常会先看下当前mysql的执行状态,使用 show processlist 来查看,例如:其中state状
- location.href这个东西常常用于跳转,location既是window对象的属性,又是document对象的属性。JavaScri
- 对于MySQL数据库,如果你要使用事务以及行级锁就必须使用INNODB引擎。如果你要使用全文索引,那必须使用myisam。 INNODB的实
- 简单的测试用例#!/usr/bin/python# -*- coding: UTF-8 -*-import timefrom pymongo
- 弄了半天也没有找到原因,然后又重新到网上下载几个,还是不行,郁闷坏了,最后结合其他编辑器的用法,才知道是配置文件夹中上传文件夹的路径写错了
- 前言在日常开发中,我们经常需要判断目标元素是否在视窗之内或者和视窗的距离小于一个值(例如 100 px),从而实现一些常用的功能,例如:图片
- 本文实例讲述了Python数据结构与算法之完全树与最小堆。分享给大家供大家参考,具体如下:# 完全树 最小堆class CompleteTr
- 定义函数:CREATE FUNCTION [dbo].[GetAge] ( @BirthDay nvarchar(2
- 0. 简介传统的并发编程模型是基于线程和共享内存的同步访问控制的,共享数据受锁的保护,线程将争夺这些锁以访问数据。通常而言,使用线程安全的数
- 一、property() 函数讲解了解 @property 装饰器之前,我们首先要了解内置函数的 property()。class prop