.pack()使用Tkinter格式化问题

时间:2015-10-13 03:59:19

标签: python tkinter

我目前在显示列表框时遇到问题,列表框的宽度与其上方的按钮相同。

我的代码是:

import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog


class List(tk.Listbox):

    def __init__(self, master):
        super().__init__(master)


class button_list(object):

    def __init__(self, master):

        button = tk.Button(master, text="button", width=20)
        button.pack(side=tk.LEFT, anchor=tk.N)

        button2 = tk.Button(master, text="button2", width=20)
        button2.pack(side=tk.LEFT, anchor=tk.N)

        self.list = List(master)
        self.list.pack(side=tk.LEFT, fill=tk.Y)


def main():
    root = tk.Tk()
    app = button_list(root)
    root.geometry("800x400")
    root.mainloop()

if __name__ == '__main__':
    main()

目前按钮和列表显示如下:

enter image description here

如何在按钮下方显示列表(列表框),拉伸到按钮的宽度?我的印象是self.list.pack(side=tk.LEFT)会将它放在下面,但它似乎只是将列表移动到第二个按钮的边缘。

提前致谢

1 个答案:

答案 0 :(得分:2)

以下是我认为你想要它的GUI布局。

import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog


class List(tk.Listbox):
    def __init__(self, master):
        super().__init__(master)


class button_list(object):
    def __init__(self, master):
        # anchor=tk.W forces the frame west (left)
        # fill=tk.Y allows the widget to fill up the space in y direction (vertical)
        #     allows for the widgets to stretch
        # expand=True allows the widget to expand into avaliable space and "take it up"
        #    if fill is not an argument, the the space is still taken up, but the widget doesn't
        #    stretch into that space
        # if side is not specified in argument then side=tk.TOP by default

        # leftFrame holds the widgets which is anchored to the left
        leftFrame = tk.Frame(master)
        leftFrame.pack(side=tk.LEFT, anchor=tk.W, fill=tk.Y)

        # btnFrame holds the buttons and packs to top of leftFrame
        # this is what's creating the leftFrame width (much like divs in html if you've used it)
        btnFrame = tk.Frame(leftFrame)
        btnFrame.pack()

        button = tk.Button(btnFrame, text="button", width=20)
        button.pack(side=tk.LEFT) #anchor removed because it wasn't needed

        button2 = tk.Button(btnFrame, text="button2", width=20)
        button2.pack(side=tk.LEFT) # anchor removed because it wasn't needed

        self.list = List(leftFrame)
        self.list.pack(fill=tk.BOTH, expand=True) #changed fill=tk.BOTH, added expand=True

        # ==== Visual Layout ==== #
"""       leftFrame
       ______________________________
       |  btnFrame   |              |
       |_____________|              |
       |             |              |
       |  Listbox    |  Rest of     |
       |             |  window      |
       |             |              |
       |_____________|______________|
"""

def main():
    root = tk.Tk()
    app = button_list(root)
    root.geometry("800x400")
    root.mainloop()

if __name__ == '__main__':
    main()