質問編集履歴

1

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

2021/12/18 04:14

投稿

saya24
saya24

スコア222

test CHANGED
@@ -1 +1 @@
1
- TkInter: Treeview全体幅を設定したいが ひょっとして その手法はない?
1
+ TkInter: Treeview全体幅が可変してしまう事象回避したい
test CHANGED
@@ -1,43 +1,443 @@
1
- 下記一目瞭然だと思いますが、ツリービュ設定される内容次第 ツリビュー全体幅が可変してしまいます
1
+ ツリービュ全体幅の設定は現実的きでないとのアドバイスを頂き、簡素化したコドを作成確認を進める流れとなりまし、今回簡易版かつ動作できるコードをご用意しました
2
-
2
+
3
- ![イメージ説明](935628137d0ebe9a1ecaec433f91444d.png)
3
+ ![1](381fe133ed94bcdde5e4cda42574920a.png)
4
+
4
-
5
+ ![2](0947a90f20ca7a22079c8a4286c54559.png)
6
+
5
- ![イメージ説明](6a0ee69333ddfa4c800c63b0202e119a.png)
7
+ ![3](139abd62967409638061b826dbce04cb.png)
6
-
7
-
8
-
9
- 標題のとおり表示内容にかかわらず当該ツリービューの全体幅を固定したいので ウィンドウから幅を導いて設定するよう以下コードを試行しましたが unknown option "-width" というエラーを招いてしまいました。
10
8
 
11
9
 
12
10
 
13
11
  ```python
14
12
 
13
+ import tkinter as tk
14
+
15
+ import tkinter.ttk as ttk
16
+
17
+ import math
18
+
19
+ import os
20
+
21
+
22
+
23
+ def resource_path(relative_path):
24
+
25
+ try:
26
+
27
+ base_path = sys._MEIPASS
28
+
29
+ except Exception:
30
+
31
+ base_path = os.path.abspath(".")
32
+
33
+
34
+
35
+ return os.path.join(base_path, relative_path)
36
+
37
+
38
+
39
+ def adjust_windowsize1(root):
40
+
41
+ ww = root.winfo_screenwidth()
42
+
43
+ wh = root.winfo_screenheight()
44
+
45
+
46
+
47
+ lw = math.ceil(ww * 0.3208)
48
+
49
+ lh = math.ceil(wh * 0.477)
50
+
51
+
52
+
53
+ root.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2)))
54
+
55
+
56
+
57
+ def generate_frame(root):
58
+
59
+ def change_frame(frame):
60
+
61
+ frame.tkraise()
62
+
63
+
64
+
65
+ def force_entrytree1():
66
+
67
+ dict_columns = {'提供データ': ['固定値', '機器[A1]', '国名[B1]', '運送[C1]', '区分[D1]', '出荷[E1]', 'D/O[F1]', '顧客[G1]', '業者[H1]', '注番[I1]', '商品[J1]', 'サブ[K1]', 'ロケ[L1]', '数量[M1]', 'ロット[N1]', '有効期限[O1]']}
68
+
69
+
70
+
71
+ tree1.delete(*tree1.get_children())
72
+
73
+ for p in dict_columns.keys():
74
+
75
+ parent = tree1.insert("", tk.END, text=p,)
76
+
77
+ for m in dict_columns[p]:
78
+
79
+ child = tree1.insert(parent, tk.END, text=m,)
80
+
81
+
82
+
83
+ def force_entrytree3():
84
+
85
+ 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]}
86
+
87
+
88
+
89
+ lst_header = []
90
+
91
+ for i in dict_reccolumns.keys():
92
+
93
+ lst_header.append(i)
94
+
95
+
96
+
97
+ tree3["columns"] = lst_header
98
+
99
+ for i in tree3["columns"]:
100
+
101
+ tree3.heading(i, text="ABC")
102
+
103
+
104
+
105
+ val = []
106
+
107
+ faddress = ""
108
+
109
+ ftype = ""
110
+
111
+ fconvno = ""
112
+
113
+ fvalue = ""
114
+
115
+ for i in dict_reccolumns.keys():
116
+
117
+ if dict_reccolumns[i][1] == "":
118
+
119
+ faddress = "□"
120
+
121
+ else:
122
+
123
+ faddress = dict_reccolumns[i][1]
124
+
125
+
126
+
127
+ if dict_reccolumns[i][2] == "":
128
+
129
+ ftype = "□"
130
+
131
+ else:
132
+
133
+ ftype = str(dict_reccolumns[i][2])
134
+
135
+
136
+
137
+ if dict_reccolumns[i][3] == "":
138
+
139
+ fconvno = "□"
140
+
141
+ else:
142
+
143
+ fconvno = str(dict_reccolumns[i][3])
144
+
145
+
146
+
147
+ if dict_reccolumns[i][5] == "":
148
+
149
+ fvalue = "□"
150
+
151
+ else:
152
+
153
+ fvalue = dict_reccolumns[i][5]
154
+
155
+
156
+
157
+ val.append(faddress + "/" + ftype + "/" + fconvno + "/" + fvalue)
158
+
159
+
160
+
161
+ tree3.delete(*tree3.get_children())
162
+
163
+
164
+
165
+ if len(dict_reccolumns.keys()) > 0:
166
+
167
+ tree3.insert("", tk.END, values=val)
168
+
169
+ for i in tree3["columns"]:
170
+
171
+ tree3.column(i, anchor=tk.CENTER)
172
+
173
+
174
+
175
+ style = ttk.Style()
176
+
177
+ style.theme_use('winnative')
178
+
179
+ style.configure("TButton", font=("Arial", 16))
180
+
181
+ style.configure("A.TButton", font=("Arial", 12))
182
+
183
+ style.configure("LTREE.Treeview", background="black", foreground="white", fieldbackground="black", rowheight=25,)
184
+
185
+ style.configure("UTREE.Treeview", background="white", foreground="black", fieldbackground="white", rowheight=25,)
186
+
187
+
188
+
189
+ style.configure("Treeview.Heading", background="green", foreground="white", rowheight=25,)
190
+
191
+ style.configure("INQ.Treeview.Heading", background="green", foreground="white", rowheight=25,)
192
+
193
+ #
194
+
195
+ frmMain = ttk.Frame(root, name="frmMain")
196
+
197
+ frmMain.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S)
198
+
199
+
200
+
201
+ btn_IOMenu = ttk.Button(frmMain, text = "あ", command=lambda: change_frame(frmIOMenu))
202
+
203
+ btn_IOMenu.pack(fill = tk.BOTH, expand=True)
204
+
205
+ btn_IOMenu.bind('<Return>', lambda a: change_frame(frmIOMenu))
206
+
207
+
208
+
209
+ btn_ConvMenu = ttk.Button(frmMain, text = "い")
210
+
211
+ btn_ConvMenu.pack(fill = tk.BOTH, expand=True)
212
+
213
+
214
+
215
+ btn_RunMenu = ttk.Button(frmMain, text = "う")
216
+
217
+ btn_RunMenu.pack(fill = tk.BOTH, expand=True)
218
+
219
+
220
+
221
+ btn_Close = ttk.Button(frmMain, text = "終了", command=root.destroy)
222
+
223
+ btn_Close.pack(fill = tk.BOTH, expand=True)
224
+
225
+ btn_Close.bind('<Return>', lambda a : root.destroy())
226
+
227
+
228
+
229
+ #
230
+
231
+ frmIOMenu = ttk.Frame(root, name="frmIOMenu")
232
+
233
+ frmIOMenu.grid_rowconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], weight=1)
234
+
235
+ frmIOMenu.grid_columnconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1, minsize=40)
236
+
237
+ frmIOMenu.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S)
238
+
239
+
240
+
241
+ cmbox_RecNo = ttk.Combobox(frmIOMenu, height=3, justify=tk.CENTER)
242
+
243
+ cmbox_RecNo.grid(row=0, column=0, columnspan=2, sticky=tk.N + tk.S + tk.E + tk.W, pady=(10,0), padx=(10,0))
244
+
245
+
246
+
247
+ ent_RecName = ttk.Entry(frmIOMenu, font=("Arial", 16))
248
+
249
+ ent_RecName.grid(row=0, column=2, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, pady=(10,0), padx=(0,10))
250
+
251
+
252
+
253
+ btn_AssignInputFile = ttk.Button(frmIOMenu, text = "え", command=lambda : force_entrytree1())
254
+
255
+ btn_AssignInputFile.grid(row=1, column=0, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=(10,0))
256
+
257
+
258
+
259
+ ent_InputPath = ttk.Entry(frmIOMenu, font=("Arial", 16))
260
+
261
+ ent_InputPath.grid(row=1, column=2, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
262
+
263
+
264
+
265
+ btn_AssignOutputFile = ttk.Button(frmIOMenu, text = "お")
266
+
267
+ btn_AssignOutputFile.grid(row=2, column=0, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=(10,0))
268
+
269
+
270
+
271
+ ent_OutputPath = ttk.Entry(frmIOMenu, font=("Arial", 16))
272
+
273
+ ent_OutputPath.grid(row=2, column=2, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
274
+
275
+
276
+
277
+ lab_ConvNo = ttk.Label(frmIOMenu, text="け", font=("Arial", 12))
278
+
279
+ lab_ConvNo.grid(row=3, column=4, columnspan=2, sticky=tk.E + tk.N + tk.S)
280
+
281
+
282
+
283
+ ent_Sheet = ttk.Entry(frmIOMenu, font=("Arial", 16))
284
+
285
+ ent_Sheet.grid(row=3, column=6, columnspan=4, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
286
+
287
+
288
+
15
289
  tws = root.winfo_width() * 0.4594
16
290
 
17
- tree1 = ttk.Treeview(frmIOMenu, show="tree", style="LTREE.Treeview", width=tws)
291
+ tree1 = ttk.Treeview(frmIOMenu, show="tree", style="LTREE.Treeview")
292
+
293
+ 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))
294
+
295
+
296
+
297
+ ybar1 = ttk.Scrollbar(frmIOMenu, orient=tk.VERTICAL, command=tree1.yview)
298
+
299
+ ybar1.grid(row=4, column=3, rowspan=4, sticky=tk.N + tk.S + tk.E, pady=(5,0), padx=(10,0))
300
+
301
+ tree1.config(yscrollcommand=lambda f, l: ybar1.set(f, l))
302
+
303
+
304
+
305
+ btn_AddColumn = ttk.Button(frmIOMenu, text = "→")
306
+
307
+ btn_AddColumn.grid(row=4, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, pady=(5,0))
308
+
309
+
310
+
311
+ lstbox = tk.Listbox(frmIOMenu, bg="black", exportselection=False, foreground="white", font=("Arial", 14))
312
+
313
+ 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))
314
+
315
+
316
+
317
+ ybar2 = ttk.Scrollbar(frmIOMenu, orient=tk.VERTICAL, command=lstbox.yview)
318
+
319
+ ybar2.grid(row=4, column=9, rowspan=4, sticky=tk.N + tk.S + tk.E, pady=(5,0), padx=(0,10))
320
+
321
+ lstbox.config(yscrollcommand=lambda f, l: ybar2.set(f, l))
322
+
323
+
324
+
325
+ xbar2 = ttk.Scrollbar(frmIOMenu, orient=tk.HORIZONTAL, command=lstbox.xview)
326
+
327
+ xbar2.grid(row=7, column=6, columnspan=4, sticky=tk.S + tk.E + tk.W, padx=(0,10))
328
+
329
+ lstbox.config(xscrollcommand=lambda f, l: xbar2.set(f, l))
330
+
331
+
332
+
333
+ btn_Up = ttk.Button(frmIOMenu, text = "↑")
334
+
335
+ btn_Up.grid(row=5, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S)
336
+
337
+
338
+
339
+ btn_Down = ttk.Button(frmIOMenu, text = "↓")
340
+
341
+ btn_Down.grid(row=6, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S)
342
+
343
+
344
+
345
+ btn_Cancel = ttk.Button(frmIOMenu, text = "←")
346
+
347
+ btn_Cancel.grid(row=7, column=4, columnspan=2, sticky=tk.E + tk.W + tk.N+ tk.S)
348
+
349
+
350
+
351
+ lab_Type = ttk.Label(frmIOMenu, text="こ")
352
+
353
+ lab_Type.grid(row=8, column=0, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=(0,10))
354
+
355
+
356
+
357
+ cmbox_Type = ttk.Combobox(frmIOMenu, height=3, font=("Arial", 16))
358
+
359
+ cmbox_Type.grid(row=9, column=0, columnspan=2, sticky=tk.N + tk.S + tk.E + tk.W, padx=(10, 0))
360
+
361
+
362
+
363
+
364
+
365
+ tree3 = ttk.Treeview(frmIOMenu, show="headings", height=1, style="UTREE.Treeview")
366
+
367
+ tree3.grid(row=10, column=0, columnspan=9, sticky=tk.E + tk.W, padx=(10,0), pady=(5,0))
368
+
369
+
370
+
371
+ btn_Add = ttk.Button(frmIOMenu, text = "せ", command=lambda : force_entrytree3())
372
+
373
+ btn_Add.grid(row=10, column=9, rowspan=2, sticky=tk.N + tk.S + tk.E+ tk.W, padx=(0,10), pady=(5,0))
374
+
375
+
376
+
377
+ xbar3 = ttk.Scrollbar(frmIOMenu, orient=tk.HORIZONTAL, command=tree3.xview)
378
+
379
+ xbar3.grid(row=11, column=0, columnspan=9, sticky=tk.N + tk.S + tk.E + tk.W, padx=(10,0))
380
+
381
+ tree3.config(xscrollcommand=lambda f, l: xbar3.set(f, l))
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+ btn_ReturnMenu = ttk.Button(frmIOMenu, text = "閉じる", command=lambda: change_frame(frmMain))
390
+
391
+ btn_ReturnMenu.grid(row=12, column=8, columnspan=2, sticky=tk.E + tk.W + tk.N + tk.S, pady=(0,10), padx=(0,10))
392
+
393
+
394
+
395
+
396
+
397
+ frmMain.tkraise()
398
+
399
+
400
+
401
+
402
+
403
+ if __name__ == "__main__":
404
+
405
+ root = tk.Tk()
406
+
407
+
408
+
409
+ adjust_windowsize1(root)
410
+
411
+ root.title("ボタン小さくなる")
412
+
413
+
414
+
415
+ root.grid_columnconfigure([0,1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1)
416
+
417
+ root.grid_rowconfigure([0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], weight=1)
418
+
419
+
420
+
421
+ root.resizable(0,0)
422
+
423
+ root.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
424
+
425
+
426
+
427
+ root["cursor"] = "hand2"
428
+
429
+
430
+
431
+ generate_frame(root)
432
+
433
+
434
+
435
+ root.mainloop()
436
+
437
+
18
438
 
19
439
  ```
20
440
 
21
441
 
22
442
 
23
-
24
-
25
- ### 問題解決のため 自分が試したこと
26
-
27
- [こちら](https://cercopes-z.com/Python/stdlib-tkinter-widget-treeview-py.html#method-column)の参考
28
-
29
- カラム単位列幅を指定する手法の記あるも、全体幅を固定値設定できるような記述を 見つけるこができせんでした。
443
+ コード含め 本文10,000文字がリミットので 主旨が短文なってしいました。すみません
30
-
31
-
32
-
33
-
34
-
35
- ### 質問
36
-
37
- ツリービュー全体の列幅を設定することはできないのか?
38
-
39
- つまり....各列幅のトータルから自動で求まるので、各列単位に幅を調整しれければならないのか? をお聞きしたいです。
40
-
41
-
42
-
43
- よろしくお願いします。