0
0

More than 1 year has passed since last update.

Tkinterのウィジェット一覧をリストで取得する

Posted at

TkinterでGUIを作成するときに配置したウィジェットを一覧で取得する必要があったのでメモ。

import tkinter as tk

# ウィジェット一覧をリストで取得
def get_all_widget (wid, finList=None) -> list:
    if finList is None:
        finList = []
    _list = wid.winfo_children()        
    for item in _list :
        finList.append(item)
        get_all_widget(item,finList)   
    return finList

root = tk.Tk()

# ~~~~~~~~~~
# ウィジェット配置処理は割愛。
# ~~~~~~~~~~

for wdg in get_all_widget(root):
    print(wdg)

結果

直下だけでなくcanvasの中のwidgetも取得する。

.!application
.!label 
.!entry 
.!label2
.!entry2
.!label3
.!entry3
.!button
.!label4
.!canvas
.!canvas.!radiobutton
.!canvas.!radiobutton2
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0