Nov-10-2021, 04:36 PM
You could make your own class.
import tkinter as tk class MyLabel(tk.Label): '''A Label with padding that takes on parents background color''' def __init__(self, parent=None, text='', row=0, col=0, padx=5, pady=5, **kwargs): bg = None if parent is None else parent['bg'] super().__init__(parent, text=text, bg=bg, **kwargs) self.grid(row=row, column=col, padx=padx, pady=pady) root = tk.Tk() root.configure(bg='black') MyLabel(root, "Red", 0, 0, fg='blue') MyLabel(root, "Blue", 1, 1, fg='green') MyLabel(root, "Green", 2, 2, fg='red') root.mainloop()You could write a helper function.
import tkinter as tk def new_label(parent=None, text='', row=0, col=0, padx=5, pady=5, **kwargs): bg = None if parent is None else parent['bg'] label = tk.Label(parent, text=text, bg=bg, **kwargs) label.grid(row=row, column=col, padx=padx, pady=pady) return label root = tk.Tk() root.configure(bg='black') new_label(root, "Red", 0, 0, fg='blue') new_label(root, "Blue", 1, 1, fg='green') new_label(root, "Green", 2, 2, fg='red') root.mainloop()You could make your controls in a loop.
import tkinter as tk root = tk.Tk() root.configure(bg='black') for index, config in enumerate(zip(('Red', 'Blue', 'Green'), ('blue', 'green', 'red'))): text, fg = config label = tk.Label(root, text=text, fg=fg, bg='black') label.grid(row=index, column=index, padx=5, pady=5) root.mainloop()