ttk .grid(sticky = 'ew') with .grid_columnconfigure 不能正常用于笔记本条目

ttk .grid(sticky = 'ew') with .grid_columnconfigure doesn't work properly for notebook entries

我在 Python 2.7.11 中使用 Tkinterttk 创建一个 GUI,它基本上是数据集的转换器。这部分工作没有问题。 window 我正在编程的帮助让我很头疼。

目前帮助是 Tkinter.Toplevel 并通过 Notebooks 构建,其中包括 ttk.Framesttk.LabelFramesttk.Labels。我的问题是,不同笔记本的框架和内部带有标签的标签框不能正确粘贴。例如,如果一个条目的长度为 50 个字符,而所有其他条目的长度均为 20 个字符,则整个 window 将扩展到 50 个字符(这很好),但包含这 20 个字符的框架和标签将不会传播,如果它们不在同一个笔记本条目中。

老实说,我不知道,如果我使用 weight 错误,或者我是否忽略了一些其他细节。

你可以在这里看到一些没有标签框的示例代码(没有标签框来缩短示例代码):

 # -*- coding: utf-8 -*-

import Tkinter
import ttk
import align_text

class NotebookApp:

# Initiate modules
def __init__(self, parent):
    self.my_parent = parent
    self.globalStyle()
    self.myNotebookEntries()
    self.myNotebook()
    self.closeButton()

# Set global style:
def globalStyle(self):
    # Style:
    style_global = ttk.Style()
    style_global.theme_use('winnative')

# Create and define the notebook:
def myNotebook(self):
    bd_width = 2
    relief = 'ridge'
    # Top Notebook:
    top_notebook = ttk.Notebook(self.my_parent)
    top_notebook.grid(column = 0, row = 0)

    # Tabs of top Notebook:
    first_tab_of_top = ttk.Frame(top_notebook)
    second_tab_of_top = ttk.Frame(top_notebook)

    # Notebook of tab one:
    nb_one_of_top = ttk.Notebook(first_tab_of_top)
    nb_one_of_top.grid(column = 0, row = 0)

    # Notebook of tab two:
    nb_two_of_top = ttk.Notebook(second_tab_of_top)
    nb_two_of_top.grid(column = 0, row = 0)

    # Tabs of the Notebook of tab one:
    first_tab_of_nb_one = ttk.Frame(nb_one_of_top)
    first_tab_of_nb_one.grid(column = 0, row = 0, sticky = 'ew')
    first_tab_of_nb_one.grid_columnconfigure(0, weight = 1)
    second_tab_of_nb_one = ttk.Frame(nb_one_of_top)
    second_tab_of_nb_one.grid(column = 0, row = 0, sticky = 'ew')
    second_tab_of_nb_one.grid_columnconfigure(0, weight = 1)

    # Tabs of the Notebook of tab two:
    first_tab_of_nb_two = ttk.Frame(nb_two_of_top)
    first_tab_of_nb_two.grid(column = 0, row = 0, sticky = 'ew')
    first_tab_of_nb_two.grid_columnconfigure(0, weight = 1)
    second_tab_of_nb_two = ttk.Frame(nb_two_of_top)
    second_tab_of_nb_two.grid(sticky = 'ew')
    second_tab_of_nb_two.grid_columnconfigure(0, weight = 1)

    # Adding the Tabs (Frames) of the top Notebook:
    top_notebook.add(first_tab_of_top, text = self.first_tab_of_top_txt)
    top_notebook.add(second_tab_of_top, text = self.second_tab_of_top_txt)

    # Adding the Tabs (Frames) of the Notebook of tab one of the top Notebook:
    nb_one_of_top.add(first_tab_of_nb_one, text = self.first_tab_of_nb_one_txt)
    nb_one_of_top.add(second_tab_of_nb_one, text = self.second_tab_of_nb_one_txt)

    # Adding the Tabs (Frames) of the Notebook of tab two of the top Notebook:
    nb_two_of_top.add(first_tab_of_nb_two, text = self.first_tab_of_nb_two_txt)
    nb_two_of_top.add(second_tab_of_nb_two, text = self.second_tab_of_nb_two_txt)

    # Add Labels to the Tabs (Frames) of the Notebook of tab one of the top Notebook:
    label_one_of_nb_one_of_top = ttk.Label(first_tab_of_nb_one, 
                                text = '\n'.join(align_text.align_paragraph(
                                self.label_one_of_nb_one_of_top_txt, 
                                width = 50, 
                                debug = 0)),
                                borderwidth = bd_width,
                                relief = relief)
    label_one_of_nb_one_of_top.grid(column = 0, row = 0, sticky = 'nsew')
    label_one_of_nb_one_of_top.grid_columnconfigure(0, weight = 1)

    label_two_of_nb_one_of_top = ttk.Label(second_tab_of_nb_one, 
                                text = self.label_two_of_nb_one_of_top_txt,
                                borderwidth = bd_width,
                                relief = relief)
    label_two_of_nb_one_of_top.grid(column = 0, row = 0, sticky = 'nsew')
    label_two_of_nb_one_of_top.grid_columnconfigure(0, weight = 1)

    # Add Labels to the Tabs (Frames) of the Notebook of tab two of the top Notebook:
    label_one_of_nb_two_of_top = ttk.Label(first_tab_of_nb_two, 
                                text = self.label_one_of_nb_two_of_top_txt,
                                borderwidth = bd_width,
                                relief = relief)
    label_one_of_nb_two_of_top.grid(column = 0, row = 0, sticky = 'nsew')
    label_one_of_nb_two_of_top.grid_columnconfigure(0, weight = 1)

    label_two_of_nb_two_of_top = ttk.Label(second_tab_of_nb_two, 
                                text = self.label_two_of_nb_two_of_top_txt,
                                borderwidth = bd_width,
                                relief = relief)
    label_two_of_nb_two_of_top.grid(column = 0, row = 0, sticky = 'nsew')
    label_two_of_nb_two_of_top.grid_columnconfigure(0, weight = 1)

# All notebookentries: 
def myNotebookEntries(self):
    self.first_tab_of_top_txt = "First Notebook"
    self.second_tab_of_top_txt = "Second Notebook"

    self.first_tab_of_nb_one_txt = "First Tab"
    self.second_tab_of_nb_one_txt = "Second Tab"

    self.first_tab_of_nb_two_txt = "First Tab"
    self.second_tab_of_nb_two_txt = "Second Tab"

    self.label_one_of_nb_one_of_top_txt = ("This is the text of the first " 
                                        "label of the first notebook."
                                        "It is somewhat longer, than the "
                                        "second one.")
    self.label_two_of_nb_one_of_top_txt = "Text of Label two of notebook one."

    self.label_one_of_nb_two_of_top_txt = "Text of Label one of notebbok two."
    self.label_two_of_nb_two_of_top_txt = "Text of Label two of notebbok two."

# Button to close the notebook:
def closeButton(self):
    close_button = ttk.Button(self.my_parent, text = "Close App", 
                              command = self.closeButtonClick)
    close_button.bind('<Return>', self.wrapperCloseButton)
    close_button.grid(column = 0, row = 1)

# Command, used by the close button:                             
def closeButtonClick(self):
    self.my_parent.destroy()

def wrapperCloseButton(self, event):
    self.closeButtonClick()

# Main:
if __name__ == '__main__':
    root = Tkinter.Tk()
    root.title("Simple Notebbok")
    notebook_app = NotebookApp(root)
    root.mainloop() 

我也读到过两个看似相似的问题,但与框架和按钮的分布有关。在这两种情况下,正确使用 weight 有助于实现每个程序员的目标。因为我正在为我的转换器的用户编写帮助,所以有多行文本,由 Denis Barmenkov 的食谱 414870 的 align_paragraph 函数分隔。但是,对于 sticky 属性,我是否使用此功能来自动调整文本大小似乎没有区别。使用 grid_columnconfigurecolumnconfigure.

似乎也没有区别

您似乎误解了列权重的作用、如何使用它们,以及 sticky 属性如何与列交互。

列和行权重的作用

列的权重 会影响多余 space 分配给列的方式。如果一列有权重,它会被赋予一些(或全部)额外的space。此外,权重仅影响 interior 小部件,而不影响小部件本身。例如,foo.grid_columnconfigure(weight=1) 仅影响放置在 内部的小部件 foo,而不会影响 foo 在其父级内部的分配方式 space .

配置父行列的权重

您的第一个问题似乎是您没有给 window 中的任何列作为一个整体(即:top_notebook 的父列)赋予任何权重。如果你想让 top_notebook 在父级中被赋予额外的 space (随着 window 增长和缩小),你需要给它的父级的行和列赋予权重。

例如:

self.my_parent.grid_columnconfigure(0, weight=1)
self.my_parent.grid_rowconfigure(0, weight=1)

由于 top_notebookself.my_parent 中,如果您希望笔记本能够填充父级,则必须赋予 self.my_parent 列权重。如果你想让笔记本填满分配给它的所有额外space,你必须给它sticky属性:

top_notebook.grid(column = 0, row = 0, sticky="nsew")

不要为没有子项的小部件赋予权重

你对这几行代码还有一点误解:

label_one_of_nb_one_of_top.grid_columnconfigure(0, weight = 1)
...
label_one_of_nb_two_of_top.grid_columnconfigure(0, weight = 1)
...
label_one_of_nb_two_of_top.grid_columnconfigure(0, weight = 1)
...
label_two_of_nb_two_of_top.grid_columnconfigure(0, weight = 1)

您正在做的是告诉网格,标签内的任何额外space都应该分配给标签第 0 列中的小部件。但是,标签内没有其他小部件,因此这绝对没有效果。请记住,grid_rowconfiguregrid_columnconfigure 仅影响子部件。

不要严格依赖网格

grid 很棒,但有时需要做更多的工作才能设置。如果您在另一个小部件中有一个小部件,并且您希望内部小部件填充其父级,pack 是一个更好的选择。如果您在单行(例如:工具栏)或单列(例如:工具栏/主工作区/状态栏从上到下排列)中排列小部件也很好,但这与您的代码并不特别相关。

例如,您的每个 "top" 选项卡只有一个子项,即另一个笔记本。这是使用 pack 的完美地方,因为您可以在一行代码中完成所有操作:

# Notebook of tab one:
nb_one_of_top = ttk.Notebook(first_tab_of_top)
nb_one_of_top.pack(fill="both", expand=True)

# Notebook of tab two:
nb_two_of_top = ttk.Notebook(second_tab_of_top)
nb_two_of_top.pack(fill="both", expand=True)

要正确使用网格,您必须为每个选项卡的第 0 行和第 0 列赋予权重,并添加四行额外代码。