Python: Tkinter、ttk编程之计算器
起源:
研究Python UI编程,我偏喜欢其原生组件,于是学习Tkinter、ttk组件用法。找一计算器开源代码,略加修整,以为备忘。
其界面如图所示:

1、源代码(Python 2.7):
# encoding: UTF-8 from Tkinter import *
from ttk import * calc = Tk()
calc.title('计算器')
calc.resizable(False, False) buttons = [
'', '', '', '*', 'C',
'', '', '', '/', 'Neg',
'', '', '', '-', '$',
'', '.', '=', '+', '@'] # set up GUI
row = 1
col = 0
style = Style()
style.configure('BW.TButton', padding=12)
for i in buttons:
action = lambda x=i: click_event(x)
Button(calc, text=i, width=7, command=action, style='BW.TButton') \
.grid(row=row, column=col, sticky='nesw', )
col += 1
if col > 4:
col = 0
row += 1 display = Entry(calc, width=60)
display.grid(row=0, column=0, columnspan=5) calc.update()
w = calc.winfo_reqwidth()
h = calc.winfo_reqheight()
s_w = calc.winfo_screenwidth()
s_h = calc.winfo_screenheight()
calc.geometry('%dx%d+%d+%d' % (w, h, (s_w - w) / 2, (s_h - h) / 2)) display.focus_set() def click_event(key):
# = -> calculate results
if key == '=':
# safeguard against integer division
if '/' in display.get() and '.' not in display.get():
display.insert(END, '.0') # attempt to evaluate results
try:
result = eval(display.get())
display.insert(END, ' = ' + str(result))
except:
display.insert(END, ' Error, use only valid chars') # C -> clear display
elif key == 'C':
display.delete(0, END) # $ -> clear display
elif key == '$':
display.delete(0, END)
display.insert(END, '$$$$C.$R.$E.$A.$M.$$$$') # @ -> clear display
elif key == '@':
display.delete(0, END)
display.insert(END, 'website') # neg -> negate term
elif key == 'Neg':
if '=' in display.get():
display.delete(0, END)
try:
if display.get()[0] == '-':
display.delete(0)
else:
display.insert(0, '-')
except IndexError:
pass # clear display and start new input
else:
if '=' in display.get():
display.delete(0, END)
display.insert(END, key) # RUNTIME
calc.mainloop()
2、生成exe
反复对比py2exe及PyInstaller,发现py2exe在x64位下不能支持生成一个exe文件,而其在x32下,对Tkinter,也不能生成一个文件。
费尽工夫,也只是少生成几个文件 ,甚为不爽:

而用PyInstaller,可生成单一文件 。但验证其启动速度,甚为耗时:

综合对比,Python做UI,实非方便之物,用其胶水语言之长处,足矣!
Python: Tkinter、ttk编程之计算器的更多相关文章
- python -Tkinter 实现一个小计算器功能
文章来源:http://www.cnblogs.com/Skyyj/p/6618739.html 本代码是基于python 2.7的 如果是对于python3.X 则需要将 tkinter 改为Tk ...
- Python GUI编程(TKinter)(简易计算器)
搞课设搞得心累,现在看到人脸这两个字就烦躁,无聊搞搞tkinter,实现一个计算器的功能,能够简单的加减乘除. 简单的页面如下: 简单的代码如下: # encoding:utf-8 import tk ...
- Python GUI——tkinter菜鸟编程(中)
8. Radiobutton 选项按钮:可以用鼠标单击方式选取,一次只能有一个选项被选取. Radiobutton(父对象,options,-) 常用options参数: anchor,bg,bitm ...
- Python的GUI编程(TK)
TK在大多数 Unix平台.Windows平台和Macintosh系统都是预装好的,TKinter 模块是 Tk GUI 套件的标准Python接口.可实现Python的GUI编程. Tkinter模 ...
- Python Tkinter 学习成果:点歌软件music
笔者工作业余时间也没什么爱好,社交圈子也小,主要娱乐就是背着自己带电瓶的卖唱音响到住地附近找个人多的位置唱唱KtV. 硬件上点歌就用笔记本电脑,歌曲都是网上下载的mkv格式的含有两个音轨的视频.因此点 ...
- Python 黑帽编程大纲(变化中)
Python 黑帽编程大纲(预览版) 教程说明: 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and Defense with Pytho ...
- Python黑帽编程 3.4 跨越VLAN
Python黑帽编程 3.4 跨域VLAN VLAN(Virtual Local Area Network),是基于以太网交互技术构建的虚拟网络,既可以将同一物理网络划分成多个VALN,也可以跨越物理 ...
- Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016
Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Att ...
- Python黑帽编程1.2 基于VS Code构建Python开发环境
Python黑帽编程1.2 基于VS Code构建Python开发环境 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...
随机推荐
- zipfile模块
在python中操作zip文件, 基本上都是使用zipfile模块,他可以创建.解压文件,获取zip文件的元数据信息. 我们想要操作一个zip文件,第一步就是初始化ZipFile实例. 1.打开tes ...
- AJAX之发送GET请求
用jquery发送get请求 function AjaxSubmit1() { $.ajax({ //用jQuery发送 url: '/app04/ajax1/', type: 'GET', data ...
- Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher
Context namespace element 'annotation-config' and its parser class [org.springframework.context.anno ...
- ubuntu卸载福昕阅读器
在安装目录找到maintenancetool.sh运行之 ~/opt/foxitsoftware/foxitreader
- [namespace]PHP命名空间的动态访问 & 使用技巧
----------------------------------------------------------------------------------------------- /* | ...
- it工程师常用英文自我介绍常用用语
Good morning ! It is really my honor to have this opportunity for an interview, I hope i can make ...
- python java scala 单例模式
Python class Single2(object): """ 同一个对象 """ __instance = None def __ne ...
- RabbitMQ、Memcached、SQLAlchemy
一.RabbitMQ 1.基础概念 rabbitMQ说白了就是一个消息队列,类似于Queue,也是生产者与消费者模型.只不过做了扩展,所不同的是Queue在内存中的消息队列,而RabbitMQ是部署在 ...
- DOM精简版笔记
1.1. 基本概念 1.1.1. DOM DOM Document Object Model 文档对象模型 就是把HTML文档模型化,当作对象来处理 DOM提供的一系列属性和方法可以 ...
- reids遇到问题
今天重启爬虫服务器在连接redis数据库时突然报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not ...