查找 Ttk Notebook 当前选中的选项卡 [英] Finding the currently selected tab of Ttk Notebook

查看:38
本文介绍了查找 Ttk Notebook 当前选中的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 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 将其转换为所选标签的编号.

<预><代码>>>>nb.select()'.4299842480.4300630784'>>>nb.index(nb.select())2

请注意,您还可以使用 tab

获取有关所选标签的更多信息<预><代码>>>>nb.tab(nb.select(), "文本")'mytab2'

您可以查看 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 当前选中的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆