Python Forum
[Tkinter] Settting Default Values for grid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Settting Default Values for grid
#6
You could make a mixin class that adds the features you want for every widget and use that to make your own mixin versions of Label, Entry, Button...

Maybe a better solution for you is to write a Frame subclass that takes care of the layout responsibilities.
import tkinter as tk

class Grid(tk.Frame):
    '''A tk.Frame for lazy typers'''
    def __init__(self, parent=None, bg=None, **kwargs):
        if bg is None and parent is not None:
            bg = parent['bg']
        self.bg = bg
        super().__init__(parent, bg=self.bg, **kwargs)
        self.row = 0
        self.column = 0
        self.padx = 5
        self.pady = 5

    def add(self, widget, row=None, column=None, padx=None, pady=None, bg=None):
        if row is not None:
            self.row = row
        if column is not None:
            self.column = column
        if padx is not None:
            self.padx = padx
        if pady is not None:
            self.pady = pady
        if bg is not None:
            self.bg = bg
        widget['bg'] = self.bg
        widget.grid(row=self.row, column=self.column, padx=self.padx, pady=self.pady)
        return self

root = tk.Tk()
root.configure(bg='black')
grid = Grid(root, bg='black')
grid.pack()
grid.add(tk.Label(grid, text='Red', fg='blue'), row=0, column=0) \
    .add(tk.Label(grid, text='Blue', fg='green'), row=1) \
    .add(tk.Label(grid, text='Green', fg='red'), row=2) \
    .add(tk.Entry(grid, width=5), row=0, column=1, bg='yellow') \
    .add(tk.Entry(grid, width=5), row=1) \
    .add(tk.Entry(grid, width=5), row=2)

root.mainloop()
You could also repack everything after you create the widgets.
import tkinter as tk

root = tk.Tk()
root.configure(bg='black')

tk.Label(root, text='Red', fg='blue').grid(row=0, column=0)
tk.Label(root, text='Blue', fg='green').grid(row=1, column=0)
tk.Label(root, text='Green', fg='red').grid(row=2, column=0)
tk.Entry(root, width=5).grid(row=0, column=1)
tk.Entry(root, width=5).grid(row=1, column=1)
tk.Entry(root, width=5).grid(row=2, column=1)

for child in root.winfo_children():
    child.grid_configure(padx=5, pady=5)
    if isinstance(child, tk.Label):
        child['bg'] = 'black'
    elif isinstance(child, tk.Entry):
        child['bg'] = 'yellow'

root.mainloop()
Or you could set your own grid method.
import tkinter as tk

print(tk.Label.__bases__)
print(tk.Entry.__bases__)

root = tk.Tk()

def mygrid(self, row, column, padx=10, pady=5):
    self.grid(row=row, column=column, padx=padx, pady=pady)

setattr(tk.Widget, 'mygrid', mygrid)

tk.Label(root, text='Red', fg='blue').mygrid(row=0, column=0)
tk.Label(root, text='Blue', fg='green').mygrid(row=1, column=0)
tk.Label(root, text='Green', fg='red').mygrid(row=2, column=0)
tk.Entry(root, width=5).mygrid(row=0, column=1)
tk.Entry(root, width=5).mygrid(row=1, column=1)
tk.Entry(root, width=5).mygrid(row=2, column=1)

root.mainloop()
What I am trying to show is that if you have a regular pattern that you use when writing code, you can create tools that support that pattern. I don't write much tkinter code, and I think the tkinter object model is rather clunky, but it didn't take me long to come up with half a dozen ways to speed up how I could write tkinter code. You should review your code, look at your work patterns and think about what you could make that would speed up what you do.
Reply


Messages In This Thread
Settting Default Values for grid - by gw1500se - Nov-09-2021, 09:27 PM
RE: Settting Default Values for grid - by gw1500se - Nov-10-2021, 03:08 PM
RE: Settting Default Values for grid - by gw1500se - Nov-10-2021, 06:16 PM
RE: Settting Default Values for grid - by deanhystad - Nov-10-2021, 08:26 PM
RE: Settting Default Values for grid - by gw1500se - Nov-11-2021, 06:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Default Values for radiobuttons xuraax 2 4,096 May-17-2020, 06:43 PM
Last Post: xuraax

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020