如何在Tkinter中将两个小部件在同一行中分散到窗口的两端?

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

How to spread two widgets in the same row to the extremes of the window in Tkinter?

问题

这是我的窗口:

如何在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:

如何在Tkinter中将两个小部件在同一行中分散到窗口的两端?

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)

以及结果图片:

如何在Tkinter中将两个小部件在同一行中分散到窗口的两端?

英文:

You need to:

  • add self.columnconfigure(0, weight=1) to expand self.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:

如何在Tkinter中将两个小部件在同一行中分散到窗口的两端?

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

发表评论

匿名网友

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

确定