質問文はLabelとEntryを合わせたクラスを新規に作りたいという話なのでしょうか?
それともパラメータを元にGUIの生成処理を纏めたいという話なのでしょうか?
どこまでGUIの生成処理に汎用性を持たせるのかは、要件と時間とのトレードオフになりますが。
あるパラメータを元にGUIの生成処理を自動化したいだけでしたら。
以下のようになります。
Python
1# -*- coding: utf-8 -*-
2import tkinter as Tk
3from collections import OrderedDict
4from pprint import PrettyPrinter
5
6
7def load_widget():
8 widget_list = list()
9 widget_list.append({'type': 'Label', 'text': "Data File :", 'relx': 0.01, 'rely': 0.005})
10 widget_list.append({'type': 'Entry', 'width': 80, 'relx': 0.07, 'rely': 0.01, 'focus_set': True})
11 widget_list.append({'type': 'Label', 'text': "Ref File :", 'relx': 0.01, 'rely': 0.055})
12 widget_list.append({'type': 'Entry', 'width': 80, 'relx': 0.07, 'rely': 0.055})
13 widget_list.append({'type': 'Label', 'text': "Sample Name :", 'relx': 0.01, 'rely': 0.10})
14 widget_list.append({'type': 'Entry', 'width': 20, 'relx': 0.1, 'rely': 0.105})
15 return widget_list
16
17
18def create_widgets(root):
19 # ルート参照から到達できるように生成したwidgetを保持
20 widget_dict = OrderedDict()
21 for i, component in enumerate(load_widget()):
22 widget = None
23 if component['type'] == 'Label':
24 widget = Tk.Label(root, text=component['text'])
25 elif component['type'] == 'Entry':
26 widget = Tk.Entry(root, width=component['width'])
27 if 'focus_set' in component:
28 if component['focus_set']:
29 widget.focus_set()
30 else:
31 assert widget is not None, component['type']
32 widget.place(relx=component['relx'], rely=component['rely'])
33 widget_dict[i] = widget
34 return widget_dict
35
36
37def main():
38 root = Tk.Tk()
39 root.geometry("1000x600")
40 root.widgets = create_widgets(root)
41 # 作成したwidgetを出力
42 pp = PrettyPrinter(indent=4)
43 pp.pprint(root.widgets)
44 root.mainloop()
45
46
47if __name__ == '__main__':
48 main()
49
- GUIの生成処理は使用するレイアウトマネージャに依存します
今回の場合は#placeメソッドです。
0. GUIの構成情報はjsonまたはxml形式のファイルにして、読み込んだ方が汎用性が高いです。
0. Tk.Entry,Tk.Labelのコンストラクタは引数:optionsとしてdictを受け付けるので、あるLabelに設定値が大量にある場合は、dictをそのまま渡す形にしたほうが楽です。
0. GUIをごりごり書く場合はtkinter 以外のウィジットについて一考してみてはー。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。