【发布时间】:2016-06-17 13:12:29
【问题描述】:
我在 Python 2.7.11 中使用 Tkinter 和 ttk 创建 GUI,它基本上是数据集的转换器。这部分工作没有问题。然而,我正在编程的帮助窗口让我很头疼。
目前帮助是Tkinter.Toplevel 并通过Notebooks 构成,其中包括ttk.Frames、ttk.LabelFrames 和ttk.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_columnconfigure 或columnconfigure 似乎也没有区别。
【问题讨论】:
-
您所说的 2 个笔记本条目配置为保存在同一个位置(行=0,列=0)。
-
什么是
align_text,它对于重现问题是否重要?另外,重现问题是否需要取胜主题? -
目前还不清楚是什么问题。这一切似乎都按我预期的方式工作,但显然你期待的是不同的东西。您提到使用
Toplevel,但您的代码中没有Toplevel。问题仅仅是笔记本没有填满窗口吗?
标签: python python-2.7 tkinter sticky ttk