如何使用网格管理器使列表框填满整个框架?

时间:2014-07-15 01:20:30

标签: python listbox tkinter

考虑以下tk接口;

enter image description here

有两个框架,每个框架由网格几何体管理器管理。我尝试了几个粘性选项,但我无法让列表框看起来更长。我想让它跨越整排。我怎样才能实现它?这是我的代码;

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()

1 个答案:

答案 0 :(得分:0)

您必须使用rowconfigure(0, weight=1) - 而不是Listbox,但使用其父级来管理此几何图形(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