这是我的窗口:

enter image description here

我想将标签中的值尽可能放在窗口中.我想把它们放在这一排的左右两端.

以下是我正在做的不起作用的事情:

# 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)

总而言之,我只是将每行的值标签和实际值分别粘贴到West和East.但是,我无法让父框架(即out_frame)跨越窗口的整个列空间.

推荐答案

您需要:

  • self.columnconfigure(0, weight=1)以扩展self.out_frame以水平填充可用空间
  • self.out_frame.columnconfigure(1, weight=1)可展开包含数字的列,以水平填充可用空间

更新代码:

# 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)

结果是:

enter image description here

Python相关问答推荐

如何从格式为note:{neighbor:weight}的字典中构建networkx图?

如何知道标志是否由用户传递或具有默认值?

过滤绕轴旋转的螺旋桨

使用matplotlib pcolormesh,如何停止从一行绘制的磁贴连接到上下行?

Python panda拆分列保持连续多行

试图找到Python方法来部分填充numpy数组

难以在Manim中正确定位对象

Python上的Instagram API:缺少client_id参数"

删除任何仅包含字符(或不包含其他数字值的邮政编码)的观察

pandas滚动和窗口中有效观察的最大数量

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

如何在WSL2中更新Python到最新版本(3.12.2)?

Pre—Commit MyPy无法禁用非错误消息

Stacked bar chart from billrame

如何从需要点击/切换的网页中提取表格?

dask无groupby(ddf. agg([min,max])?''''

在用于Python的Bokeh包中设置按钮的样式

语法错误:文档. evaluate:表达式不是合法表达式

如何强制向量中的特定元素在Gekko中处于优化解决方案中