如何在使用 .grid() 添加时使 LabelFrame 中的所有项目保持在一起?

huangapple go评论65阅读模式
英文:

How to keep all items in LabelFrame stuck together when added using .grid()

问题

I understand your issue. To keep the widgets aligned properly, you can use the sticky parameter within the .grid() method to specify the alignment of widgets within their grid cells. Here's the modified code for adding justification:

def add_just(action_row):
    just_label = Label(frame, text="Justification:")
    just_entry = Entry(frame, width=100)
    just_label.grid(column=3, row=action_row, sticky='w')
    just_entry.grid(column=4, row=action_row, columnspan=3, sticky='w')

This modification should make the "Justification:" label and its entry field stick to the left (west) within their respective grid cells and maintain the alignment you desire.

英文:

I have a tkinter window that shows identified gaps in a process, and allows users to press buttons to either assign them as actions or provide justification why they're not going to be actioned. Pressing the action button creates label "assigned to" and entry for it, and it creates label "due date" and entry for it - all in one row, in consecutive 4 columns using .grid(). The two entry fields are relatively small.

Pressing the justification button creates a "justification:" label and an entry field next to it. The justification field should be big, because it can be a whole sentence, not just initials or date.
When I click action for one gap, it created neatly packed widgets next to each other. But when I click justification for another gap, the widgets in the row above get spread out.
I would like for them to stick together to the left, like they are before adding any justifications.

My understanding is that adding justification that includes the columnspan=3 option means that any columns that it spans will be distributed evenly. I guess another way of phrasing what I want to achieve is to make the first two columns only as wide as their contents require them to be, and only the third column be stretchy as needed.

Thanks in advance for any help. Below is the code, and screenshots of what I get.

from tkinter import *


def add_action(row):
    actioner_label = Label(frame, text="Assigned to:")
    actioner_entry = Entry(frame)
    actioner_label.grid(column=3, row=row)
    actioner_entry.grid(column=4, row=row)
    date_label = Label(frame, text="Due date:")
    date_entry = Entry(frame)
    date_label.grid(column=5, row=row)
    date_entry.grid(column=6, row=row)


def add_just(action_row):
    just_label = Label(frame, text="Justification:")
    just_entry = Entry(frame, width=100)
    just_label.grid(column=3, row=action_row)
    just_entry.grid(column=4, row=action_row, columnspan=3)


root = Tk()

frame = LabelFrame(root)
frame.pack()
gaps = ["some gap 1", "some gap 2"]
gap_row = 0

for gap in gaps:
    gap_label = Label(frame, text=gap)
    gap_label.grid(column=0, row=gap_row)
    action_button = Button(frame, text="add action", command=lambda r=gap_row: add_action(r))
    action_button.grid(column=1, row=gap_row)
    just_button = Button(frame, text="justification", command=lambda r=gap_row: add_just(r))
    just_button.grid(column=2, row=gap_row)
    gap_row += 1

root.mainloop()

before clicking any buttons:
enter image description here

after clicking 'add action':
enter image description here

after clicking 'justification':
enter image description here

I tried using the sticky='w' option, didn't work. I tried justify option (but that obviously only justifies the contents of the label). I thought about fixing the widths of the labels and adding a blank label in 4th column, and making the justification label columnspan=4, but when I do that, and don't include any justifications, the "due date" entry just has a weird blank space to its right, which looks weird when there's no justifications:
enter image description here

答案1

得分: 1

你可以使用 sticky='we' 来使你的输入框左右粘附。一个问题是,对齐的输入框跨越了3列(4、5、6),由于你将其设为了100字符大,它将调整4、5、6列的大小。如果你想不均匀地调整这些列的大小,你可以使用 grid_columnconfigure 来设置 weight

from tkinter import *

def add_action(row):
    actioner_label = Label(frame, text="Assigned to:")
    actioner_entry = Entry(frame)
    actioner_label.grid(column=3, row=row)
    actioner_entry.grid(column=4, row=row, sticky='we')
    date_label = Label(frame, text="Due date:")
    date_entry = Entry(frame)
    date_label.grid(column=5, row=row)
    date_entry.grid(column=6, row=row, sticky='we')

def add_just(action_row):
    just_label = Label(frame, text="Justification:")
    just_entry = Entry(frame, width=100)
    just_label.grid(column=3, row=action_row)
    just_entry.grid(column=4, row=action_row, columnspan=3)
    frame.grid_columnconfigure(4, weight=4)
    frame.grid_columnconfigure(5, weight=1)
    frame.grid_columnconfigure(6, weight=4)

root = Tk()

frame = LabelFrame(root)
frame.pack()
gaps = ["some gap 1", "some gap 2"]
gap_row = 0

for gap in gaps:
    gap_label = Label(frame, text=gap)
    gap_label.grid(column=0, row=gap_row)
    action_button = Button(frame, text="add action", command=lambda r=gap_row: add_action(r))
    action_button.grid(column=1, row=gap_row)
    just_button = Button(frame, text="justification", command=lambda r=gap_row: add_just(r))
    just_button.grid(column=2, row=gap_row)
    gap_row += 1

root.mainloop()

输出:

如何在使用 .grid() 添加时使 LabelFrame 中的所有项目保持在一起?

英文:

You can use sticky='we' to make your entry fields stick left and right. One problem is that the justification entry spans over 3 columns (4, 5, 6) and since you make it 100 character large, it will resize columns 4, 5, 6. If you want to resize these columns unevenly, you can set weight with grid_columnconfigure:

from tkinter import *


def add_action(row):
    actioner_label = Label(frame, text="Assigned to:")
    actioner_entry = Entry(frame)
    actioner_label.grid(column=3, row=row)
    actioner_entry.grid(column=4, row=row, sticky='we')
    date_label = Label(frame, text="Due date:")
    date_entry = Entry(frame)
    date_label.grid(column=5, row=row)
    date_entry.grid(column=6, row=row, sticky='we')


def add_just(action_row):
    just_label = Label(frame, text="Justification:")
    just_entry = Entry(frame, width=100)
    just_label.grid(column=3, row=action_row)
    just_entry.grid(column=4, row=action_row, columnspan=3)
    frame.grid_columnconfigure(4, weight=4)
    frame.grid_columnconfigure(5, weight=1)
    frame.grid_columnconfigure(6, weight=4)


root = Tk()

frame = LabelFrame(root)
frame.pack()
gaps = ["some gap 1", "some gap 2"]
gap_row = 0

for gap in gaps:
    gap_label = Label(frame, text=gap)
    gap_label.grid(column=0, row=gap_row)
    action_button = Button(frame, text="add action", command=lambda r=gap_row: add_action(r))
    action_button.grid(column=1, row=gap_row)
    just_button = Button(frame, text="justification", command=lambda r=gap_row: add_just(r))
    just_button.grid(column=2, row=gap_row)
    gap_row += 1

root.mainloop()

Output:

如何在使用 .grid() 添加时使 LabelFrame 中的所有项目保持在一起?

答案2

得分: 0

更容易将小部件对齐的方法是将它们放在另一个框架中:

def add_action(row):
    frame2 = Frame(frame) # 为下面的小部件创建一个框架
    actioner_label = Label(frame2, text="Assigned to:")
    actioner_entry = Entry(frame2)
    actioner_label.grid(column=0, row=0)
    actioner_entry.grid(column=1, row=0)
    date_label = Label(frame2, text="Due date:")
    date_entry = Entry(frame2)
    date_label.grid(column=2, row=0)
    date_entry.grid(column=3, row=0)
    frame2.grid(row=row, column=3, sticky="w")


def add_just(action_row):
    frame2 = Frame(frame) # 为下面的小部件创建一个框架
    just_label = Label(frame2, text="Justification:")
    just_entry = Entry(frame2, width=100)
    just_label.grid(column=0, row=0)
    just_entry.grid(column=1, row=0)
    frame2.grid(row=action_row, column=3, sticky="w")

以及结果:

如何在使用 .grid() 添加时使 LabelFrame 中的所有项目保持在一起?

英文:

It is more easier to align widgets by putting them in another frame instead:

def add_action(row):
    frame2 = Frame(frame) # create a frame for the below widgets
    actioner_label = Label(frame2, text="Assigned to:")
    actioner_entry = Entry(frame2)
    actioner_label.grid(column=0, row=0)
    actioner_entry.grid(column=1, row=0)
    date_label = Label(frame2, text="Due date:")
    date_entry = Entry(frame2)
    date_label.grid(column=2, row=0)
    date_entry.grid(column=3, row=0)
    frame2.grid(row=row, column=3, sticky="w")


def add_just(action_row):
    frame2 = Frame(frame) # create a frame for the below widgets
    just_label = Label(frame2, text="Justification:")
    just_entry = Entry(frame2, width=100)
    just_label.grid(column=0, row=0)
    just_entry.grid(column=1, row=0)
    frame2.grid(row=action_row, column=3, sticky="w")

And the result:

如何在使用 .grid() 添加时使 LabelFrame 中的所有项目保持在一起?

答案3

得分: 0

@acw1668,

谢谢!这正是我想要的。事实证明,add_just 实际上不需要自己的框架来实现上述布局,可能是因为它足够宽,没有必要以这种或那种方式来对齐内容。我只需要在 add_actionframe2 中添加 columnspan=2

def add_action(row):
    frame2 = Frame(frame)
    actioner_label = Label(frame2, text="Assigned to:")
    actioner_entry = Entry(frame2)
    actioner_label.grid(column=0, row=row)
    actioner_entry.grid(column=1, row=row)
    date_label = Label(frame2, text="Due date:")
    date_entry = Entry(frame2)
    date_label.grid(column=2, row=row)
    date_entry.grid(column=3, row=row)
    frame2.grid(column=3, row=row, sticky='w', columnspan=2)

def add_just(action_row):
    just_label = Label(frame, text="Justification:")
    just_entry = Entry(frame, width=100)
    just_label.grid(column=3, row=action_row)
    just_entry.grid(column=4, row=action_row)

希望对你有所帮助。

英文:

@acw1668,

Thank you! This is exactly what I was after. It turns out that the add_just doesn't actually need a frame of its own to achieve the above layout, probably because it is wide enough that it doesn't have any scope for justifying contents this way or that. I just had to add columnspan=2 to frame2 in add_action.

def add_action(row):
    frame2 = Frame(frame)
    actioner_label = Label(frame2, text="Assigned to:")
    actioner_entry = Entry(frame2)
    actioner_label.grid(column=0, row=row)
    actioner_entry.grid(column=1, row=row)
    date_label = Label(frame2, text="Due date:")
    date_entry = Entry(frame2)
    date_label.grid(column=2, row=row)
    date_entry.grid(column=3, row=row)
    frame2.grid(column=3, row=row, sticky='w', columnspan=2)


def add_just(action_row):
    just_label = Label(frame, text="Justification:")
    just_entry = Entry(frame, width=100)
    just_label.grid(column=3, row=action_row)
    just_entry.grid(column=4, row=action_row)

huangapple
  • 本文由 发表于 2023年6月26日 15:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定