1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| import tkinter
def func(var):
# print(var)
display.config(text=str(var))
win = tkinter.Tk()
win.title("command & lambda")
geomopts = {'padx':5, 'pady':5}
codeframe = tkinter.Frame(win)
xscrollbar = tkinter.Scrollbar(codeframe, orient=tkinter.HORIZONTAL)
xscrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
yscrollbar = tkinter.Scrollbar(codeframe)
yscrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
text = tkinter.Text(codeframe, wrap=tkinter.NONE,
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.pack()
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
code = """import tkinter
def func(var):
# print(var)
display.config(text=str(var))
win = tkinter.Tk()
win.title("command & lambda")
geomopts = {'padx':5, 'pady':5}
codeframe = tkinter.Frame(win)
xscrollbar = tkinter.Scrollbar(codeframe, orient=tkinter.HORIZONTAL)
xscrollbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
yscrollbar = tkinter.Scrollbar(codeframe)
yscrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
text = tkinter.Text(codeframe, wrap=tkinter.NONE,
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
text.pack()
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
widgetsframe = tkinter.Frame(win)
display = tkinter.Label(widgetsframe, bd=1, relief=tkinter.SUNKEN, bg='white', fg='red')
display.grid(row=0, column=0, columnspan=9,
sticky=tkinter.W+tkinter.E+tkinter.N+tkinter.S, **geomopts)
tkinter.Label(widgetsframe, text="Whitout lambda",
fg='blue').grid(row=1, column=0, **geomopts)
tkinter.Label(widgetsframe, text="lambda : func(value)",
fg='blue').grid(row=1, column=2, **geomopts)
tkinter.Label(widgetsframe, text="lambda value=value: func(value)",
fg='blue').grid(row=1, column=4, **geomopts)
tkinter.Label(widgetsframe, text="lambda value=staticentry.get(): func(value)",
fg='blue').grid(row=1, column=6, **geomopts)
tkinter.Label(widgetsframe, text="lambda : func(variableentry.get())",
fg='blue').grid(row=1, column=8, **geomopts)
for line in range(1, 6):
tkinter.Button(widgetsframe, text="func %d" % (line),
command=func(line)).grid(row=line+2, column=0, **geomopts)
tkinter.Button(widgetsframe, text="func %d" % (line),
command=lambda : func(line)).grid(row=line+2,
column=2,
**geomopts)
tkinter.Button(widgetsframe, text="func %d" % (line),
command=lambda line=line: func(line)).grid(row=line+2,
column=4,
**geomopts)
staticentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
font=("Helvetica", "12", "bold italic"))
staticentry.grid(row=4, column=6, **geomopts)
staticentry.insert(0, "Enter text")
staticentry.bind("<1>", lambda event: staticentry.delete(0, tkinter.END))
variableentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
font=("Helvetica", "12", "bold italic"))
tkinter.Button(widgetsframe, text="Apply text",
command=lambda value=staticentry.get(): func(value)).grid(row=5, column=6, **geomopts)
variableentry.grid(row=4, column=8, **geomopts)
variableentry.insert(0, "Enter text")
variableentry.bind("<1>", lambda event: variableentry.delete(0, tkinter.END))
tkinter.Button(widgetsframe, text="Apply text",
command=lambda : func(variableentry.get())).grid(row=5, column=8,
**geomopts)
tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
for c in range(1, 9, 2):
tkinter.Frame(widgetsframe, width=2, bd=1,
relief=tkinter.SUNKEN).grid(row=1, column=c, rowspan=7,
sticky=tkinter.N+tkinter.S,
**geomopts)
tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=8, column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
codeframe.grid(row=0, column=0, columnspan=2, **geomopts)
codeframe.grid_remove()
widgetsframe.grid(row=0, column=0, columnspan=2, **geomopts)
wb = tkinter.Button(win, text="Widgets",
command=lambda: codeframe.grid_remove() or widgetsframe.grid())
wb.grid(row=1, column=0, **geomopts)
wc = tkinter.Button(win, text="Code",
command=lambda: widgetsframe.grid_remove() or codeframe.grid())
wc.grid(row=1, column=1, **geomopts)
tkinter.Frame(win, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
wc = tkinter.Button(win, text="Exit", command=win.destroy)
wc.grid(row=3, column=0, columnspan=2, **geomopts)
win.mainloop()
"""
text.insert(tkinter.END, code)
widgetsframe = tkinter.Frame(win)
display = tkinter.Label(widgetsframe, bd=1, relief=tkinter.SUNKEN, bg='white', fg='red')
display.grid(row=0, column=0, columnspan=9,
sticky=tkinter.W+tkinter.E+tkinter.N+tkinter.S, **geomopts)
tkinter.Label(widgetsframe, text="Whitout lambda",
fg='blue').grid(row=1, column=0, **geomopts)
tkinter.Label(widgetsframe, text="lambda : func(value)",
fg='blue').grid(row=1, column=2, **geomopts)
tkinter.Label(widgetsframe, text="lambda value=value: func(value)",
fg='blue').grid(row=1, column=4, **geomopts)
tkinter.Label(widgetsframe, text="lambda value=staticentry.get(): func(value)",
fg='blue').grid(row=1, column=6, **geomopts)
tkinter.Label(widgetsframe, text="lambda : func(variableentry.get())",
fg='blue').grid(row=1, column=8, **geomopts)
for line in range(1, 6):
tkinter.Button(widgetsframe, text="func %d" % (line),
command=func(line)).grid(row=line+2, column=0, **geomopts)
tkinter.Button(widgetsframe, text="func %d" % (line),
command=lambda : func(line)).grid(row=line+2,
column=2,
**geomopts)
tkinter.Button(widgetsframe, text="func %d" % (line),
command=lambda line=line: func(line)).grid(row=line+2,
column=4,
**geomopts)
staticentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
font=("Helvetica", "12", "bold italic"))
staticentry.grid(row=4, column=6, **geomopts)
staticentry.insert(0, "Enter text")
staticentry.bind("<1>", lambda event: staticentry.delete(0, tkinter.END))
variableentry = tkinter.Entry(widgetsframe, bd=1, relief=tkinter.SUNKEN,
font=("Helvetica", "12", "bold italic"))
tkinter.Button(widgetsframe, text="Apply text",
command=lambda value=staticentry.get(): \
func(value)).grid(row=5, column=6, **geomopts)
variableentry.grid(row=4, column=8, **geomopts)
variableentry.insert(0, "Enter text")
variableentry.bind("<1>", lambda event: variableentry.delete(0, tkinter.END))
tkinter.Button(widgetsframe, text="Apply text",
command=lambda : func(variableentry.get())).grid(row=5, column=8,
**geomopts)
tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, \
column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
for c in range(1, 9, 2):
tkinter.Frame(widgetsframe, width=2, bd=1,
relief=tkinter.SUNKEN).grid(row=1, column=c, rowspan=7,
sticky=tkinter.N+tkinter.S,
**geomopts)
tkinter.Frame(widgetsframe, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=8, \
column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
codeframe.grid(row=0, column=0, columnspan=2, **geomopts)
codeframe.grid_remove()
widgetsframe.grid(row=0, column=0, columnspan=2, **geomopts)
wb = tkinter.Button(win, text="Widgets",
command=lambda: codeframe.grid_remove() or \
widgetsframe.grid())
wb.grid(row=1, column=0, **geomopts)
wc = tkinter.Button(win, text="Code",
command=lambda: widgetsframe.grid_remove() or \
codeframe.grid())
wc.grid(row=1, column=1, **geomopts)
tkinter.Frame(win, height=2, bd=1, relief=tkinter.SUNKEN).grid(row=2, \
column=0, columnspan=9, sticky=tkinter.E+tkinter.W, **geomopts)
wc = tkinter.Button(win, text="Exit", command=win.destroy)
wc.grid(row=3, column=0, columnspan=2, **geomopts)
win.mainloop() |
Partager