起源:

研究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编程之计算器的更多相关文章

  1. python -Tkinter 实现一个小计算器功能

    文章来源:http://www.cnblogs.com/Skyyj/p/6618739.html 本代码是基于python 2.7的 如果是对于python3.X  则需要将 tkinter 改为Tk ...

  2. Python GUI编程(TKinter)(简易计算器)

    搞课设搞得心累,现在看到人脸这两个字就烦躁,无聊搞搞tkinter,实现一个计算器的功能,能够简单的加减乘除. 简单的页面如下: 简单的代码如下: # encoding:utf-8 import tk ...

  3. Python GUI——tkinter菜鸟编程(中)

    8. Radiobutton 选项按钮:可以用鼠标单击方式选取,一次只能有一个选项被选取. Radiobutton(父对象,options,-) 常用options参数: anchor,bg,bitm ...

  4. Python的GUI编程(TK)

    TK在大多数 Unix平台.Windows平台和Macintosh系统都是预装好的,TKinter 模块是 Tk GUI 套件的标准Python接口.可实现Python的GUI编程. Tkinter模 ...

  5. Python Tkinter 学习成果:点歌软件music

    笔者工作业余时间也没什么爱好,社交圈子也小,主要娱乐就是背着自己带电瓶的卖唱音响到住地附近找个人多的位置唱唱KtV. 硬件上点歌就用笔记本电脑,歌曲都是网上下载的mkv格式的含有两个音轨的视频.因此点 ...

  6. Python 黑帽编程大纲(变化中)

    Python 黑帽编程大纲(预览版) 教程说明: 本系列教程,采用的大纲母本为<Understanding Network Hacks Attack and Defense with Pytho ...

  7. Python黑帽编程 3.4 跨越VLAN

    Python黑帽编程 3.4 跨域VLAN VLAN(Virtual Local Area Network),是基于以太网交互技术构建的虚拟网络,既可以将同一物理网络划分成多个VALN,也可以跨越物理 ...

  8. Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016

    Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Att ...

  9. Python黑帽编程1.2 基于VS Code构建Python开发环境

    Python黑帽编程1.2  基于VS Code构建Python开发环境 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...

随机推荐

  1. Delphi FrieDAC 大数据处理

    Delphi FrieDAC 大数据处理 大数据处理, 要用到Array DML 插入数据 先要设置插入的数据量 FQuery1.Params.ArraySize := 1000; for index ...

  2. Go语言学习笔记(2)

    数组 var a [2]string a[0] = "Hello" a[1] = "World" primes := [6]int{2, 3, 5, 7, 11 ...

  3. 关于PHP Notice: A non well formed numeric value encountered, 你知道多少

    ---------------------------------------------------------------------------------------------- A non ...

  4. Android DevArt6:Android中IPC的六种方式

    Android中IPC的六种方式 1.使用Bundle 最简单的进程间通信方式:Intent + Bundle: 支持三大组件:Activity.Service.BroadcastReceiver : ...

  5. AAPTEXECPTION

    Error:java.util.concurrent.ExecutionException: com.android.builder.\files-.aar\a234c5f0534a8da0e4db0 ...

  6. 0 开发的准备工作一一虚拟机virturalbox

    https://www.virtualbox.org/wiki/Linux_Downloads官网下载linux版本 https://www.ubuntu.com/desktop/developers ...

  7. 学JS的心路历程-JS支持面向对象?(一)

    昨天在看Prototype看到JS支持面向对象,被前辈问到说那什么是面向对象?JS是面向对象语言吗? 便开始了一连串艰辛爬文过程,今天就来看一下两者有什么差异吧(rgwyjc)! 首先面向对象有三大特 ...

  8. workerman Channel组件全局广播

    <?phpuse Workerman\Worker; require_once '../../web/Workerman/Autoloader.php';require_once '../../ ...

  9. 辅助测试工具xip.io

    http://xip.io/ https://github.com/basecamp/xip-pdns

  10. linux安装memcached和php的memcache扩展 (已使用)

    所需软件libevent-1.4.6-stable.tar.gz (http://monkey.org/~provos/libevent/)memcache-2.2.3.tgz (http://pec ...