起源:

研究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. day41-解决粘包问题

    一.socket缓冲区 研究粘包之前先看看socket缓冲区的问题: 二.socket缓存区的详细解释 每个socket被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区. write()/send ...

  2. Spring cloud Eureka 和 Zookeeper 比较

    Eureka       AP Zookeeper CP 好处: 

  3. ubuntu安装pgAdmin 4

    One way to install pgadmin4 is to download its Python wheel at https://www.postgresql.org/ftp/pgadmi ...

  4. python 读取文件

    python 一次读取多行 with open(filename, "r") as f: lines = f.readlines(LINE_BATCH) while lines: ...

  5. Android Studio 版本间区别

    2.3.2 ->3.0.1  Gradle版本为4.1   com.android.tools.build:gradle:3.0.x Android Monitor 被换成了 Android P ...

  6. python 拷贝 深拷贝 浅拷贝 赋值

    t = [1,["a","b"]] t_bak = t t_cop = copy.copy(t) t_deep = copy.deepcopy(t) print ...

  7. MVC 2nd

    步骤 3 创建控制器. StudentController.java public class StudentController { private Student model; private S ...

  8. c#栈和队列习题

    3.1 比较线性表.栈和队列这三种数据结构的相同点和不同点. 栈(Stack)是限定只能在表的一端进行插入和删除操作的线性表.队列(Queue)是限定只能在表的一端进行插入和在另一端进行删除操作的线性 ...

  9. centos 安装python3.6

    环境准备 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 首先去官网下 ...

  10. Python 如何创建2维空数组

    http://blog.csdn.net/yockie/article/details/46127829 myList = [ ( [0] * 3 ) for i in range(4) ] > ...