def createButton(self): CRIT = 'crit' WARN = 'warn' REGU = 'regu' SIGNS = { 'do not enter': CRIT, 'railroad crossing': WARN, '55\nspeed limit': 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') self.quitButton = Button(self, text='Quit', bg='red', fg='white', command=self.quit) self.quitButton.pack() # 调用mybutton时,会调用Button类,并将self作为第一个参数传递 mybutton = pto(Button, self) # 二阶偏函数,根据不同类型 CritButton = pto(mybutton, command=critCB, bg='white', fg='red') WarnButton = pto(mybutton, command=warnCB, bg='blue') ReguButton = pto(mybutton, command=infoCB, bg='white') for eachSign in SIGNS: signType = SIGNS[eachSign] cmd = '%sButton(text=%r%s).pack(fill=X, expand=1)' % ( signType.title(), eachSign, '.upper()' if signType == CRIT else '.title()') eval(cmd)
def roadSigns(): WARN = 'warn' CRIT = 'crit' REGU = 'regu' SIGNS = { 'do not enter' : CRIT, 'railroad crossing' : WARN, '55\nspeed limit' : 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') 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()
def roadSigns(): WARN = 'warn' CRIT = 'crit' REGU = 'regu' SIGNS = { 'do not enter': CRIT, 'railroad crossing': WARN, '55\nspeed limit': 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') 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()
def BuildStreetSignsGUI(): WARN = 'warn' # Warning CRIT = 'crit' # Critical message REGU = 'regu' # Regular message SIGNS = { 'Do Not Enter': CRIT, 'Railroad Crossing': WARN, '65 Speed Limit': 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') quit = Button(top, text='QUIT', command=top.quit, bg='red', fg='white') quit.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)
def test1(): WARN = "warn" CRIT = "crit" REGU = "regu" SIGNS = { "do not enter": CRIT, "railroad crossing": WARN, "55\nspeed limit": REGU, "wrong way": CRIT, "merging traffic": WARN, "one way": REGU, } critCB = lambda: messagebox.showerror("Error", "Error Button Pressed!") warnCB = lambda: messagebox.showwarning("Warning", "Warning Button Pressed!") infoCB = lambda: messagebox.showinfo("Info", "Info Button Pressed!") top = tkinter.Tk() top.title("Road Signs") tkinter.Button(top, text="QUIT", command=top.quit, bg="red", fg="white").pack() MyButton = pto(tkinter.Button, top) CritButton = pto(MyButton, command=critCB, bg="white", fg="red") WarnButton = pto(MyButton, command=warnCB, bg="white") ReguButton = pto(MyButton, command=infoCB, bg="white") for eachSign in SIGNS: signType = SIGNS[eachSign] cmd = "%sButton(text=%r%s).pack(fill=tkinter.X, expand=True)" % ( signType.title(), eachSign, ".upper()" if signType == CRIT else ".title()") eval(cmd) top.mainloop() pass
SIGNS = { 'do not enter': CRIT, 'railroad crossing': WARN, '55\nspeed limit': 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') 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()
CRIT = "crit" REGU = "regu" signs = {"do not enter:":CRIT, "railroad crossing":WARN, "55\nspeed limit":REGU, "wront way":CRIT, "merging traffic":WARN, "one way":REGU, } critCB = lambda: messagebox.showerror("Error", "Error button Pressed!") warnCB = lambda: messagebox.showerror("Warning", "Warnig Button Pressed!") infoCB = lambda: messagebox.showinfo("Info", "Info button Pressed!") top = Tk() top.title("Road signs") Button(top, text = 'Quit', command = top.quit, bg = "red", fg = "white").pack() mybotton = pto(Button, top) CritButton = pto(mybotton, command = critCB) WarnButton = pto(mybotton, command = warnCB) ReguButton = pto(mybotton, command = infoCB) for each in signs: signtype = signs[each] cmd = "%sButton(text=%r%s).pack(fill=X, expand=True)" % (signtype.title(), each,".upper()" if signs == CRIT else ".title()") eval(cmd) top.mainloop()
SIGNS = { 'do not enter': CRIT, 'railroad crossing': WARN, '55\nspeed limit': 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') 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='green') ReguButton = pto(myButton, command=infoCB, bg='white') for eachSign in SIGNS: signType = SIGNS[eachSign] #print(signType, signType.title()) cmd = '%sButton(text=%r%s).pack(fill=X, expand=True)' % (signType.title( ), eachSign, '.upper()' if signType == CRIT else '.title()') eval(cmd) top.mainloop()
signs = { "do not enter:": CRIT, "railroad crossing": WARN, "55\nspeed limit": REGU, "wront way": CRIT, "merging traffic": WARN, "one way": REGU, } critCB = lambda: messagebox.showerror("Error", "Error button Pressed!") warnCB = lambda: messagebox.showerror("Warning", "Warnig Button Pressed!") infoCB = lambda: messagebox.showinfo("Info", "Info button Pressed!") top = Tk() top.title("Road signs") Button(top, text='Quit', command=top.quit, bg="red", fg="white").pack() mybotton = pto(Button, top) CritButton = pto(mybotton, command=critCB) WarnButton = pto(mybotton, command=warnCB) ReguButton = pto(mybotton, command=infoCB) for each in signs: signtype = signs[each] cmd = "%sButton(text=%r%s).pack(fill=X, expand=True)" % ( signtype.title(), each, ".upper()" if signs == CRIT else ".title()") eval(cmd) top.mainloop()
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, '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') Button(top, text='QUIT', command=top.quit(), bg='green', fg='blue').pack() MyButton = pto(Button, top) CritButton = pto(MyButton, command=critCB, bg='yellow', fg='pick') WarnButton = pto(MyButton, command=warnCB, bg='goldenrod1') CritButton = pto(MyButton, command=infoCB, bg='white') for eachSign in SIGNS: signType = SIGNS[eachSign] cmd = '%s Button(text=%r%s).pack(fill=X, expand=True)' % ( signType.title(), eachSign, '.upper()' if signType == CRIT else '.title()') eval(cmd) top.mainloop()
REGU = "regu" SIGNS = { "do not enter": CRIT, "railroad crossing": WARN, "55\nspeed limit": REGU, "merging traffic": WARN, "wrong way": CRIT, "one way": REGU, } critCB = lambda: showerror("Error", "Error Button Pressed!") warnCB = lambda: showwarning("Warning", "Warning Button Pressed!") reguCB = lambda: showinfo("Info", "Info Button Pressed") top = Tk() top.title("Road Signs") 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=reguCB, bg="white") for each_sign in SIGNS: sign_type = SIGNS[each_sign] cmd = "%sButton(text=%r%s).pack(fill=X, expand=True)" % (sign_type.title( ), each_sign, '.upper()' if sign_type == CRIT else '.title()') eval(cmd) top.mainloop()
return a * a f = lambda a: a * a print(square(5)) print(f(6)) # dont seem to be of great use right # Lets see in the next example # let us get the other guy out of the way # partial functions are function created on top of function with parameters half filled # EXAMPLE def func(a, b, c, d): x = a + b + c + d return x # whatever that means # Now this supecomplex function has under certain conditions a=1,b=2,c=4 which only d changing from functools import partial as pto # I can now define a new func easy_func with these parameters fixed easy_func = pto(func, 1, 2, 4) print(func(1, 2, 3, 4)) print(easy_func(3)) # thats all boss nothing extra ordinary # BUT WHY THESE TWO CONCEPTS RANDOMLY IN THE MIDDLE OF SOMETHING # The next example will help you understand and thene maybe we do a bit of functional programming # and iterators and generators and this new thing calles ups from the apc standard
"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!") #创建顶层窗口并创建一个quit按钮 top = Tk() top.title("Road Signs") Button(top, text="QUIT", command=top.quit, bg="red", fg="white").pack() #使用二阶偏函数进行模板化 MyButton = pto(Button, top) #一阶模板化Button类和根窗口 CritButton = pto(MyButton, command=critCB, bg="white", fg="red") WarnButton = pto(MyButton, command=warnCB, bg="goldenrod1") ReguButton = pto(MyButton, command=infoCB, bg="white") #格式化创建命令字符串并通过eval函数实例化 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()
'one way': REGU } crit_CB = lambda: showerror('Error', 'Error Button Pressed!') warn_CB = lambda: showwarning('Warning', 'Warning Button Pressed!') info_CB = lambda: showinfo('Info', 'Info Button Pressed!') top = Tk() top.geometry('250x300') top.title('Road Signs') # формируются шаблон класса Button и корневое окно top. Эrо # означает, что при каждом вызове в проrрамме объекта MyButton # этот объект вызывает Button (метод Tkinter.Button() создает # кнопку) с указанием top в качестве первоrо параметра. My_button = pto(Button, top) # применяется первый созданный объект, MyButton, и на ero # основе создаются шаблоны Crit_button = pto(My_button, command=crit_CB, bg='white', fg='red') Warn_button = pto(My_button, command=warn_CB, bg='yellow') Regu_button = pto(My_button, command=info_CB, bg='white') for each_sign in SIGNS: sign_type = SIGNS[each_sign] # Формируется строка, которую может вычислить интерпретатор # Python, включающая имя создаваемой кнопки, в качестве # текстового параметра передается метка на кнопке, затем к # параметрам применяется метод pack(). Если формируемый знак # является запрещающим, то буквы метки на кнопке преобразуются # в прописные, в противном случае в прописную преобразуются
"railroad crossing": WARN, "55\nspeed limit": 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") 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()
REGU = "regu" SIGNS = { "do not enter": CRIT, "railroad crossing": WARN, "55\nspeed limit": REGU, "wrong way": CRIT, "meging 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.Tk() top.title("Road Signs") tk.Button(top, text="QUIT", command=top.quit, bg="red", fg="white").pack() MyButton = pto(tk.Button, top) CritButton = pto(MyButton, command=critCB, bg="white", fg='red') WarnButton = pto(MyButton, command=warnCB, bg="goldnrod1") ReguButton = pto(MyButton, command=infoCB, bg="white") for eachSign in SIGNS: signType = SIGNS[eachSign] cmd = '%sButton (text={%r}{%s}).pack(fill=\"x\",expand=True)'.format( signType.title(), eachSign, ".upper()" if signType == CRIT else ".title()") eval(cmd) top.mainloop()
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') # bg在Mac上无效, 建议使用ttk替代Tkinter Button(top, text='QUIT', command=top.quit, activebackground='red', activeforeground='white').pack() MyButton = pto(Button, top) CritButton = pto(MyButton, command=critCB, activebackground='white', activeforeground='red') WarnButton = pto(MyButton, command=warnCB, activebackground='goldenrod1') ReguButton = pto(MyButton, command=infoCB, activebackground='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()
SINS = { 'do not enter': CRIT, 'railroad crossing': WARN, '55\nspeed limit': 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') 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 SINS: signType = SINS[eachSign] cmd = '%sButton(text=%r%s).pack(fill=X,expand=True)' % (signType.title( ), eachSign, '.upper()' if signType == CRIT else '.title()') # print cmd eval(cmd) top.mainloop()
WARN = "warn" CRIT = "crit" REGU = "regu" SIGNS = { "do not enter":CRIT, "railroad crossing":WARN, "55\nspeed limit":REGU, "wrong way":CRIT, "meging 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.Tk() top.title("Road Signs") tk.Button(top,text="QUIT",command=top.quit,bg="red",fg="white").pack() MyButton = pto(tk.Button,top) CritButton = pto(MyButton,command=critCB,bg="white",fg='red') WarnButton = pto(MyButton,command=warnCB,bg="goldnrod1") ReguButton = pto(MyButton,command=infoCB,bg="white") for eachSign in SIGNS: signType = SIGNS[eachSign] cmd = '%sButton (text={%r}{%s}).pack(fill=\"x\",expand=True)'.format(signType.title(),eachSign,".upper()" if signType == CRIT else ".title()") eval(cmd) top.mainloop()