回答編集履歴

6

typo 修正 tow -> row

2020/05/07 10:21

投稿

teamikl
teamikl

スコア8817

test CHANGED
@@ -312,7 +312,7 @@
312
312
 
313
313
  @addrow
314
314
 
315
- def tow_message(row, col, **kw):
315
+ def row_message(row, col, **kw):
316
316
 
317
317
  view.message.grid(row=row, column=col(), columnspan=maxcolumn, sticky=tk.NSEW, **kw)
318
318
 

5

init_view_layout関数: columnの値を動的に生成。グリッド内のマジックナンバー排除

2020/05/07 10:21

投稿

teamikl
teamikl

スコア8817

test CHANGED
@@ -238,7 +238,11 @@
238
238
 
239
239
  # これにより、行の入れ替えや追加が容易に管理できるようになります。
240
240
 
241
+
242
+
241
- # 但し、列の追加には弱いです。
243
+ from itertools import count
244
+
245
+ from functools import partial
242
246
 
243
247
 
244
248
 
@@ -248,6 +252,8 @@
248
252
 
249
253
  maxcolumn = 6
250
254
 
255
+ right = lambda column: maxcolumn-column-1
256
+
251
257
  nopad = {"padx":0, "pady": 0}
252
258
 
253
259
 
@@ -256,7 +262,7 @@
256
262
 
257
263
  for row, func in enumerate(rows):
258
264
 
259
- func(row)
265
+ func(row, partial(next, count()), **nopad)
260
266
 
261
267
 
262
268
 
@@ -270,19 +276,19 @@
270
276
 
271
277
  @addrow
272
278
 
273
- def row_header(row):
279
+ def row_header(row, col, **kw):
274
-
280
+
275
- view.labelA.grid(row=row, column=0, **nopad)
281
+ view.labelA.grid(row=row, column=col(), **kw)
276
-
282
+
277
- view.labelB.grid(row=row, column=1, **nopad)
283
+ view.labelB.grid(row=row, column=col(), **kw)
278
-
279
-
280
-
284
+
285
+
286
+
281
- @addrow
287
+ @addrow
282
-
288
+
283
- def row_scrolledtext(row):
289
+ def row_scrolledtext(row, col, **kw):
284
-
290
+
285
- view.text.grid(row=row, column=0, columnspan=maxcolumn, sticky=tk.NSEW, **nopad)
291
+ view.text.grid(row=row, column=col(), columnspan=maxcolumn, sticky=tk.NSEW, **kw)
286
292
 
287
293
  parent.grid_rowconfigure(row, weight=True)
288
294
 
@@ -290,25 +296,25 @@
290
296
 
291
297
  @addrow
292
298
 
293
- def row_center_button(row):
299
+ def row_center_button(row, col, **kw):
294
-
300
+
295
- view.buttonA.grid(row=row, column=0, columnspan=maxcolumn)
301
+ view.buttonA.grid(row=row, column=col(), columnspan=maxcolumn)
296
-
297
-
298
-
302
+
303
+
304
+
299
- @addrow
305
+ @addrow
300
-
306
+
301
- def row_blank_separator(row):
307
+ def row_blank_separator(row, col, **kw):
302
-
308
+
303
- view.separator.grid(row=row, column=0, sticky=tk.NSEW, **nopad)
309
+ view.separator.grid(row=row, column=col(), sticky=tk.NSEW, **kw)
304
-
305
-
306
-
310
+
311
+
312
+
307
- @addrow
313
+ @addrow
308
-
314
+
309
- def row_message(row):
315
+ def tow_message(row, col, **kw):
310
-
316
+
311
- view.message.grid(row=row, column=0, columnspan=maxcolumn, sticky=tk.NSEW, **nopad)
317
+ view.message.grid(row=row, column=col(), columnspan=maxcolumn, sticky=tk.NSEW, **kw)
312
318
 
313
319
  parent.grid_rowconfigure(row, weight=True)
314
320
 
@@ -316,21 +322,21 @@
316
322
 
317
323
  @addrow
318
324
 
319
- def row_bottom_label(row):
325
+ def row_bottom_label(row, col, **kw):
320
-
326
+
321
- view.buttonB.grid(row=row, column=0, sticky=tk.NSEW, **nopad)
327
+ view.buttonB.grid(row=row, column=col(), sticky=tk.NSEW, **kw)
322
-
328
+
323
- view.labelC.grid(row=row, sticky=tk.NSEW, column=1, **nopad)
329
+ view.labelC.grid(row=row, column=col(), sticky=tk.NSEW, **kw)
324
-
325
-
326
-
330
+
331
+
332
+
327
- @addrow
333
+ @addrow
328
-
334
+
329
- def row_footer(row):
335
+ def row_footer(row, col, **kw):
336
+
330
-
337
+ view.buttonD.grid(row=row, column=right(col()), sticky=tk.NSEW, **kw)
338
+
331
- view.buttonC.grid(row=row, column=maxcolumn-2, sticky=tk.NSEW, **nopad)
339
+ view.buttonC.grid(row=row, column=right(col()), sticky=tk.NSEW, **kw)
332
-
333
- view.buttonD.grid(row=row, column=maxcolumn-1, sticky=tk.NSEW, **nopad)
334
340
 
335
341
 
336
342
 

4

コードの実行に支障はありませんが、gridでcolumn=0 明示してなかったのを2か所訂正

2020/05/07 10:19

投稿

teamikl
teamikl

スコア8817

test CHANGED
@@ -292,7 +292,7 @@
292
292
 
293
293
  def row_center_button(row):
294
294
 
295
- view.buttonA.grid(row=row, columnspan=maxcolumn)
295
+ view.buttonA.grid(row=row, column=0, columnspan=maxcolumn)
296
296
 
297
297
 
298
298
 
@@ -300,7 +300,7 @@
300
300
 
301
301
  def row_blank_separator(row):
302
302
 
303
- view.separator.grid(row=row, sticky=tk.NSEW, **nopad)
303
+ view.separator.grid(row=row, column=0, sticky=tk.NSEW, **nopad)
304
304
 
305
305
 
306
306
 

3

packでの例を追記

2020/05/07 10:04

投稿

teamikl
teamikl

スコア8817

test CHANGED
@@ -373,3 +373,93 @@
373
373
 
374
374
 
375
375
  ![イメージ説明](6bc65ea3cb9918edbe6e5b14cf1b573b.png)
376
+
377
+
378
+
379
+ ----
380
+
381
+ 追記: packでの実装例
382
+
383
+
384
+
385
+ 最小限のコードになりますが、簡単な方法では、
386
+
387
+ 「横に並べる」部分だけを別フレームに切り出し、
388
+
389
+ `side="left"` を指定して横並びにします。
390
+
391
+
392
+
393
+ ```python
394
+
395
+ import tkinter as tk
396
+
397
+ from tkinter import ttk
398
+
399
+ from tkinter.scrolledtext import ScrolledText
400
+
401
+
402
+
403
+
404
+
405
+ def main():
406
+
407
+ root = tk.Tk()
408
+
409
+
410
+
411
+ frameA = ttk.Frame(root)
412
+
413
+ frameA.pack(fill=tk.BOTH)
414
+
415
+ ttk.Label(frameA, text="Label", width=10).pack(side=tk.LEFT)
416
+
417
+ ttk.Label(frameA, text="ListBox?", width=10).pack(side=tk.LEFT)
418
+
419
+
420
+
421
+ text = ScrolledText(root, height=10)
422
+
423
+ text.pack(fill=tk.BOTH, expand=1)
424
+
425
+ text.insert(tk.END, "ScrolledText")
426
+
427
+
428
+
429
+ ttk.Button(root, text="Button").pack()
430
+
431
+ ttk.Label(root).pack()
432
+
433
+ tk.Message(root, text="Message", width=200, bg="black", fg="white").pack(fill=tk.BOTH, expand=1)
434
+
435
+
436
+
437
+ frameB = ttk.Frame(root)
438
+
439
+ frameB.pack(fill=tk.BOTH)
440
+
441
+ ttk.Button(frameB, text="Button", width=10).pack(side=tk.LEFT)
442
+
443
+ ttk.Label(frameB, text="Label", width=10).pack(side=tk.LEFT)
444
+
445
+
446
+
447
+ ttk.Button(root, text="Quit", command=root.destroy).pack(side=tk.RIGHT)
448
+
449
+ ttk.Button(root, text="Button").pack(side=tk.RIGHT)
450
+
451
+
452
+
453
+ root.mainloop()
454
+
455
+
456
+
457
+
458
+
459
+ if __name__ == '__main__':
460
+
461
+ main()
462
+
463
+
464
+
465
+ ```

2

typo修正。実行に影響はなし。

2020/05/07 08:21

投稿

teamikl
teamikl

スコア8817

test CHANGED
@@ -306,7 +306,7 @@
306
306
 
307
307
  @addrow
308
308
 
309
- def tow_message(row):
309
+ def row_message(row):
310
310
 
311
311
  view.message.grid(row=row, column=0, columnspan=maxcolumn, sticky=tk.NSEW, **nopad)
312
312
 

1

内部関数の関数名修正。create ではなかった。

2020/05/07 08:13

投稿

teamikl
teamikl

スコア8817

test CHANGED
@@ -270,7 +270,7 @@
270
270
 
271
271
  @addrow
272
272
 
273
- def create_header(row):
273
+ def row_header(row):
274
274
 
275
275
  view.labelA.grid(row=row, column=0, **nopad)
276
276
 
@@ -280,7 +280,7 @@
280
280
 
281
281
  @addrow
282
282
 
283
- def scrolledtext_layout(row):
283
+ def row_scrolledtext(row):
284
284
 
285
285
  view.text.grid(row=row, column=0, columnspan=maxcolumn, sticky=tk.NSEW, **nopad)
286
286
 
@@ -290,7 +290,7 @@
290
290
 
291
291
  @addrow
292
292
 
293
- def create_center_button(row):
293
+ def row_center_button(row):
294
294
 
295
295
  view.buttonA.grid(row=row, columnspan=maxcolumn)
296
296
 
@@ -298,7 +298,7 @@
298
298
 
299
299
  @addrow
300
300
 
301
- def create_blank_separator(row):
301
+ def row_blank_separator(row):
302
302
 
303
303
  view.separator.grid(row=row, sticky=tk.NSEW, **nopad)
304
304
 
@@ -306,7 +306,7 @@
306
306
 
307
307
  @addrow
308
308
 
309
- def create_message(row):
309
+ def tow_message(row):
310
310
 
311
311
  view.message.grid(row=row, column=0, columnspan=maxcolumn, sticky=tk.NSEW, **nopad)
312
312
 
@@ -316,7 +316,7 @@
316
316
 
317
317
  @addrow
318
318
 
319
- def create_bottom_label(row):
319
+ def row_bottom_label(row):
320
320
 
321
321
  view.buttonB.grid(row=row, column=0, sticky=tk.NSEW, **nopad)
322
322
 
@@ -326,7 +326,7 @@
326
326
 
327
327
  @addrow
328
328
 
329
- def create_footer(row):
329
+ def row_footer(row):
330
330
 
331
331
  view.buttonC.grid(row=row, column=maxcolumn-2, sticky=tk.NSEW, **nopad)
332
332