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 ...
随机推荐
- SpringBoot读取application.properties文件内容
application.properties存储数据的方式是key-value. application.properties内容 userManager.userFile=data/user.pro ...
- JAVA 原子操作类
上文中,guava代码中就用到了,在这里再专门捋一下 部分内容源自: https://www.jianshu.com/p/712681f5aecd https://www.yiibai.com/jav ...
- es6 初级之let
1.在es6 中,定义变量使用 let. 1.1 var定义变量: <!DOCTYPE html> <html lang="en"> <head> ...
- javascript:查看一个图片是否加载完成
查看一个图片是否加载完成:<img id="img1" src="http://pic1.xxx.com/wall/f/51c3bb99a21ea.jpg" ...
- Emac
https://emacs-china.org/ 启动 console模式的emacs,$emacs -nw 学习参考: 1)<21天精通emacs> 2)Emacs常用命令简集. 3)& ...
- Linux Shell 内建命令:冒号(:)
https://blog.csdn.net/honghuzhilangzixin/article/details/7073312/ 在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据 ...
- Java IO中转换流的作用
在<Java网络编程>中,有这样一段话: ”Reader和Writer最重要的子类是InputStreamReader和OutputStreamWriter类. InputStreamRe ...
- [PHP]快速实现:将二维数组转为一维数组
如何将下面的二维数组转为一维数组. $msg = array( array( 'id'=>'45', 'name'=>'jack' ), array( 'id'=>'34', 'na ...
- ArcGIS案例学习笔记3_1_ArcMap编辑练习
ArcGIS案例学习笔记3_1_ArcMap编辑练习 计划时间:第三天上午 目的:ArcMap编辑练习 教程: pdf page67 数据: gis_ex10/ex07 方法: 1.新建shp文件 目 ...
- sublime text3 激活码——许可证
亲测: 现在是公元2018年6月4日.晴 ``` ----- BEGIN LICENSE ----- sgbteam Single User License EA7E-1153259 8891CBB9 ...