How to get widget object from ttk.Notebook tab?

技术标签: Python  tkinter.  TK.

I have notebook widget of class ttk.Notebook. In this notebook I'm adding many tabs:

notebook.add(child=my_object, text='my tab')

How can I get tab object and not tab name?

更新1

So, there is children dictionary. OK that brings next problem.

What I really want to do is to take my hands on object of active tab in Notebook.所以我有这个:

notebook.children[notebook.select().split('.')[2]])

but it looks ugly. Unfortunately widget name returned by select() has not exactly same format as widget names in children[]. Is there any method of reliable translation of one format to another or split() will never fail in this situation?

This is demo of problem:

#!/usr/bin/env python3

from tkinter import *
from tkinter.ttk import *


class App(Tk):
    def __init__(self):
        super().__init__()
        notebook = Notebook(self)
        notebook.add(child=Frame(notebook), text='tab1')
        notebook.add(child=Frame(notebook), text='tab2')
        notebook.add(child=Frame(notebook), text='tab3')
        notebook.pack()

        print('Tabs are: ', notebook.children.keys())
        print('Active tab: ', notebook.select())
        print(type(notebook.children[notebook.select().split('.')[2]]))

App().mainloop()

看答案

notebook.tabs() call returns a list of "tab names" which correspond to the children widget objects. A call to someWidget.nametowidget(tab_name) resolves to the child widget associated with that tab.

In other words, the Notebook .tabs() method returns a list of the names of the Notebook's children.

For your UPDATE:

The same as above applies, only now it's:

active_object = notebook.nametowidget(notebook.select())

Different versions of Python use different naming conventions, so using nametowidget() is the better way to go rather than manually processing the name to get the desired key to use for a reference into the children dict.


智能推荐

Unable to get information from SQL Server

java.sql.SQLException: Unable to get information from SQL Server: localhost. 参考官方资料:http://jtds.sourceforge.net/faq.html#getConnectionHangs 解决方法:启动SqlServer Browser服务。 若服务是被禁用的,先设置成自动或手动再启动Browser服务。...

Android SDK How to Install and Get Started

Android SDK How to Install and Get Started 1.  Introduction Android is an Operating System for mobile devices developed by Google, which is built upon Linux kernel. Android compete...

How to get propose products in Sales Order

Logon on HN1/Q2U using SALESPRO role, Sales Cycle -> Sales Order Search -> Edit List in Items AB -> More -> Propose Items from Listings (please see CRM_PT_SALES_Product_Proposal.xls for de...

Get a Better Video Image: Learn How to Set Exposure

Get a Better Video Image: Learn How to Set Exposure by David Bode30 Mar 2017 Difficulty:BeginnerLength:MediumLanguages:Englishالعربية/عربيEspañolFrançaisहिंदीBahasa Indonesia VideoE...

how to get line number of given ABAP source code

Suppose you need to get the line number of given ABAP source code for whatever reasons ( analysis or statistics ), just execute report in AG3/001: TOOL_CODE_LINE_COUNT Specify the package and execute ...

猜你喜欢

opc使用教程

新新opc使用教程 如上图所示,以图片的序号为下面的说明序号对应: 1.用于获取本机的账号。 2.选择本机获取到的账号。 3.选择获取到的本机的一个opc server。 4.点击建立连接,最下面的运行日志,会有显示,连接成功与否。 5.开始浏览标签,即可获取到opc server中的所有标签,选择一些标签。 6.这一列和右侧类似的列会显示,你所选择的标签。 7.读取标签,读取间隔为2s. 8.这...

构建Xamarin.Forms NuGet

探索Xamarin.Forms(并在代码中变得更加舒适)的一个很好的方法是克隆存储库并运行它,特别是任何ControlGallery项目。 我们知道您中有许多人已经做到了这一点,所以您可以采取的下一步是为XAMarin.Forms进行自己的定制,以便在组织内构建和分发您自己的NuGet软件包。 在这篇文章中,让我们在本地进行一下,然后讨论如何在Visual Studio Team Services...

tomcat项目上线

目前我用的eclipse,myeclipse同样也是一样的操作 右击项目 选中在您要导出的项目上点击鼠标右键,选择Export 然后选择路经 起名字 然后finish。 接下来就是把war包放入tomcat服务器中, 首先启用远程连接命令连接服务器  远程连接的就是你购买的服务器的公网IP地址。  用户名一般都是Adminnistrator 密码是在阿里云控制台中设置 连接完毕...

session的实例

3、将要获取的name放在一个类对象中 将1中的name修改为 将2中的String改为Person 然后输出 4、设置失效时间 5、 session和cookie的区别 cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个) session把用户的数据写到用户独占session中,在服务器端保存(保存重要信息,减少服务器的资源浪费) session对象由服务器创建; 6、使用场景...

安装jdk 缺少jvm.dll 问题

下载JDK: https://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量: 一般装完jdk配置好环境变量就没问题了,第一次碰到不存在jvm.dll的情况。 安装1.7就不会,所以把1.7里JDK\bin 和 jre\bin 里面的jvm.dll所在的client、server目录拷贝到1.8对应的目...

问答精选

joining table with itself using mysql

When I write this line on mysql on phpmyadmin using mariadb server getting unexpected error near a.cpid = b.cid MySQL said: #1064 - You have an error in your SQL syntax; check the manual that correspo...

Pointers: c++ requires a type specifier for all declarations

I am currently learning c programming, and I've encountered a problem in one of the exercises I'm doing. Whilst trying to decode how pointers work, i receive the following error: c++ requires a type s...

Using Tensorflow in C++ application: How to release GPU memory

I am trying to use Tensorflow for inference within my C++ application. Other parts of the application need access to large amounts of GPU memory (not at exactly the same time as Tensorflow). However, ...

VBA: Download a picture via HTTPS

I'm trying to download a picture from a website (Grafana monitoring tool, API) using VBA (MS Access 2016). It's a HTTPS address using a self signed ceritificat and requires username and password for a...

Selectively retrieving an object from a list within a Grails Domain object

I'm 2 months into Grails development and have stumbled across this challenge which i can't find a answer to having used books, the grails website and forum searches. I'm hoping someone can help me. I ...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答