Saving some time with a LabelInput class

Every input widget on our form has a label associated with it. In a small application, we can just create the label and input separately, then add each to the parent frame as follows:

form = Frame()
label = Label(form, text='Name')
name_input = Entry(form)
label.grid(row=0, column=0)
name_input.grid(row=1, column=0)

That works fine and you could do it that way for your application, but it also creates a lot of tedious, repetitious code, and moving inputs around means changing twice as much code. Since the label and input widgets belong together, it would be smart to create a small wrapper class to contain both and establish some universal defaults.

When coding, be on the lookout for sections that contain ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.