teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

動作するコード提示と 主旨変更

2021/12/18 04:14

投稿

saya24
saya24

スコア258

title CHANGED
@@ -1,1 +1,1 @@
1
- TkInter: Treeview全体幅を設定したいが ひょっとして その手法はない?
1
+ TkInter: Treeview全体幅が可変してしまう事象回避したい
body CHANGED
@@ -1,22 +1,222 @@
1
- 下記一目瞭然だと思いますが、ツリービュ設定される内容次第 ツリビュー全体幅が可変してしまいます
1
+ ツリービュ全体幅の設定は現実的きでないとのアドバイスを頂き、簡素化したコドを作成確認を進める流れとなりまし、今回簡易版かつ動作できるコードをご用意しました
2
- ![イメージ説明](935628137d0ebe9a1ecaec433f91444d.png)
2
+ ![1](381fe133ed94bcdde5e4cda42574920a.png)
3
+ ![2](0947a90f20ca7a22079c8a4286c54559.png)
3
- ![イメージ説明](6a0ee69333ddfa4c800c63b0202e119a.png)
4
+ ![3](139abd62967409638061b826dbce04cb.png)
4
5
 
6
+ ```python
7
+ import tkinter as tk
5
- 標題のとおり表示内容にかかわらず当該ツリービューの全体幅を固定したいので ウィンドウから幅を導いて設定するよう以下コードを試行しましたが unknown option "-width" というエラーを招いてしまいました。
8
+ import tkinter.ttk as ttk
9
+ import math
10
+ import os
6
11
 
7
- ```python
12
+ def resource_path(relative_path):
13
+ try:
14
+ base_path = sys._MEIPASS
15
+ except Exception:
16
+ base_path = os.path.abspath(".")
17
+
18
+ return os.path.join(base_path, relative_path)
19
+
20
+ def adjust_windowsize1(root):
21
+ ww = root.winfo_screenwidth()
22
+ wh = root.winfo_screenheight()
23
+
24
+ lw = math.ceil(ww * 0.3208)
25
+ lh = math.ceil(wh * 0.477)
26
+
27
+ root.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2)))
28
+
29
+ def generate_frame(root):
30
+ def change_frame(frame):
31
+ frame.tkraise()
32
+
33
+ def force_entrytree1():
34
+ dict_columns = {'提供データ': ['固定値', '機器[A1]', '国名[B1]', '運送[C1]', '区分[D1]', '出荷[E1]', 'D/O[F1]', '顧客[G1]', '業者[H1]', '注番[I1]', '商品[J1]', 'サブ[K1]', 'ロケ[L1]', '数量[M1]', 'ロット[N1]', '有効期限[O1]']}
35
+
36
+ tree1.delete(*tree1.get_children())
37
+ for p in dict_columns.keys():
38
+ parent = tree1.insert("", tk.END, text=p,)
39
+ for m in dict_columns[p]:
40
+ child = tree1.insert(parent, tk.END, text=m,)
41
+
42
+ def force_entrytree3():
43
+ dict_reccolumns = {'554347829': ['商品', 'J1', 'そのまま', '', '製品', '', 1], '608825980': ['商品', 'J1', '変換', 1, '品目コード', '', 2], '744751990': ['商品', 'J1', '変換', 2, '品名', '', 3], '832969584': ['ロット', 'N1', 'そのまま', '', 'ロット', '', 4], '929827175': ['有効期限', 'O1', 'そのまま', '', '使用期限', '', 5], '054158659': ['固定値', '', '', '', 'ラベル大', 'ごにょごにょ', 6], '824305845': ['固定値', '', '', '', 'ラベル小', 'ごにょごにょ', 7], '955828413': ['割当数', 'M1', 'そのまま', '', '入荷数量', '', 8], '432056575': ['固定値', '', '', '', 'ラベル枚数', 'ごにょごにょ', 9], '717725311': ['商品', 'J1', '変換', 3, '5', '', 10]}
44
+
45
+ lst_header = []
46
+ for i in dict_reccolumns.keys():
47
+ lst_header.append(i)
48
+
49
+ tree3["columns"] = lst_header
50
+ for i in tree3["columns"]:
51
+ tree3.heading(i, text="ABC")
52
+
53
+ val = []
54
+ faddress = ""
55
+ ftype = ""
56
+ fconvno = ""
57
+ fvalue = ""
58
+ for i in dict_reccolumns.keys():
59
+ if dict_reccolumns[i][1] == "":
60
+ faddress = "□"
61
+ else:
62
+ faddress = dict_reccolumns[i][1]
63
+
64
+ if dict_reccolumns[i][2] == "":
65
+ ftype = "□"
66
+ else:
67
+ ftype = str(dict_reccolumns[i][2])
68
+
69
+ if dict_reccolumns[i][3] == "":
70
+ fconvno = "□"
71
+ else:
72
+ fconvno = str(dict_reccolumns[i][3])
73
+
74
+ if dict_reccolumns[i][5] == "":
75
+ fvalue = "□"
76
+ else:
77
+ fvalue = dict_reccolumns[i][5]
78
+
79
+ val.append(faddress + "/" + ftype + "/" + fconvno + "/" + fvalue)
80
+
81
+ tree3.delete(*tree3.get_children())
82
+
83
+ if len(dict_reccolumns.keys()) > 0:
84
+ tree3.insert("", tk.END, values=val)
85
+ for i in tree3["columns"]:
86
+ tree3.column(i, anchor=tk.CENTER)
87
+
88
+ style = ttk.Style()
89
+ style.theme_use('winnative')
90
+ style.configure("TButton", font=("Arial", 16))
91
+ style.configure("A.TButton", font=("Arial", 12))
92
+ style.configure("LTREE.Treeview", background="black", foreground="white", fieldbackground="black", rowheight=25,)
93
+ style.configure("UTREE.Treeview", background="white", foreground="black", fieldbackground="white", rowheight=25,)
94
+
95
+ style.configure("Treeview.Heading", background="green", foreground="white", rowheight=25,)
96
+ style.configure("INQ.Treeview.Heading", background="green", foreground="white", rowheight=25,)
97
+ #
98
+ frmMain = ttk.Frame(root, name="frmMain")
99
+ frmMain.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S)
100
+
101
+ btn_IOMenu = ttk.Button(frmMain, text = "あ", command=lambda: change_frame(frmIOMenu))
102
+ btn_IOMenu.pack(fill = tk.BOTH, expand=True)
103
+ btn_IOMenu.bind('<Return>', lambda a: change_frame(frmIOMenu))
104
+
105
+ btn_ConvMenu = ttk.Button(frmMain, text = "い")
106
+ btn_ConvMenu.pack(fill = tk.BOTH, expand=True)
107
+
108
+ btn_RunMenu = ttk.Button(frmMain, text = "う")
109
+ btn_RunMenu.pack(fill = tk.BOTH, expand=True)
110
+
111
+ btn_Close = ttk.Button(frmMain, text = "終了", command=root.destroy)
112
+ btn_Close.pack(fill = tk.BOTH, expand=True)
113
+ btn_Close.bind('<Return>', lambda a : root.destroy())
114
+
115
+ #
116
+ frmIOMenu = ttk.Frame(root, name="frmIOMenu")
117
+ frmIOMenu.grid_rowconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], weight=1)
118
+ frmIOMenu.grid_columnconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1, minsize=40)
119
+ frmIOMenu.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S)
120
+
121
+ cmbox_RecNo = ttk.Combobox(frmIOMenu, height=3, justify=tk.CENTER)
122
+ cmbox_RecNo.grid(row=0, column=0, columnspan=2, sticky=tk.N + tk.S + tk.E + tk.W, pady=(10,0), padx=(10,0))
123
+
124
+ ent_RecName = ttk.Entry(frmIOMenu, font=("Arial", 16))
125
+ ent_RecName.grid(row=0, column=2, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, pady=(10,0), padx=(0,10))
126
+
127
+ btn_AssignInputFile = ttk.Button(frmIOMenu, text = "え", command=lambda : force_entrytree1())
128
+ btn_AssignInputFile.grid(row=1, column=0, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=(10,0))
129
+
130
+ ent_InputPath = ttk.Entry(frmIOMenu, font=("Arial", 16))
131
+ ent_InputPath.grid(row=1, column=2, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
132
+
133
+ btn_AssignOutputFile = ttk.Button(frmIOMenu, text = "お")
134
+ btn_AssignOutputFile.grid(row=2, column=0, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=(10,0))
135
+
136
+ ent_OutputPath = ttk.Entry(frmIOMenu, font=("Arial", 16))
137
+ ent_OutputPath.grid(row=2, column=2, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
138
+
139
+ lab_ConvNo = ttk.Label(frmIOMenu, text="け", font=("Arial", 12))
140
+ lab_ConvNo.grid(row=3, column=4, columnspan=2, sticky=tk.E + tk.N + tk.S)
141
+
142
+ ent_Sheet = ttk.Entry(frmIOMenu, font=("Arial", 16))
143
+ ent_Sheet.grid(row=3, column=6, columnspan=4, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
144
+
8
145
  tws = root.winfo_width() * 0.4594
9
- tree1 = ttk.Treeview(frmIOMenu, show="tree", style="LTREE.Treeview", width=tws)
146
+ tree1 = ttk.Treeview(frmIOMenu, show="tree", style="LTREE.Treeview")
10
- ```
147
+ tree1.grid(row=4, column=0, rowspan=4, columnspan=4, sticky=tk.E + tk.W + tk.N + tk.S, pady=(5,0), padx=(10,0))
11
148
 
149
+ ybar1 = ttk.Scrollbar(frmIOMenu, orient=tk.VERTICAL, command=tree1.yview)
150
+ ybar1.grid(row=4, column=3, rowspan=4, sticky=tk.N + tk.S + tk.E, pady=(5,0), padx=(10,0))
151
+ tree1.config(yscrollcommand=lambda f, l: ybar1.set(f, l))
12
152
 
13
- ### 問題解決のため 自分が試したこと
153
+ btn_AddColumn = ttk.Button(frmIOMenu, text = "→")
14
- [こちら](https://cercopes-z.com/Python/stdlib-tkinter-widget-treeview-py.html#method-column)の参考
154
+ btn_AddColumn.grid(row=4, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, pady=(5,0))
15
- カラム単位の列幅を指定する手法の記載あるも、全体の幅を固定値で設定できるような記述を 見つけることができませんでした。
16
155
 
156
+ lstbox = tk.Listbox(frmIOMenu, bg="black", exportselection=False, foreground="white", font=("Arial", 14))
157
+ lstbox.grid(row=4, column=6, rowspan=4, columnspan=4, sticky=tk.E + tk.W + tk.N + tk.S, pady=(5,0), padx=(0,10))
17
158
 
18
- ### 質問
19
- ツリービュー全体の列幅を設定することはできないのか?
159
+ ybar2 = ttk.Scrollbar(frmIOMenu, orient=tk.VERTICAL, command=lstbox.yview)
160
+ ybar2.grid(row=4, column=9, rowspan=4, sticky=tk.N + tk.S + tk.E, pady=(5,0), padx=(0,10))
20
- つまり....各列幅のトータルから自動で求まるので、各列単位に幅を調整しれければならないのか? をお聞きしたいです。
161
+ lstbox.config(yscrollcommand=lambda f, l: ybar2.set(f, l))
21
162
 
163
+ xbar2 = ttk.Scrollbar(frmIOMenu, orient=tk.HORIZONTAL, command=lstbox.xview)
164
+ xbar2.grid(row=7, column=6, columnspan=4, sticky=tk.S + tk.E + tk.W, padx=(0,10))
165
+ lstbox.config(xscrollcommand=lambda f, l: xbar2.set(f, l))
166
+
167
+ btn_Up = ttk.Button(frmIOMenu, text = "↑")
168
+ btn_Up.grid(row=5, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S)
169
+
170
+ btn_Down = ttk.Button(frmIOMenu, text = "↓")
171
+ btn_Down.grid(row=6, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S)
172
+
173
+ btn_Cancel = ttk.Button(frmIOMenu, text = "←")
174
+ btn_Cancel.grid(row=7, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N+ tk.S)
175
+
176
+ lab_Type = ttk.Label(frmIOMenu, text="こ")
177
+ lab_Type.grid(row=8, column=0, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
178
+
179
+ cmbox_Type = ttk.Combobox(frmIOMenu, height=3, font=("Arial", 16))
180
+ cmbox_Type.grid(row=9, column=0, columnspan=2, sticky=tk.N + tk.S + tk.E + tk.W, padx=(10, 0))
181
+
182
+
183
+ tree3 = ttk.Treeview(frmIOMenu, show="headings", height=1, style="UTREE.Treeview")
184
+ tree3.grid(row=10, column=0, columnspan=9, sticky=tk.E + tk.W, padx=(10,0), pady=(5,0))
185
+
186
+ btn_Add = ttk.Button(frmIOMenu, text = "せ", command=lambda : force_entrytree3())
187
+ btn_Add.grid(row=10, column=9, rowspan=2, sticky=tk.N + tk.S + tk.E+ tk.W, padx=(0,10), pady=(5,0))
188
+
189
+ xbar3 = ttk.Scrollbar(frmIOMenu, orient=tk.HORIZONTAL, command=tree3.xview)
190
+ xbar3.grid(row=11, column=0, columnspan=9, sticky=tk.N + tk.S + tk.E + tk.W, padx=(10,0))
191
+ tree3.config(xscrollcommand=lambda f, l: xbar3.set(f, l))
192
+
193
+
194
+
195
+ btn_ReturnMenu = ttk.Button(frmIOMenu, text = "閉じる", command=lambda: change_frame(frmMain))
196
+ btn_ReturnMenu.grid(row=12, column=8, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, pady=(0,10), padx=(0,10))
197
+
198
+
199
+ frmMain.tkraise()
200
+
201
+
202
+ if __name__ == "__main__":
22
- よろしくお願いします。
203
+ root = tk.Tk()
204
+
205
+ adjust_windowsize1(root)
206
+ root.title("ボタン小さくなる")
207
+
208
+ root.grid_columnconfigure([0,1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1)
209
+ root.grid_rowconfigure([0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], weight=1)
210
+
211
+ root.resizable(0,0)
212
+ root.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
213
+
214
+ root["cursor"] = "hand2"
215
+
216
+ generate_frame(root)
217
+
218
+ root.mainloop()
219
+
220
+ ```
221
+
222
+ コード含め 本文10,000文字がリミットの掲載なので 主旨が短文となってしまいました。すみません