tkinter布局1像素输出为什么?

时间:2017-07-19 08:41:25

标签: tkinter

Screenshot

希望你能提供帮助。

我无法理解为什么我的代码会在某些地方产生一个像素间隙的框架中生成标签和输入框?它根本不应该有差距吗?

代码:

定义框架元素:

def create_frame(self):
    self.frame = Frame(self.root)
    self.frame.pack(side=TOP, anchor="w")

第一个数据输入功能:

def data_entry_01(self, frame):

    L1 = Label(frame, text="STATE 01", anchor="w", width=23, bg=white)
    L1.grid(row=0, column=0, columnspan=3)

    # ------------------------------------------------------------------

    L2 = Label(frame, text="Dry bulb:", anchor="w", width=10, bg=white)
    L2.grid(row=1, column=0)

    E1 = Entry(frame, width=8, state=NORMAL)
    E1.grid(row=1, column=1)

    L3 = Label(frame, text="°C", anchor="w", width=5, bg=white)
    L3.grid(row=1, column=2)

    L5 = Label(frame, text="Humidity:", anchor="w", width=10, bg=white)
    L5.grid(row=2, column=0)

    E2 = Entry(frame, width=8, state=NORMAL)
    E2.grid(row=2, column=1)

    L6 = Label(frame, text="%", anchor="w", width=5, bg=white)
    L6.grid(row=2, column=2)

第二个数据输入功能:

def data_entry_02(self, frame):

    L1 = Label(frame, text="STATE 02", anchor="w", width=23, bg=white)
    L1.grid(row=0, column=3, columnspan=3)

    # ------------------------------------------------------------------

    L2 = Label(frame, text="Dry bulb:", anchor="w", width=10, bg=white)
    L2.grid(row=1, column=3)

    E1 = Entry(frame, width=8, state=NORMAL)
    E1.grid(row=1, column=4)

    L3 = Label(frame, text="°C", anchor="w", width=5, bg=white)
    L3.grid(row=1, column=5)

    L5 = Label(frame, text="Humidity:", anchor="w", width=10, bg=white)
    L5.grid(row=2, column=3)

    E2 = Entry(frame, width=8, state=NORMAL)
    E2.grid(row=2, column=4)

    L6 = Label(frame, text="%", anchor="w", width=5, bg=white)
    L6.grid(row=2, column=5)

所有行宽总计为23,那么为什么会出现间隙?

1 个答案:

答案 0 :(得分:2)

为了使用grid几何管理器填充每个项目周围的“空格”,您可以使用sticky参数。

在这种情况下,您可以在每个项目的选项中添加sticky=N+E+S+W,让它们填充网格中每个空间的整体。这将消除您在每个项目周围看到的差距。