O'Reilly logo

Python GUI Programming with Tkinter by Alan D. Moore

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required