質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3363閲覧

TkInter: フレーム名(インスタンス名?)を取得したいが対応方法が分かりません

saya24

総合スコア227

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/10/22 12:58

編集2021/10/22 14:56

フレームの最上位表示(切替え)で画面遷移を達成しているイメージで、今後更にフレーム(メニュー画面)を増やしていく計画です。
フレームの切替えは 下記コード内のchange_frame関数で行っており、引数からフレーム種類を導いています。

と、いうことで...該当フレームを表示するとき
フレーム上のWidgetを初期状態に戻すため 別途のフレーム別の関数(Initial_フレーム名)を呼び出そうと考えているのですが
以外にフレーム名の取得に頭を悩ましています。

TkInterの理解不足ではなく、インスタンス名を得る Pythonの基本的な理解不足かも知れませんが、change_frame関数の引数・Frameの名称の把握方法を 教えて頂けないでしょうか
基本的なことで 申し訳ありませんがよろしくお願いします。

Python

1# tkinterのインポート 2import tkinter as tk 3import tkinter.ttk as ttk 4import math 5import os 6from tkinter import filedialog 7import tkinter.messagebox as tkmb 8import openpyxl 9from functools import partial 10################################################################################# 11# # 12# 一般関数 # 13# # 14# # 15################################################################################# 16# フレーム初期化-入手力定義メニュー 17def initial_frmIOMenu(): 18 ent_InputPath.configure(state='normal') 19 ent_InputPath.delete(0, tk.END) 20 ent_InputPath.configure(state='disabled') 21 22 ent_OutputPath.configure(state='normal') 23 ent_OutputPath.delete(0, tk.END) 24 ent_OutputPath.configure(state='disabled') 25 26 tree1.delete(*tree1.get_children()) 27 tree2.delete(*tree2.get_children()) 28 29# フレーム切替え=画面遷移 30def change_frame(frame): 31 ###if frame.name == "frmIOMenu": #★フレーム名の取得方法が分かりません!!★ 32 ### initial_frmIOMenu() 33 34 frame.tkraise() 35 36 37# 入力ブック情報取得 38def get_bookinfo(dict_Book, tree): 39 40 for p in dict_Book: # キーを親要素に設定 41 parent = tree.insert("","end",text=p,) 42 First = True 43 for m in dict_Book[p]: # 要素を子要素に設定 44 if (First): 45 tree.insert(parent,"end",text="===固定値挿入===",) 46 First = False 47 child = tree.insert(parent,"end",text=m,) 48 49################################################################################# 50# # 51# 画面生成 # 52# # 53# # 54################################################################################# 55def resource_path(relative_path): 56 try: 57 # PyInstaller creates a temp folder and stores path in _MEIPASS 58 base_path = sys._MEIPASS 59 except Exception: 60 base_path = os.path.abspath(".") 61 62 return os.path.join(base_path, relative_path) 63 64 65 66def adjust_windowsize(root): 67 ww = root.winfo_screenwidth() 68 wh = root.winfo_screenheight() 69 70 lw = math.ceil(ww * 0.208) 71 lh = math.ceil(wh * 0.277) 72 73 root.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2))) 74################################################################################# 75# # 76# フレーム生成 # 77# # 78# # 79################################################################################# 80def generate_frame(root): 81 82 #列構成のツリーで、列が選択された 83 def on_tree_state(state, event): 84 """状態に応じて、ボタンのstateを変更""" 85 if state: 86 btn_Cancel.config(state=tk.DISABLED) 87 btn_AddColumn.config(state=tk.NORMAL) 88 else: 89 btn_Cancel.config(state=tk.NORMAL) 90 btn_AddColumn.config(state=tk.DISABLED) 91 92 def get_tree_selection_children(tree): 93 """選択中の子要素のリストを返す""" 94 for item in tree.selection(): 95 if tree.parent(item): 96 yield item 97 98 def on_tree_select(event): 99 tree = event.widget 100 items = list(get_tree_selection_children(tree)) 101 if items: 102 # 子要素が一つ以上選択されている場合 103 tree.event_generate("<<StateSelect>>") 104 else: 105 # 子要素が一つも選択されていない場合 106 tree.event_generate("<<StateDeselect>>") 107 108 ############################################################################# 109 # # 110 # STYLE # 111 # # 112 ############################################################################# 113 style = ttk.Style() 114 style.theme_use('winnative') 115 116 style.configure("Treeview", background="black", foreground="white", fieldbackground="black") 117 style.map("Treeview",background=[("selected", "silver")]) 118 style.map("Treeview",foreground=[("selected", "red")]) 119 ############################################################################# 120 # # 121 # メインメニュー用フレーム設置 # 122 # # 123 ############################################################################# 124 frmMain = ttk.Frame(root) 125 frmMain.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S) 126 127 # メインメニューにボタン配置 128 btn_SettingMenu = tk.Button(frmMain, text = "入出力定義", font=("",0,"normal","roman","normal"), command=lambda: change_frame(frmIOMenu)) 129 btn_SettingMenu.pack(fill = tk.BOTH, expand=True) 130 131 btn_SettingMenu = tk.Button(frmMain, text = "終了", font=("",0,"normal","roman","normal"), command=root.destroy) 132 btn_SettingMenu.pack(fill = tk.BOTH, expand=True) 133 ############################################################################# 134 # # 135 # 入出力定義メニュー用フレーム設置 # 136 # # 137 ############################################################################# 138 frmIOMenu = ttk.Frame(root) 139 frmIOMenu.grid_rowconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], weight=1) 140 frmIOMenu.grid_columnconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1, minsize=40) 141 frmIOMenu.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S) 142 143 144 145 # 3 シート構成・列構成ツリー 146 tree1 = ttk.Treeview(frmIOMenu, show="tree", height=4) 147 tree1.grid(row=3, column=0, rowspan=4, columnspan=5, sticky=tk.E + tk.W + tk.N + tk.S) 148 tree1.bind("<<StateSelect>>", partial(on_tree_state, True)) 149 tree1.bind("<<StateDeselect>>", partial(on_tree_state, False)) 150 tree1.bind("<<TreeviewSelect>>", on_tree_select) 151 152 153 154 # 3 転記ボタン 155 btn_AddColumn = tk.Button(frmIOMenu, text = "→", state="disabled") 156 btn_AddColumn.grid(row=3, column=5, sticky=tk.E + tk.W + tk.N + tk.S) 157 158 # 3 転記済列の表 159 tree2 = ttk.Treeview(frmIOMenu, show="headings", height=4) 160 tree2.grid(row=3, column=6, rowspan=4, columnspan=4, sticky=tk.E + tk.W + tk.N + tk.S) 161 162 163 # 1 入力ファイル指定ボタン 164 dict_Book = {'店舗一覧': ['県CD [A1]', '市町村CD [B1]', '店舗CD [C1]', '担当者CD [D1]'], 165 '取扱品目': ['品目CD [A1]', '品目名称 [B1]', '販売価格 [C1]', '販売開始月 [D1]'], 166 '納入実績': ['県CD [A1]', '品目CD [B1]', '年月 [C1]', '金額 [D1]']} 167 168 btn_AssignInputFile = tk.Button(frmIOMenu, text = "入力ファイル指定", command=get_bookinfo(dict_Book, tree1)) 169 btn_AssignInputFile.grid(row=1, column=0, columnspan=3, sticky=tk.E + tk.W + tk.N + tk.S) 170 171 172 # 1 指定ファイルパス格納テキストボックス 173 ent_InputPath = tk.Entry(frmIOMenu, state="disabled") 174 ent_InputPath.grid(row=1, column=3, columnspan=7, sticky=tk.E + tk.W + tk.N + tk.S) 175 176 # 2 出力ファイル指定ボタン 177 btn_AssignOutputFile = tk.Button(frmIOMenu, text = "出力ファイル指定") 178 btn_AssignOutputFile.grid(row=2, column=0, columnspan=3, sticky=tk.E + tk.W + tk.N + tk.S) 179 180 # 2 指定ファイルパス格納テキストボックス 181 ent_OutputPath = tk.Entry(frmIOMenu, state="disabled") 182 ent_OutputPath.grid(row=2, column=3, columnspan=7, sticky=tk.E + tk.W + tk.N + tk.S) 183 184 # 4 出力一上昇ボタン 185 btn_Up = tk.Button(frmIOMenu, text = "↑", state="disabled") 186 btn_Up.grid(row=4, column=5, sticky=tk.E + tk.W + tk.N + tk.S) 187 188 # 5 出力一下降ボタン 189 btn_Down = tk.Button(frmIOMenu, text = "↓", state="disabled") 190 btn_Down.grid(row=5, column=5, sticky=tk.E + tk.W + tk.N + tk.S) 191 192 # 6 転記中止ボタン 193 btn_Cancel = tk.Button(frmIOMenu, text = "←", state="disabled") 194 btn_Cancel.grid(row=6, column=5, sticky=tk.E + tk.W + tk.N+ tk.S) 195 196 # 11 メニューへボタン 197 btn_ReturnMenu = tk.Button(frmIOMenu, text = "閉じる", command=lambda: change_frame(frmMain)) 198 btn_ReturnMenu.grid(row=11, column=8, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S) 199 200 # 起動時メインのフレームを前面に 201 frmMain.tkraise() 202 203 204 205if __name__ == "__main__": 206 # ウインドウの作成 207 root = tk.Tk() 208 209 #フォームサイズを実行端末から導き、ド真中に配置表示 210 adjust_windowsize(root) 211 212 #タイトルを指定 213 root.title("TkInterの勉強") 214 215 #フレーム切替え達成の上で とても重要、ルートのグリッド定義 216 root.grid_rowconfigure(0, weight=1) 217 root.grid_columnconfigure(0, weight=1) 218 219 220 #タイトル左隅のアイコン 221 #iconfile = resource_path("images\favicon.ico") 222 #root.iconbitmap(default=iconfile) 223 224 #フォームの最大化、×ボタン操作を無効化 225 root.resizable(0,0) 226 root.protocol('WM_DELETE_WINDOW', (lambda: 'pass')()) 227 228 # カーソル変更 229 root["cursor"] = "hand2" 230 231 generate_frame(root) 232 233 root.mainloop()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

生成時に name を付けて識別することができます。
指定しない場合は、連番のIDが割り振られています。

python

1import tkinter as tk 2 3root = tk.Tk() 4frame = tk.Frame(root, name="mainFrame") 5 6# ウィジェットの名前を問い合わせる 7print(frame.winfo_name()) # => "mainFrame" 8 9# 名前からウィジェットを問い合わせ 10frame2 = root.nametowidget("mainFrame") 11frame2.config(bg="blue", width=200, height=200) 12 13frame.pack() 14 15root.mainloop()

但し、同じ name のウィジェットは Python上で別インスタンスでも
tcl/tk 側では同じウィジェットになるので注意。

投稿2021/10/23 04:20

teamikl

総合スコア8664

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

saya24

2021/10/23 07:58

いつもお世話になっております。ありがとうございました、予め与える必要があるのですね
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問