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 ...
随机推荐
- EmEditor
姓 名:ttrar.com 序 列 号:DKAZQ-R9TYP-5SM2A-9Z8KD-3E2RK 免费版地址:https://zh-cn.emeditor.com/#download
- SpringBoot配置定时任务的两种方式
一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- span标签 宽度无效解决方案
完美的解决方案 下 面代码的CSS定义完美解决了span的宽度设置问题. 由于浏览器通常对不支持的CSS属性采取忽略处理的态度, 所以最好将display:inline -block行写在后面,这样在 ...
- CSS 的 ID 和 Class 有什么区别,如何正确使用它们。
css只用class来写并有专门的class通用和私有模块命名, id具有唯一性且优先级太高只作为js操作dom的挂钩全部不添加样式, 如果使用jq或zepto的话,操作的class类名一般也不加样式 ...
- 尚硅谷springboot学习19-日志切换
查看相关依赖关系,排除相关依赖,引入新的日志依赖 slf4j+log4j的方式: <dependency> <groupId>org.springframework.boot& ...
- Spring格式化注解
Spring Framework 3.0发布了.这里我们介绍其中的一个:用于格式化的注解.简介 Spring 3 提供了两个可以用于格式化数字.日期和时间的注解@NumberFormat和@DateT ...
- 16.1 用auth0服务 实现用登录和管理 使用auth版本的2个大坑。
这是三周内容,实现用户登录和管理 回到master分支 切换到 han分支 更新一下 然后工作 开始工作写代码了 安装2个angular端的auth0的lib,也可不安装,后边有不安装的做法 不安装的 ...
- Hadoop集群(三) Hbase搭建
前面已经完成Zookeeper和HDFS的安装,本文会详细介绍Hbase的安装步骤.以及安装过程中遇到问题的汇总. 系列文章: Hadoop集群(一) Zookeeper搭建 Hadoop集群(二 ...
- Error logged from Ant UI:
2down vote Have a look at the Eclipse error log. I recently saw following error there: Error logged ...
- vim字符串替换及小技巧
vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法(vi 真是强大啊,还有很多需要学习),记录几种在此,方便以后查询. :s/vivian/s ...