第6章:ヘッダー右クリックでフィルタ(Excel風ポップアップ)

python

目的

ヘッダーを右クリックするとポップアップを表示し、列ごとのフィルタを設定できるようにします。

仕様

  • 非数値列:Values(ユニーク値)タブでチェックボックス選択
  • 数値列:Values タブは非表示。Number Filter タブで <=, <, ==, >=, > と閾値を指定
  • OK でフィルタを保存し、全列に対して AND で適用
  • フィルタ中の列ヘッダーには [F] を表示

数値列判定(ヘルパー)

def _is_numeric_column(self, col: str) -> bool:
    idx = self.data_columns.index(col)
    saw = False
    for _, row in self.data_rows:
        v = row[idx] if idx < len(row) else ""
        if v == "":  # 空は無視
            continue
        try:
            float(v)
            saw = True
        except Exception:
            return False
    return saw

フィルタ適用(AND で束ねる)

def apply_filter(self):
    base_rows = self.data_rows
    if self.column_filters:
        rows1 = []
        for ln, r in base_rows:
            ok = True
            for col, cfg in self.column_filters.items():
                idx = self.data_columns.index(col)
                val = r[idx] if idx < len(r) else ""
                if "values" in cfg and cfg["values"] is not None:
                    if val not in cfg["values"]:
                        ok = False; break
                if ok and ("num_op" in cfg and cfg["num_op"] is not None):
                    try: x = float(val)
                    except Exception: ok = False; break
                    op, th = cfg["num_op"], cfg.get("num_val")
                    if th is None: ok = False; break
                    if op == "<=" and not (x <= th): ok = False; break
                    if op == "<"  and not (x <  th): ok = False; break
                    if op == "==" and not (x == th): ok = False; break
                    if op == ">=" and not (x >= th): ok = False; break
                    if op == ">"  and not (x >  th): ok = False; break
            if ok:
                rows1.append((ln, r))
    else:
        rows1 = list(base_rows)
    self.current_rows = rows1
    self._populate_trees(self.data_columns, self.current_rows)

前へ → 第5章
次へ → 第7章:操作性を高める

pythontkinter
スポンサーリンク
この記事を書いた人

運営者について

当サイトは、個人が運営する学習・記録ブログです。

AI・データサイエンス・自動化を中心に、
Python、G検定・DS検定の学習内容や、
実際に試しながら整理した知識をまとめています。

特定の企業や団体に属さない個人サイトとして、
学習過程で得た気づきや判断の整理を目的に運営しています。

「知識はあるが、どう使えばよいか分からない」
「情報が多く、判断に迷ってしまう」
といった状態を減らすことを目的に発信しています。

専門家として教える立場ではなく、
自分自身がつまずき、試し、整理してきた過程をそのまま共有するスタイルです。

用語の暗記やテクニックの紹介よりも、
・なぜそう考えるのか
・どの順番で判断するのか
・どこで迷いやすいのか
といった思考の整理を重視しています。

学び場をフォローする
学び場をフォローする
タイトルとURLをコピーしました