Home > Software design >  Is there a way to set the padding, width, anchor, etc for all widgets of a specific type in tkinter?
Is there a way to set the padding, width, anchor, etc for all widgets of a specific type in tkinter?

Time:06-16

I'm creating a small tkinter app that takes multiple inputs for varying uses. The GUI for it is split up into different frames with entries and buttons. So far each of my labels have the same values for padding, to be anchored to the west, same width. Is there a way of setting a global style for labels instead of copy/pasting it over again for each new label?

The code is written on an offline machine, but it looks similar to this, save the other aspects of codes for the buttons/entries.

from tkinter import *

root = Tk()

# frame 1
top_frame = LabelFrame(root, text = "File Checker")
top_frame.grid(row=0, column=0, padx=10, sticky="w")

pjt_title = Label(top_frame, text="Project Title:", anchor="w", width=13, padx=30).grid(row=0, column=0)
pjt_num = Label(top_frame, text="Project Number:", anchor="w", width=13, padx=30).grid(row=1, column=0)
pjt_ver = Label(top_frame, text="Project Version:", anchor="w", width=13, padx=30).grid(row=2, column=0)


# frame 2

mid_frame = LabelFrame(root, text = "Programmer Information")
mid_frame.grid(row=1, column=0, padx=10, sticky="w")

pjt_title = Label(mid_frame, text="Project Title:", anchor="w", width=13, padx=30).grid(row=0, column=0)
pjt_num = Label(mid_frame, text="Project Number:", anchor="w", width=13, padx=30).grid(row=1, column=0)

# frame 3

bot_frame = LabelFrame(root, text = "Project Information")
bot_frame.grid(row=0, column=0, padx=10, sticky="w")


pjt_desc = Label(bot_frame, text="Project Descrption:", anchor="w", width=13, padx=30).grid(row=0, column=0)
pjt_lead = Label(bot_frame, text="Project Lead:", anchor="w", width=13, padx=30).grid(row=1, column=0)
pjt_date = Label(bot_frame, text="Project Start Date:", anchor="w", width=13, padx=30).grid(row=2, column=0)
pjt_cost = Label(bot_frame, text="Project Cost:", anchor="w", width=13, padx=30).grid(row=3, column=0)

root.mainloop()

CodePudding user response:

We put the same label options into a dictionary and then unpack it in place. If more than two or three nearly identical widgets use a loop. When creating widgets and placing them using geometry managers on the same line, you are in for a surprise, I showed it with print. In the first case, you get an object, in the second, None.

from tkinter import *

options = {'anchor': "w", 'width': 13, 'padx': 30}

root = Tk()

# frame 2

mid_frame = LabelFrame(root, text="Programmer Information")
mid_frame.grid(row=1, column=0, padx=10, sticky="w")

(pjt_title := Label(mid_frame, text="Project Title:", **options)).grid(row=0, column=0)
pjt_num = Label(mid_frame, text="Project Number:", **options).grid(row=1, column=0)
print(pjt_title)
print(pjt_num)

# frame 3

bot_frame = LabelFrame(root, text = "Project Information")
bot_frame.grid(row=0, column=0, padx=10, sticky="w")

text = ("Project Descrption:", "Project Lead:", "Project Start Date:", "Project Cost:")

for i in range(4):
    Label(bot_frame, text=text[i], **options).grid(row=i, column=0)

root.mainloop()
  • Related