Button列之间的Python大空间

时间:2018-01-31 19:40:53

标签: python tkinter grid

我想将我的Tk窗口中的按钮设置为彼此靠近,但它们之间的空间太大。他们实际上必须非常接近,但我无法改变他们的位置。

代码:

from tkinter import *
from tkinter import ttk

def Page():

    Profile = Tk()

    TreeSearchbar = Text(Profile, width=30, height=1, font="Arial 15")
    TreeSearchbar.grid(row=0, column=0, sticky=W, pady=5, columnspan=2)

    TreeSearchButton = ttk.Button(Profile, text="Search").grid(row=0, column=1, sticky=W, columnspan=2)


    Tree = ttk.Treeview(Profile, height=20)
    Tree["columns"] = ("value1", "value2", "value3", "value4", "value5", "value6")
    Tree.column("#0", width=170)
    Tree.column("#1", width=180)
    Tree.column("#2", width=0)
    Tree.heading("#0", text="1")
    Tree.heading("#1", text="2")
    Tree.grid(row=1, column=0, sticky="nw", columnspan=2)

    Button1 = ttk.Button(Profile, text="Delete").grid(row=2, column=0, sticky=W, pady=12, padx=(20, 0))
    Button2 = ttk.Button(Profile, text="Update").grid(row=2, column=1, sticky=W)
    Button3 = ttk.Button(Profile, text="Edit").grid(row=2, column=2, sticky=W)

    Profile.mainloop()

Page()

1 个答案:

答案 0 :(得分:1)

使用网格单元格内的框架,您将能够打包控件。这将为您提供更大的布局灵活性,如下所示:

from tkinter import *
from tkinter import ttk

def Page():

    Profile = Tk()

    TreeSearchbar = Text(Profile, width=30, height=1, font="Arial 15")
    TreeSearchbar.grid(row=0, column=0, sticky=W, pady=5, columnspan=2)

    TreeSearchButton = ttk.Button(Profile, text="Search").grid(row=0, column=1, sticky=W, columnspan=2)


    Tree = ttk.Treeview(Profile, height=20)
    Tree["columns"] = ("value1", "value2", "value3", "value4", "value5", "value6")
    Tree.column("#0", width=170)
    Tree.column("#1", width=180)
    Tree.column("#2", width=0)
    Tree.heading("#0", text="1")
    Tree.heading("#1", text="2")
    Tree.grid(row=1, column=0, sticky="nw", columnspan=2)

    ButtonFrame = ttk.Frame(Profile)

    Button1 = ttk.Button(ButtonFrame,
                         text="Delete").pack(side=LEFT, padx=5, pady=5)
    Button2 = ttk.Button(ButtonFrame,
                         text="Update").pack(side=LEFT, padx=5, pady=5)
    Button3 = ttk.Button(ButtonFrame,
                         text="Edit").pack(side=LEFT, padx=5, pady=5)

    ButtonFrame.grid(row=2, column=0)

    Profile.mainloop()

Page()

你应该在UI中对你的区域进行网格划分,并且可能在每个区域中应用这个原则,再次使用网格或打包器(虽然你可以使用通常不推荐的放置器,但是可以调整到窗口大小...)