1
votes

I have a window with a listbox that resize with one another. I'm using grid for this. There are a row of buttons under the list box which are currently moving and resizing with the window. I'd like the buttons to remain in place and remain a consistent size while window and listbox resize (so the buttons always remain in the bottom left corner of the listbox).

Is there an easy way to do this? I just started using Tkinter today so I may have just missed it in the docs or might just be confused on grid layouts.

The buttons are set up as follows:

nextbutton = Button(self, width = 30, height = 30, image = ffbtn)
nextbutton.image = ffbtn
nextbutton.bind("<Button-1>", self.forward)
nextbutton.grid(row = 10, column=4, sticky='w')

I've tried all sticky combinations but the ones that don't resize the buttons still allow them to move around, which I don't want.

EDIT: Heres an very basic example of what I'm talking about, as requested. When the window is resized, the buttons move apart from each other. I don't want that.

On a side note, do I need all those grid configures at the end to set the weight even if I use its one column or row with column/rowspan taking up all the space?

import tkinter
from tkinter import *
from tkinter import ttk

class Application(tkinter.Tk):
    def __init__(self, parent):
        tkinter.Tk.__init__(self, parent)
        self.minsize(300,300)
        self.parent = parent
        self.main()

    def main(self):

        self.grid()

        listbox = tkinter.Listbox(self, width=20, height=25, bg = 'grey')
        listbox.grid(padx=30, pady=30, columnspan=11,  sticky='NEW')

        bt1 = Button(self, width = 5, height = 5)
        bt1.grid(row=10, column=0, padx=(30,0), sticky='w')

        bt2 = Button(self, width = 5, height = 5)
        bt2.grid(row = 10, column=1, sticky='w')

        bt3 = Button(self, width = 5, height = 5)
        bt3.grid(row = 10, column=2, sticky='w')

        bt4 = Button(self, width = 5, height = 5) 
        bt4.grid(row = 10, column=3, sticky='w')

        bt5 = Button(self, width = 5, height = 5)
        bt5.grid(row = 10, column=4, sticky='w')

        self.grid_columnconfigure(0,weight=1)
        self.grid_columnconfigure(1,weight=1)
        self.grid_columnconfigure(2,weight=1)
        self.grid_columnconfigure(3,weight=1)
        self.grid_columnconfigure(4,weight=1)
        self.grid_columnconfigure(5,weight=1)
        self.grid_columnconfigure(6,weight=1)
        self.grid_columnconfigure(7,weight=1)
        self.grid_columnconfigure(8,weight=1)
        self.grid_columnconfigure(9,weight=1)
        self.grid_columnconfigure(10,weight=1)      
        self.grid_rowconfigure(0,weight=1)
        self.grid_rowconfigure(1,weight=1)
        self.grid_rowconfigure(2,weight=1)
        self.grid_rowconfigure(3,weight=1)
        self.grid_rowconfigure(4,weight=1)
        self.grid_rowconfigure(5,weight=1)
        self.grid_rowconfigure(6,weight=1)
        self.grid_rowconfigure(7,weight=1)
        self.grid_rowconfigure(8,weight=1)
        self.grid_rowconfigure(9,weight=1)
        self.grid_rowconfigure(10,weight=1)

app = Application(None)
app.mainloop()
1
^Just added it to the original postmxvx

1 Answers

2
votes

By giving non-zero weights to all the rows and columns in the grid, you explicitly make them resize with the main window.

Since you only want the listbox to resize, give non-zero weights only to the row and one of the columns that contain the listbox:

def main(self):
    ...
    # The default weight of all rows/columns is 0. Only modify it for row 0 and column 10
    self.grid_rowconfigure(0, weight=1)
    self.grid_columnconfigure(10, weight=1)

For more info, check the documentations of the pack, grid and place methods.