【问题标题】:ttk .grid(sticky = 'ew') with .grid_columnconfigure doesn't work properly for notebook entries带有 .grid_columnconfigure 的 ttk .grid(sticky = 'ew') 不适用于笔记本条目
【发布时间】:2016-06-17 13:12:29
【问题描述】:

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

目前帮助是Tkinter.Toplevel 并通过Notebooks 构成,其中包括ttk.Framesttk.LabelFramesttk.Labels。我的问题是,不同笔记本的框架和带有标签的标签框架不能正确粘贴。例如,如果一个条目的长度为 50 个字符,而所有其他条目的长度为 20 个字符,则整个窗口将拉伸到 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 似乎也没有区别。

【问题讨论】:

  • 您所说的 2 个笔记本条目配置为保存在同一个位置(行=0,列=0)。
  • 什么是align_text,它对于重现问题是否重要?另外,重现问题是否需要取胜主题?
  • 目前还不清楚是什么问题。这一切似乎都按我预期的方式工作,但显然你期待的是不同的东西。您提到使用Toplevel,但您的代码中没有Toplevel。问题仅仅是笔记本没有填满窗口吗?

标签: python python-2.7 tkinter sticky ttk


【解决方案1】:

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

列和行权重的作用

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

配置父行和列的权重

您的第一个问题似乎是您没有对整个窗口中的任何列赋予任何权重(即:top_notebook 的父级)。如果您希望top_notebook 在父级中获得额外空间(随窗口增长和缩小),则需要为其父级的行和列赋予权重。

例如:

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

由于top_notebookself.my_parent 中,如果您希望笔记本能够填充父级,则必须提供self.my_parent 列的权重。如果你想让笔记本填满分配给它的所有额外空间,你必须给它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)

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

不要严格依赖网格

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

例如,您的每个“顶部”标签只有一个子标签,即另一个笔记本。这是使用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 列赋予权重,添加四行额外代码。

【讨论】:

  • 这真的是我的问题。我没有将权重与小部件的父级关联,因此它们没有按照我想要的方式扩展。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2018-12-31
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多