英文:
How to spread two widgets in the same row to the extremes of the window in Tkinter?
问题
这是我的窗口:
我想要尽可能远离窗口的标签值。我想将它们放在行的左右两端。
这是我正在尝试但不起作用的方式:
# 输出框架
self.out_frame = tk.Frame(self)
self.out_frame.grid(row=1, column=0, columnspan=2, sticky="ew", pady=24)
# ...
# 以十六进制值为例->
ttk.Label(self.out_frame, text="十六进制", font=sub_title_font).grid(row=0, column=0, sticky="w")
self.hex_out.set(str(hexnum))
ttk.Label(self.out_frame, textvariable=self.hex_out, font=sub_title_font).grid(row=0, column=1, sticky="e")
ttk.Separator(self.out_frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky="ew", pady=5)
总结一下,我只是将值标签和实际值分别粘贴到每一行的西侧和东侧。但是,我无法使父框架(即 out_frame
)跨足窗口的整个列空间。
英文:
Here is my window:
I want to place the values from the labels as far as possible in the window. I want to place them in the left and right extremes of the row.
Here is what I am doing that is not working:
# output frame
self.out_frame = tk.Frame(self)
self.out_frame.grid(row=1, column=0, columnspan=2, sticky="ew", pady=24)
# ...
# take the hexadecimal value for example ->
ttk.Label(self.out_frame, text="Hexadecimal", font=sub_title_font).grid(row=0, column=0, sticky="w")
self.hex_out.set(str(hexnum))
ttk.Label(self.out_frame, textvariable=self.hex_out, font=sub_title_font).grid(row=0, column=1, sticky="e")
ttk.Separator(self.out_frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky="ew", pady=5)
To sum up, I am just sticking the value label and the actual value to West and East respectively for each row. However, I cannot get the parent frame (i.e., out_frame
) to span the entire column space of the window.
答案1
得分: 2
以下是翻译好的部分:
- 将
self.columnconfigure(0, weight=1)
添加到代码中,以使self.out_frame
水平填充可用空间。 - 将
self.out_frame.columnconfigure(1, weight=1)
添加到代码中,以使包含数字的列水平填充可用空间。
更新后的代码:
# 分配所有可用的水平空间给父容器中的第 0 列
self.columnconfigure(0, weight=1)
# 输出框架
self.out_frame = tk.Frame(self)
self.out_frame.grid(row=1, column=0, columnspan=2, sticky="ew", pady=24)
# 分配所有可用的水平空间给 self.out_frame 中的第 1 列
self.out_frame.columnconfigure(1, weight=1)
# ...
# 以十六进制值为例 ->
ttk.Label(self.out_frame, text="十六进制", font=sub_title_font).grid(row=0, column=0, sticky="w")
self.hex_out.set(str(hexnum))
ttk.Label(self.out_frame, textvariable=self.hex_out, font=sub_title_font).grid(row=0, column=1, sticky="e")
ttk.Separator(self.out_frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky="ew", pady=5)
以及结果图片:
英文:
You need to:
- add
self.columnconfigure(0, weight=1)
to expandself.out_frame
to fill the available space horizontally - add
self.out_frame.columnconfigure(1, weight=1)
to expand the column containing the number to fill the available space horizontally
Updated code:
# allocate all the available space horizontally to column 0 inside parent
self.columnconfigure(0, weight=1)
# output frame
self.out_frame = tk.Frame(self)
self.out_frame.grid(row=1, column=0, columnspan=2, sticky="ew", pady=24)
# allocate all the available space horizontally to column 1 inside self.out_frame
self.out_frame.columnconfigure(1, weight=1)
# ...
# take the hexadecimal value for example ->
ttk.Label(self.out_frame, text="Hexadecimal", font=sub_title_font).grid(row=0, column=0, sticky="w")
self.hex_out.set(str(hexnum))
ttk.Label(self.out_frame, textvariable=self.hex_out, font=sub_title_font).grid(row=0, column=1, sticky="e")
ttk.Separator(self.out_frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky="ew", pady=5)
And the result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论