Python图形界面编程---Tkinter模块使用
2016-07-20 15:19
831 查看
偏函数在GUI应用举例
具体内容可以参考Python函数式编程——偏函数(PFA)
偏函数允许
预存函数变量并
冻结这些预定参数,运行所需的变量再
解冻,由这些
最终的参数去调用函数
# -*- coding: UTF-8 -*- from functools import partial as pto from Tkinter import Tk, Button, X from tkMessageBox import showinfo, showwarning,showerror #导入消息模块 WARN = 'warn' CRIT = 'crit' REGU = 'regu' #字典 SIGNS = { 'do not enter': CRIT, 'railroad crossing':WARN, '55\nspeed limit':REGU, 'merging traffic':WARN, 'one way':REGU, } #匿名函数 函数式编程 给3种类型的提示button传参 critCB = lambda: showerror('Error', 'Error Button Pressed!') warnCB = lambda: showwarning('Warning', 'Warning Button Pressed!') infoCB = lambda: showinfo('Info', 'Info Button Pressond!') #顶层界面 top = Tk() top.title('Road Signs') Button(top, text='QUIT', command=top.quit, bg='red', fg='white').pack() #偏函数 MyButton = pto(Button, top)#模板化按钮和top窗口为mybutton CritButton = pto(MyButton, command=critCB, bg='white', fg='red')#再次模板化MyButton WarnButton = pto(MyButton, command=warnCB, bg='goldenrod1') ReguButton = pto(MyButton, command=infoCB, bg='white') for eachSign in SIGNS:#字典循环 signType = SIGNS[eachSign]#取出值 #创建表达式,相当于 cmd = '%sButton(text=%r%s).pack(fill=X, expand=True)' % (signType.title(), eachSign, '.upper()' if signType == CRIT else '.title()') print cmd eval(cmd)#eval函数将字符串当成有效Python表达式来求值,并返回计算结果 top.mainloop()
创建的5个cmd字符串:
CritButton(text='do not enter'.upper()).pack(fill=X, expand=True) WarnButton(text='railroad crossing'.title()).pack(fill=X, expand=True) ReguButton(text='55\nspeed limit'.title()).pack(fill=X, expand=True) ReguButton(text='one way'.title()).pack(fill=X, expand=True) WarnButton(text='merging traffic'.title()).pack(fill=X, expand=True)
其他更高级的控件
比如下拉列表,复选框等等具体看文档开发即可。相关文章推荐
- wxpython 编程触发菜单或按钮事件
- [numpy]split()和array_split()
- Python 黑帽编程大纲(变化中)
- 李航《统计学习方法》第四章——用Python实现朴素贝叶斯分类器(MNIST数据集)
- 【python-mysql】在ubuntu下安装python-mysql环境
- Python图形界面编程---Tkinter模块基础
- python 类成员变量
- Python爬虫----实例: 抓取百度百科Python词条相关1000个页面数据
- python学习第八天 -- 迭代
- Python 验证码识别
- Python字符编码详解
- Python批量删除特定文件
- Python isdigit()方法
- Python入门:字符转换为二进制及进制之间转换
- pythonj基础之 多线程
- Python 写文件
- Python读取写入TXT正确姿势
- Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。
- [numpy]matrix和array的乘和加
- python基础之 re(正则表达式)模块学习