Monday, June 15, 2015

tk5. Using lambda functions in Tkinter

For the last example, we will now use the lambda function so we do not need a separate function.


The function is now completely inline, within the checkbutton definition.

# tk5.py
from tkinter import *
from tkinter.ttk import *

root=Tk()
root.title('Checkbutton Status')
root.geometry('300x100')
frame = Frame(root)
frame.pack()
status = StringVar()
checkbutton = Checkbutton(frame, text='status\ncheckbutton', 
     command = lambda : display.set(status.get()),
            variable=status, onvalue='on', offvalue='off')
checkbutton.grid(row = 1, column= 0)
label = Label(frame, text = 'status', pad = 5)
label.grid(row = 0, column = 1)
display = StringVar()
display.set('off')
label1 = Label(frame, textvariable = display, pad = 5)
label1.grid(row = 1, column = 1)
root.mainloop()

No comments:

Post a Comment