问题描述
我有一个包含 8 个框架的 Ttk Notebook 小部件 - 所以,8 个标签.每个框架都包含一个文本小部件.我在 Notebook 小部件外有一个按钮,我想在按下此按钮时将文本插入到当前选项卡文本小部件中.
这似乎需要确定当前选择了 Notebook 中的哪个小部件,但我似乎无法找到如何执行此操作.我如何找到当前选择的标签?
或者,我怎样才能实现我想要的?
如果有帮助,这是我的笔记本的代码:
self.nb = Notebook(master)self.nb.pack(fill='both', expand='yes', padx=10, pady=10)self.frames = []self.texts = []对于 xrange(8) 中的 i: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
将其转换为所选标签的编号.
请注意,您还可以使用 tab
您可以查看 Notebook 参考文档:http://docs.python.org/3/library/tkinter.ttk.html#notebook
I have a Ttk Notebook widget containing 8 Frames - so, 8 tabs. Each frame contains a Text widget. I have a button outside the Notebook widget, and I want to insert text into the current tabs Text widget when this button is pressed.
This would seem to require working out which widget in the Notebook is currently selected, but I can't seem to find how to do this. How would I find the currently selected tab?
Alternatively, how can I implement what I want to?
If it helps, here's the code for my 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')
You can retrieve the selected tab through select
method. However, this method returns a tab_id which is not much useful as is. index
convert it to the number of the selected tab.
>>> nb.select()
'.4299842480.4300630784'
>>> nb.index(nb.select())
2
Note that you coud also get more information about the selected tab using tab
>>> nb.tab(nb.select(), "text")
'mytab2'
You might look at Notebook reference documentation : http://docs.python.org/3/library/tkinter.ttk.html#notebook
这篇关于查找 Ttk Notebook 当前选中的标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2