码迷,mamicode.com
首页 > 其他好文 > 详细

Partial Function Example

时间:2018-11-13 02:30:46      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:button   text   bubuko   oss   error   rail   rom   ack   图片   

from functools import partial as pto
from tkinter import Tk, Button, X
from tkinter.messagebox import showinfo, showwarning, showerror

WARN = ‘warn‘
CRIT = ‘crit‘
REGU = ‘regu‘

SIGNS = {
    ‘do not enter‘: CRIT,
    ‘railroad crossing‘: WARN,
    ‘55\nspeed linit‘: REGU,
    ‘wrong way‘: CRIT,
    ‘merging traffic‘: WARN,
    ‘one way‘: REGU,
}

critCB = lambda: showerror(‘Error‘, ‘Error Button Pressed!‘)
warnCB = lambda: showwarning(‘Warning‘, ‘Warning Button Pressed!‘)
infoCB = lambda: showinfo(‘Info‘, ‘Info Button Pressed!‘)

top = Tk()
top.title(‘Road Signs‘)
top.geometry(‘250x300‘)
Button(top, text=‘QUIT‘, command=top.quit, bg=‘red‘, fg=‘white‘).pack()

MyButton = pto(Button, top)
CritButton = pto(MyButton, command=critCB, bg=‘white‘, fg=‘red‘)
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()‘)
    eval(cmd)

top.mainloop()
    

技术分享图片

Partial Function Example

标签:button   text   bubuko   oss   error   rail   rom   ack   图片   

原文地址:https://www.cnblogs.com/TmHm/p/9950003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!