はじめに
前回の番外編11では、ビュー層の最初のファイルとして calendar_view.py のスタイル改善を行いました。今回は、メインウィンドウのレイアウトを担当する main_view.py を取り上げます。
このファイルは3つの子View(CalendarView / NoteView / ScheduleView)を生成して PanedWindow に配置するだけのシンプルな構成です。改善点も軽微ですが、これまでの番外編で整理したルールを一つひとつ丁寧に適用していきます。
1. 改善箇所
まず、改善前のコードを確認します。
改善前
""" メインウィンドウのレイアウトを担当するViewクラス。 3つの子Viewを生成してPanedWindowに配置する。 """ import tkinter as tk from tkinter import ttk from config import APP_NAME, WIN_SIZE from views.calendar_view import CalendarView from views.note_view import NoteView from views.schedule_view import ScheduleView class MainView: """ アプリケーション全体のウィンドウレイアウトを管理するクラス。 CalendarView / NoteView / ScheduleView の親として機能する。 """ def __init__(self, root: tk.Tk, colors: dict): """ :param root: Tkinter ルートウィンドウ :param colors: テーマカラー辞書 """ root.title(APP_NAME) root.geometry(WIN_SIZE) # ── 外側: 左右分割 PanedWindow ────────────────────────────── pw_horizontal = ttk.Panedwindow(root, orient=tk.HORIZONTAL) pw_horizontal.pack(fill=tk.BOTH, expand=True) # ── 左側: 上下分割 PanedWindow ────────────────────────────── pw_vertical_left = ttk.Panedwindow(pw_horizontal, orient=tk.VERTICAL) pw_horizontal.add(pw_vertical_left, weight=1) # カレンダーView(左上) self.calendar_view = CalendarView(pw_vertical_left, colors) pw_vertical_left.add(self.calendar_view.container, weight=1) # メモView(左下) self.note_view = NoteView(pw_vertical_left, colors) pw_vertical_left.add(self.note_view.container, weight=1) # ── 右側: スケジュールView ─────────────────────────────────── frame_right = tk.Frame(pw_horizontal, bg=colors["frame_bg"]) pw_horizontal.add(frame_right, weight=1) self.schedule_view = ScheduleView(frame_right, colors)
改善ポイントは以下の3点です。
2. 改善点① __init__ の型ヒントと -> None がない
問題
colors の型が dict と不具体であり、-> None も省略されています。これまでのビュー層・モデル層の改善と共通のパターンです。
# ❌ 現状 def __init__(self, root: tk.Tk, colors: dict):
改善方法
dict の中身まで明記し、-> None を追加します。
# ✅ 推奨 def __init__(self, root: tk.Tk, colors: dict[str, str]) -> None:
3. 改善点② docstring が Sphinx スタイル
問題
クラスの docstring が複数行ブロック形式(""" の直後に改行)、__init__ が :param: の Sphinx スタイルで書かれています。番外編4以降で統一してきた Google スタイルに揃えます。
# ❌ 現状:クラス docstring が複数行ブロック形式 class MainView: """ アプリケーション全体のウィンドウレイアウトを管理するクラス。 CalendarView / NoteView / ScheduleView の親として機能する。 """ # ❌ 現状:__init__ が Sphinxスタイル def __init__(self, root: tk.Tk, colors: dict): """ :param root: Tkinter ルートウィンドウ :param colors: テーマカラー辞書 """
改善方法
クラスの docstring は1行目に概要を書き、空行を挟んで補足を続けます。__init__ は Google スタイルの Args: セクションに変更します。
# ✅ 推奨:Googleスタイル class MainView: """アプリケーション全体のウィンドウレイアウトを管理するクラス。 CalendarView / NoteView / ScheduleView の親として機能する。 """ def __init__(self, root: tk.Tk, colors: dict[str, str]) -> None: """メインウィンドウのレイアウトを構築する。 Args: root: Tkinter ルートウィンドウ。 colors: テーマカラー辞書。 """
4. 改善点③ colors["frame_bg"] の直接参照にコメントがない
問題
このファイル内で colors を直接参照しているのは frame_right の背景色指定の1箇所だけです。他の設定はすべて子Viewに colors を渡して委ねているため、なぜここだけ直接参照しているのかが一見わかりません。
# ❌ 現状:なぜ直接参照するのか意図が不明 frame_right = tk.Frame(pw_horizontal, bg=colors["frame_bg"])
改善方法
コメントで意図を明示します。
# ✅ 推奨:コメントで意図を明示 # ScheduleView のコンテナとして右ペインを用意(背景色を統一) frame_right = tk.Frame(pw_horizontal, bg=colors["frame_bg"])
5. 改善後の完成コード
3点の改善を適用した main_view.py の完成形は以下のとおりです。
改善後
""" メインウィンドウのレイアウトを担当するViewクラス。 3つの子Viewを生成してPanedWindowに配置する。 """ import tkinter as tk from tkinter import ttk from config import APP_NAME, WIN_SIZE from views.calendar_view import CalendarView from views.note_view import NoteView from views.schedule_view import ScheduleView class MainView: """アプリケーション全体のウィンドウレイアウトを管理するクラス。 CalendarView / NoteView / ScheduleView の親として機能する。 """ def __init__(self, root: tk.Tk, colors: dict[str, str]) -> None: """メインウィンドウのレイアウトを構築する。 Args: root: Tkinter ルートウィンドウ。 colors: テーマカラー辞書。 """ root.title(APP_NAME) root.geometry(WIN_SIZE) # ── 外側: 左右分割 PanedWindow ────────────────────────────── pw_horizontal = ttk.Panedwindow(root, orient=tk.HORIZONTAL) pw_horizontal.pack(fill=tk.BOTH, expand=True) # ── 左側: 上下分割 PanedWindow ────────────────────────────── pw_vertical_left = ttk.Panedwindow(pw_horizontal, orient=tk.VERTICAL) pw_horizontal.add(pw_vertical_left, weight=1) # カレンダーView(左上) self.calendar_view = CalendarView(pw_vertical_left, colors) pw_vertical_left.add(self.calendar_view.container, weight=1) # メモView(左下) self.note_view = NoteView(pw_vertical_left, colors) pw_vertical_left.add(self.note_view.container, weight=1) # ── 右側: スケジュールView ─────────────────────────────────── # ScheduleView のコンテナとして右ペインを用意(背景色を統一) frame_right = tk.Frame(pw_horizontal, bg=colors["frame_bg"]) pw_horizontal.add(frame_right, weight=1) self.schedule_view = ScheduleView(frame_right, colors)
6. pylint での確認と .pylintrc の追加設定
修正後にプロジェクトルートから pylint を実行します。
% PYTHONPATH=. pylint views/main_view.py ************* Module main_view views/main_view.py:14:0: R0903: Too few public methods (0/2) (too-few-public-methods) ------------------------------------------------------------------ Your code has been rated at 9.52/10 (previous run: 0.00/10, +9.52)
10.00/10 にならず、R0903 の警告が出ました。
警告の原因
MainView クラスには __init__ のみでパブリックメソッドがありません。pylint のデフォルトでは パブリックメソッドが2つ以上必要 という基準があるため警告が発生しています。
しかし MainView はレイアウト構築専用のクラスであり、calendar_view / note_view / schedule_view というパブリック属性を外部に公開する設計です。メソッドがないのは設計上の意図であり、警告を解消するためだけにダミーメソッドを追加するのは本末転倒です。
対処方法:.pylintrc に min-public-methods を追加
[DESIGN] max-args=6 max-positional-arguments=6 min-public-methods=0
再度 pylint を実行します。
% PYTHONPATH=. pylint views/main_view.py -------------------------------------------------------------------- Your code has been rated at 10.00/10 (previous run: 9.52/10, +0.48)
10.00/10 を達成できました。
7. .pylintrc の現時点での全体像
これまでの番外編で追加してきた設定をまとめると以下のとおりです。
[DESIGN] max-args=6 max-positional-arguments=6 min-public-methods=0
| 設定項目 | 値 | 追加した理由 |
|---|---|---|
max-args |
6 |
schedule_model.py の save メソッド(番外編10) |
max-positional-arguments |
6 |
同上(R0913 と R0917 は独立した設定) |
min-public-methods |
0 |
MainView のようなレイアウト専用クラス(本記事) |
なお、番外編8で config モジュールの import エラーを回避するために PYTHONPATH=. を付けて実行する運用としています。PYTHONPATH=. を付けることでプロジェクトルートが Python のモジュール検索パスに追加されるため、.pylintrc に init-hook の設定は不要です。init-hook が必要になるのは PYTHONPATH=. を付けずに pylint を実行する場合に限られます。
おわりに
今回は main_view.py を対象に、__init__ の型ヒント強化・docstring の Google スタイルへの統一・コメントの追加という3点の改善を行いました。
またコードの修正とは別に、pylint の R0903(too-few-public-methods)という新たな警告への対処として .pylintrc に min-public-methods=0 を追加しました。「パブリックメソッドがないのは設計上の意図」という場合には、ダミーメソッドを追加するのではなく .pylintrc でその旨を明示するのが正しい対処です。
次回は、メモエリアの表示を担当する note_view.py のスタイル改善を進めていきます。