简体   繁体   English

TKinter:框架和根之间失去对齐

[英]TKinter: lost alignment between frame and root

I am running into a curious problem. 我遇到一个奇怪的问题。 I have a small grid (8 rows, 5 columns). 我有一个小的网格(8行5列)。 Root with a couple of frames. 根与几个框架。 Most run fine. 大多数运行良好。 Four fields of the the top row (1 in my case, I do not use 0) have a text and must have the same color continuously. 最上面一行的四个字段(在我的情况下为1,我不使用0)具有文本,并且必须连续具有相同的颜色。 I could not realise this in root itself so I made a frame (text=""), with labels. 我根本无法意识到这一点,因此我制作了一个带有标签的框架(text =“”)。 That gives the desired look apart from the fact that all the texts are concatenated at the left and no longer above the gridfields below. 除了所有文本都在左侧并不再位于下面的网格字段之上的事实之外,这提供了所需的外观。 The code is realy very simple but I expect it will be requested anyway so here is the relevant part: 该代码确实非常简单,但是我希望无论如何都会要求它,所以这里是相关的部分:

# Top frame
top_frame=LabelFrame(root, text= "", bg=top_line_color)
top_frame.grid(row =1, column=0, sticky=W+E, columnspan=5)

# Fixed labels:
Label(top_frame, text="Anten", font=fnt, bg=top_line_color).grid(row=1, 
column=0)
Label(top_frame, text="Antenna", font=fnt, bg=top_line_color).grid(row=1, 
column=1)
Label(top_frame, text="Corr.", font=fnt, width=4, 
bg=top_line_color).grid(row=1, column=3)
Label(top_frame, text="Move", font=fnt, width=4, 
bg=top_line_color).grid(row=1, sticky=W, column=4)
Label(root, text="Azimuth:", justify=RIGHT, font=fnt).grid(row=2, sticky=W)
Label(root, text="Elevation:", justify=RIGHT, font=fnt).grid(row=3, 
sticky=W) 
Label(root, text="Location:",justify=RIGHT, font=fnt).grid(row=4, sticky=W)
Label(root, text=acemedat.myloc, font=fnt).grid(row=4, column=1)
Label(root, text="Dat/Tim:", justify=RIGHT, font=fnt).grid(row=4, column=2) 
Label(root, text="Moon distance (km):", font=fnt).grid(row=5, columnspan=2, 
sticky=W)

The labels are followed by data fields (in root as well). 标签后面是数据字段(也在根目录中)。 They comply with the grid without problem. 它们完全符合网格要求。 What can I do to to align the frame fields with the root fields? 如何使框架字段与根字段对齐? Many thanks in advance, Harke 提前非常感谢,Harke

If you want everything to line up in a grid, the easiest solution is to put them all in the same frame. 如果您希望所有内容都排列在一个网格中,最简单的解决方案是将它们全部放在同一帧中。 If you use the sticky attribute for the fixed labels, you can have a solid, unbroken color. 如果对固定标签使用sticky属性,则可以具有不间断的纯色。 Depending on your system, you might need to change the labels to have a borderwidth and/or highlightthickness of zero. 根据您的系统,您可能需要更改标签以borderwidth和/或highlightthickness borderwidth为零。 If you want the labels to be left-aligned, you can do that with the anchor attribute of the labels. 如果希望标签左对齐,则可以使用标签的anchor属性来实现。

For example: 例如:

from tkinter import *

root = Tk()

fnt = ("Helvetica", "12")
top_line_color = "bisque"

# Fixed labels:
Label(root, text="Anten", font=fnt, bg=top_line_color,
      anchor="w").grid(row=0, column=0, sticky="nsew")
Label(root, text="Antenna", font=fnt, bg=top_line_color,
      anchor="w").grid(row=0, column=1, sticky="nsew")
Label(root, text="Corr.", font=fnt, width=4, bg=top_line_color,
      anchor="w").grid(row=0, column=2, sticky="nsew")
Label(root, text="Move", font=fnt, width=4, bg=top_line_color,
      anchor="w").grid(row=0, column=3, sticky="nsew")

Label(root, text="Azimuth:", justify=RIGHT, font=fnt).grid(row=2, sticky=W)
Label(root, text="Elevation:", justify=RIGHT, font=fnt).grid(row=3,  sticky=W) 
Label(root, text="Location:",justify=RIGHT, font=fnt).grid(row=4, sticky=W)
Label(root, text="acemedat.myloc", font=fnt).grid(row=4, column=1)
Label(root, text="Dat/Tim:", justify=RIGHT, font=fnt).grid(row=4, column=2) 
Label(root, text="Moon distance (km):", font=fnt).grid(row=5, columnspan=2, sticky=W)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM