简体   繁体   English

网格中的 Tkinter label 不必要地扩展

[英]Tkinter label in grid expands unnessarily

I have a label in a tkinter grid layout which expands when I set its content, even though the label's size is more than sufficient to cover the string.我在 tkinter 网格布局中有一个 label,当我设置它的内容时它会扩展,即使标签的大小足以覆盖字符串。

Label expands with more than enough empty space Label 展开后有足够的空间在此处输入图像描述

I am setting the content of the label via its attached textvariable.我通过其附加的文本变量设置 label 的内容。 As it's a filepath, which is too long, I am truncating the string to a length of 50 characters.因为它是一个太长的文件路径,所以我将字符串截断为 50 个字符的长度。 When I set the stringVar, the label (and its parent frame) expands.当我设置 stringVar 时,label(及其父框架)展开。 That causes the list element on the left to be squashed.这会导致左侧的列表元素被压扁。

The label is set in a grid layout whose column is configured to expand. label 设置为网格布局,其列配置为展开。 (This is mainly done so that the white label covers the area when no content is set.) However, there is no need for the label to expand, since there is enough space in the label to fit the entire string without expanding. (这样做主要是为了在没有设置内容的情况下白色label覆盖该区域。)但是label不需要扩展,因为label中有足够的空间可以容纳整个字符串而不需要扩展。 The text covers only ~50% of the label.文本仅涵盖 label 的约 50%。

What is causing this unnecessary expansion of the label, and how can I prevent this?是什么导致了 label 的这种不必要的扩展,我该如何防止这种情况发生?

Creating the panel:创建面板:

 def _buildUI(self):
    self._filesPanel = tk.LabelFrame(self._parentFrame, text="Selected file")
    self._filesPanel.grid_columnconfigure(0, weight=0)
    self._filesPanel.grid_columnconfigure(1, weight=1)
    self._filesPanel.grid(row=0, column=0, sticky="news")

    fileNameLabel = tk.Label(self._filesPanel, text="Image file:")
    maskFileNameLabel = tk.Label(self._filesPanel, text="Mask file:")

    self._imageFilePathVar = tk.StringVar()
    self._maskFilePathVar = tk.StringVar()
    self._fileNameLabelSelection = tk.Label(self._filesPanel, textvariable=self._imageFilePathVar, bg="white")
    self._maskFileLabelSelection = tk.Label(self._filesPanel, textvariable=self._maskFilePathVar, bg="white")

    fileNameLabel.grid(row=1, column=0, padx=(5, 5), pady=(2, 2))
    maskFileNameLabel.grid(row=2, column=0, padx=(5, 5), pady=(2, 2))
    self._fileNameLabelSelection.grid(row=1, column=1, sticky="news", padx=(5, 5), pady=(2, 2))
    self._maskFileLabelSelection.grid(row=2, column=1, sticky="news", padx=(5, 5), pady=(2, 2))

Setting the label content:设置label内容:

def loadFilePaths(self, imagePath:str, maskPath:str):
    imageFilePathToDisplay = self._trimStringToElementLength(imagePath)
    self._imageFilePathVar.set(imageFilePathToDisplay)
    maskFilePathToDisplay = self._trimStringToElementLength(maskPath)
    self._maskFilePathVar.set(maskFilePathToDisplay)

def _trimStringToElementLength(self, text: str, maxLength: int = 50) -> str:
    if not text:
        return "N/A"
    if len(text) > maxLength:
        return f"...{text[-maxLength:]}"
    return text

Configuraiton of the parent frames父框架的配置

    self._parentFrame.grid_columnconfigure(0, weight=2)  <--- the weight for the list on the left
    self._parentFrame.grid_columnconfigure(1, weight=5)  <--- the weight for the panel on the right
    self._parentFrame.rowconfigure(0, weight=1)
    self._imageList.getFrame().grid(row=0, column=0, sticky="news", padx=(2, 2))
    self._overlayPanel.grid(row=0, column=1, sticky="news", padx=(2, 2))

@Bryan Oakley's comment was the solution. @Bryan Oakley 的评论是解决方案。 Setting any width for the label prevents the auto extension.为 label 设置任何宽度都会阻止自动扩展。 Replace the lines in the _buildUI() function with将 _buildUI() function 中的行替换为

    self._fileNameLabelSelection = tk.Label(self._filesPanel, textvariable=self._imageFilePathVar,
                                            bg="white", width=10)
    self._maskFileLabelSelection = tk.Label(self._filesPanel, textvariable=self._maskFilePathVar,
                                            bg="white", width=10)

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

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