目标:

  1.编写一个gui,生成按钮

  2.通过偏函数,生成按钮

  3.通过装饰器,实现按钮输出信息功能

1.使用Tkinter,创建一个按钮

代码如下:

handetiandeMacBook-Pro:~ xkops$ cat button.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- import Tkinter
#定义一个窗口
root = Tkinter.Tk()
#定义一个按钮
b1 = Tkinter.Button(root, foreground='white', background='blue', text='Button1') #包装
b1.pack() root.mainloop()

•运行代码,效果如下图

2.通过使用偏函数定义按钮(偏函数定义一些相通部分的内容)

代码如下:

handetiandeMacBook-Pro:~ xkops$ cat button.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- import Tkinter
from functools import partial root = Tkinter.Tk()
#使用偏函数定义相同的内容
MyButton = partial(Tkinter.Button, root, foreground='white', background='blue') b1 = Tkinter.Button(root, foreground='white', background='blue', text='Button1')
b2 = MyButton(text='Button2')
b3 = MyButton(text='Button3')
b4 = MyButton(text='quit') b1.pack()
b2.pack()
b3.pack()
b4.pack() root.mainloop()

•运行代码,测试效果

3.定义函数,实现点击button2按钮,输出"Hello,world"功能,点击quit按钮,关闭窗口功能。

代码如下:

handetiandeMacBook-Pro:~ xkops$ cat button.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- import Tkinter
from functools import partial def greet():
print "Hello, world"
root = Tkinter.Tk() MyButton = partial(Tkinter.Button, root, foreground='white', background='blue') b1 = Tkinter.Button(root, foreground='white', background='blue', text='Button1')
b2 = MyButton(text='Button2', command=greet)
b3 = MyButton(text='Button3')
b4 = MyButton(text='quit', command=root.quit) b1.pack()
b2.pack()
b3.pack()
b4.pack() root.mainloop()

•运行代码,点击Button2和quit按钮查看效果

4.通过编写装饰器实现,点击不同按钮,打印不同的信息。

代码如下:

handetiandeMacBook-Pro:~ xkops$ cat button.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- import Tkinter
from functools import partial def greet(word):
def welcome():
print "Hello, %s" % word
return welcome root = Tkinter.Tk() MyButton = partial(Tkinter.Button, root, foreground='white', background='blue') b1 = Tkinter.Button(root, foreground='white', background='blue', text='Button1')
b2 = MyButton(text='Button2', command=greet('world'))
b3 = MyButton(text='Button3', command=greet('Python'))
b4 = MyButton(text='quit', command=root.quit) b1.pack()
b2.pack()
b3.pack()
b4.pack() root.mainloop()

•运行代码,测试效果,点击Button2,后台输出"Hello, world", 点击Button3后台输出"Hello, Python"

Python偏函数实例的更多相关文章

  1. python基础——实例属性和类属性

    python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...

  2. python 发送邮件实例

    留言板回复作者邮件提醒 -----------2016-5-11 15:03:58-- source:python发送邮件实例

  3. python Cmd实例之网络爬虫应用

    python Cmd实例之网络爬虫应用 标签(空格分隔): python Cmd 爬虫 废话少说,直接上代码 # encoding=utf-8 import os import multiproces ...

  4. Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取

    很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...

  5. Python爬虫实例:爬取猫眼电影——破解字体反爬

    字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...

  6. Python爬虫实例:爬取豆瓣Top250

    入门第一个爬虫一般都是爬这个,实在是太简单.用了 requests 和 bs4 库. 1.检查网页元素,提取所需要的信息并保存.这个用 bs4 就可以,前面的文章中已经有详细的用法阐述. 2.找到下一 ...

  7. python 创建实例--待完善

    今天好好琢磨一下 python 创建实例的先后顺序 一. 就定义一个普通类 Util (默认)继承自 object,覆写 new ,init 方法 class Util(object): def __ ...

  8. pcapng文件的python解析实例以及抓包补遗

    为了弥补pcap文件的缺陷,让抓包文件可以容纳更多的信息,pcapng格式应运而生.关于它的介绍详见<PCAP Next Generation Dump File Format> 当前的w ...

  9. 生产消费者模式与python+redis实例运用(中级篇)

    上一篇文章介绍了生产消费者模式与python+redis实例运用(基础篇),但是依旧遗留了一个问题,就是如果消费者消费的速度跟不上生产者,依旧会浪费我们大量的时间去等待,这时候我们就可以考虑使用多进程 ...

随机推荐

  1. Mounting File Systems

    1.Mounting File Systems Just creating a partition and putting a file system on it is not enough to s ...

  2. java 线程池用法

    public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ...

  3. Excel连接字符串(Oledb)

    Excel的连接字符串参数详解 2011-07-27 15:43:50|  分类: 编程专栏 |  标签:excel  连接字符串  参数  含义   |字号大中小 订阅     程序中读取Excel ...

  4. 《CSS网站布局实录》学习笔记(五)

    第五章 CSS内容排版 5.1 文字排版 5.1.1 通栏排版 进行网页通栏排版时,只要直接将段落文字放置于p或者其他对象中,再对段落文字应用间距.行距.字号等样式控制,便形成了排版雏形. 5.1.2 ...

  5. PHP常用代码大全

    1.连接MYSQL数据库代码 <?php $connec=mysql_connect("localhost","root","root" ...

  6. swift 重载 泛式 inout的使用

    swift 重载 泛式 inout的使用 函数 func 关键字 -> 表示返回值信息等等 那我们接下来利用函数做几件事情 -a 比较两个数字的大小 -b 比较两个字符串 -c 既能比较字符串, ...

  7. UIWebView加载不了页面, 但在电脑的浏览器上可以打开

    经排查, 系因为URL中包含有中文, 所以无法加载页面, 解决方法如下: 将URL进行转码 NSString *urlStr =[[NSString stringWithFormat:@"h ...

  8. CSS3过渡效果实现菜单划出效果

    下载地址 这是大体上的原理,当然案例比这个多 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  9. JavaScript 显示弹出窗口

    window . showModalDialog ( sURL,vArguments , sFeatures )参数说明: sURL--必选参数,用来指定对话框要显示的文档的URL. //要显示页面的 ...

  10. SQL中删除同一字段中重复的值

    /////////////////////目地:ZDJZ_DIS中 name字段有重复的值,删除重复的值 DELETE * FROM ZDJZ_DIS WHERE NAME IN (select NA ...