Button Placement Using '.grid' via tkinter

Hello,

I am getting familiar with the tkinter package/library. In my test program, I would like to
test the three different placement options using either pack, place, or grid. Using either
the place or the pack options seem to work just fine. However, when I attempt to use the
grid placement option, nothing happens for various values (row/column values). Does
anyone have an idea?

class miscTkinter(tk.Tk):
    
    def __init__(self):

        super().__init__()

        # Create main window
        mnWind = tk.Tk
        mnWind.title(self,'Testing Window') # Window title
        mnWind.geometry(self, '290x150')    # Window size

        # Add button
        self.btn = tk.Button(self, text="Clear")
        self.label = tk.Label(self)

        # Button placement location - Options: place, pack, grid
        #self.btn.place(x=125, y=45)
        #self.btn.pack(fill=tk.BOTH, expand=0, padx=120, pady=50)
        self.btn.grid(row=2, column=3)
        
if __name__ == "__main__":
    app = miscTkinter()
    app.mainloop() 

You’re putting only one widget, a button, on the grid, and it does appear.

Also, you’re putting something only on row 2, column 3. There’s nothing in any other row or column, so they have zero size.

If I use the following line of code instead, then it appears to provide tangible results:

self.btn.grid(padx=110, pady=50)  

The row=valuex, column=valuey appear to be irrelevant. What gives?
They market the ‘grid’ placement manager, as just that, a grid like placement
of widgets. Thus, you should be able to place widgets via an x and y pair.
The grid being the main window.

Can someone please provide an example for the grid manager using the row and column variables that provides a potential/working placement of the button widget for the example given above.

It’s still only 1 widget, it’s just that it now has some padding.

As I said, empty rows and empty columns have zero size. If there’s only 1 row, the row number is meaningless.

OK, here’s an example:

import tkinter as tk

class miscTkinter(tk.Tk):
    def __init__(self):
        super().__init__()

        # Create main window
        mnWind = tk.Tk
        mnWind.title(self,'Testing Window') # Window title
        mnWind.geometry(self, '290x150')    # Window size

        self.buttons = []

        for i, key in enumerate('123456789'):
            button = tk.Button(self, text=key)
            button.grid(row=i // 3, column=i % 3)
            self.buttons.append(button)

if __name__ == "__main__":
    app = miscTkinter()
    app.mainloop()

That is very nice.
Can you show one line of code for a particular static placement. Why does it not work for one line?

I have looked online for other examples. They all generally appear to show examples where more than one widget is being created … never just one. Here is an example that I pulled from the web:

import tkinter
master=tkinter.Tk()
master.title("grid() method")
master.geometry("350x275")
button1=tkinter.Button(master, text="button1")
button1.grid(row=0,column=5)
button1=tkinter.Button(master, text="button2")
button1.grid(row=0,column=1)
master.mainloop()

No matter if I change the values of row and column, they never seem to move no further from
either row 1 or column 1 (starting from ‘0’). Apparently, ‘grid’ placement manager is not good for the placement of one button widget. It is only if you’re wanting on packing widgets closely together starting from left to right or top to bottom (diagonal as well).

You are right. It makes no sense. Don’t do it.

The only one that gives you free reign placement with ease anywhere on a window is the place geometry manager. The pack GM is a bid rigid … you can only generally place a widget with the following restrictions: (center, y) or (x, center).

The following is the preferred way using the ‘place’ GM:

        # Add button
        self.btn1 = tk.Button(self, text="Press 1")
        self.btn2 = tk.Button(self, text="Press 2")

        # Button placement
        self.btn1.place(x=120, y=30)
        self.btn2.place(x=120, y=70)