我有一个包含8个框架的Ttk Notebook小部件 - 所以,8个标签。每个框架都包含一个Text小部件。我在Notebook小部件外面有一个按钮,我想在按下此按钮时将文本插入当前选项卡Text小部件。
这似乎需要确定当前选择Notebook中的哪个小部件,但我似乎无法找到如何执行此操作。如何找到当前选中的标签?
或者,我该如何实现我想要的?
如果它有帮助,这是我的笔记本的代码:
self.nb = Notebook(master)
self.nb.pack(fill='both', expand='yes', padx=10, pady=10)
self.frames = []
self.texts = []
for i in xrange(8):
self.frames.append(Frame())
self.nb.add(self.frames[i])
self.texts.append(Text(self.frames[i]))
self.texts[i].pack(fill='both')
您可以通过select
方法检索选定的选项卡。但是,此方法返回一个tab_id,它不是很有用。 index
将其转换为所选标签的编号。
>>> nb.select()
'.4299842480.4300630784'
>>> nb.index(nb.select())
2
请注意,您还可以使用qazxsw poi获取有关所选选项卡的更多信息
tab
您可以查看Notebook参考文档:>>> nb.tab(nb.select(), "text")
'mytab2'
您可以使用http://docs.python.org/3/library/tkinter.ttk.html#notebook关键字获取当前选定的标签:
"current"
查看本网站:noteBook.index("current")
24.2.5.3。标签标识符
我根本不是专家,但希望我可以帮助一些“新鲜的眼睛”。我想它可能是涉及的东西
https://docs.python.org/2/library/ttk.html#tab-identifiers
希望它有效或让你朝着正确的方向前进。
请随意编辑,因为我在这里和python相当新:-)
有两种简单的方法可以查看选择了哪个选项卡:
def buttonclick():
somevariablename = focus_get()
#Print your text into the somevariable notebook could be
#something like(not sure about the syntax):
focusednotebook = somevariablename
focusednotebook.insert('1.0', 'your text here')
yourbutton = Button(parent, text = "button name", command = buttonclick)
yourbutton.pack()
和
nb.select() # returns the Tab NAME (string) of the current selection
nb.index('current') # returns the Tab INDEX (number) of the current selection
方法也可用于通过.select()
选择当前活动的选项卡。如果没有arg,它将返回当前选择的tabId(以“name”形式)。
nb.select(tabId)
将tabId转换为数字索引。它还可以使用字符串“end”来返回选项卡的数量。因此,.index(tabId)
就像是笔记本小部件的nb.index(tkinter.END)
方法。
当没有制表符时,len()
返回一个空字符串,但.select()
会抛出异常。所以,如果你想要索引,我会说
.index('current')
是最好的方式。
在您的特定情况下,您可能希望获取当前的笔记本选项卡名称,然后通过if nb.select():
idx = nb.index('current')
方法将该名称转换为实际的子文本小部件,以进行操作。所以...
nametowidget()
tabName = notebook.select()
if tabName:
textWidget = notebook.nametowidget(tabName) # here, 'notebook' could be any widget
textWidget.insert(pos, text, tags)
方法将Tkinter名称映射到实际的小部件。它是一个可由任何实际小部件调用的方法。