yasar
yasar

Reputation: 13738

How to make listbox fill whole frame using grid manager?

Consider the following tk interface;

enter image description here

There are two frames, each in its own column managed by grid geometry manager. I tried several sticky options, but I couldn't make the listbox look longer. I want it to span acroos the whole row. How can I achieve it? Here are my codes;

import tkinter as tk

class MinimalTestCase(tk.Frame):

    def __init__(self, master, *args, **kwargs):

        tk.Frame.__init__(self, master)

        self.f1 = tk.Frame(self)
        self.f2 = tk.Frame(self)

        self.f1.grid(row=0, column=0)
        self.f2.grid(row=0, column=1, sticky=(tk.N, tk.S, tk.E, tk.W))

        ### Fill left frame with dummy elements to demonstrate the problem
        for i in range(15):
            tk.Label(self.f1, text="Label{}".format(i)).grid(row=i)

        ### Put listbox on right frame
        self.lbox = tk.Listbox(self.f2)
        self.lbox.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))

        self.grid()

if __name__ == "__main__":

    root=tk.Tk()
    MinimalTestCase(root)
    root.mainloop()

Upvotes: 0

Views: 4480

Answers (1)

furas
furas

Reputation: 142651

You have to use rowconfigure(0, weight=1) - not with Listbox but with its parent which manage this geometry (Frame).

import Tkinter as tk

class MinimalTestCase(tk.Frame):

    def __init__(self, master, *args, **kwargs):

        tk.Frame.__init__(self, master)

        self.f1 = tk.Frame(self)
        self.f2 = tk.Frame(self)
        self.f2.rowconfigure(0, weight=1) # <-- row 0 will be resized

        self.f1.grid(row=0, column=0)
        self.f2.grid(row=0, column=1, sticky=(tk.N, tk.S, tk.E, tk.W))

        ### Fill left frame with dummy elements to demonstrate the problem
        for i in range(15):
            tk.Label(self.f1, text="Label{}".format(i)).grid(row=i)

        ### Put listbox on right frame
        self.lbox = tk.Listbox(self.f2)
        self.lbox.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))

        self.grid()

if __name__ == "__main__":

    root=tk.Tk()
    MinimalTestCase(root)
    root.mainloop()

Tkinterbook: The Tkinter Grid Geometry Manager

Upvotes: 1

Related Questions