作業の全体像

tkinter.ttkでのGUIシステム開発では、以下のステップを踏むことでカスタムテーマを作成します。

  1. tkinterのインポート
  2. ttkのStyleクラスのインスタンスを作成
  3. Styleクラスのメソッドを使用してテーマの設定

まず、tkinter.ttkのStyleクラスを利用してGUIの見た目を変更します。ウィジェットごとに異なるスタイルを適用したり、ウィジェットの特定の状態に対して特定のスタイルを適用したりできます。

以下に、tkinter.ttkで利用可能なウィジェットと、それぞれのウィジェットで設定可能なプロパティをリストアップします。

ウィジェット 設定可能なプロパティ
TButton background, bordercolor, darkcolor, foreground, lightcolor, padding, relief, shiftrelief, width
TCheckbutton anchor, background, bordercolor, compound, darkcolor, font, foreground, height, image, indicatorbackground, indicatorforeground, indicatorsize, lightcolor, padding, relief, shiftrelief, spacing1, spacing2, spacing3, stipple, text, underline, width, wraplength
TCombobox arrowcolor, arrowrelief, bordercolor, darkcolor, fieldbackground, font, foreground, height, lightcolor, padding, postoffset, relief, shiftrelief, width
TEntry background, bordercolor, darkcolor, fieldbackground, foreground, insertcolor, insertofftime, insertontime, insertwidth, lightcolor, padding, relief, shiftrelief, width
TLabel anchor, background, bordercolor, compound, darkcolor, font, foreground, height, image, lightcolor, padding, relief, shiftrelief, spacing1, spacing2, spacing3, stipple, text, underline, width, wraplength
TMenubutton anchor, arrowcolor, background, bordercolor, compound, darkcolor, font, foreground, height, image, lightcolor, padding, relief, shiftrelief, spacing1, spacing2, spacing3, stipple, text, underline, width, wraplength
TRadiobutton anchor, background, bordercolor, compound, darkcolor, font, foreground, height, image, indicatorbackground, indicatorforeground, indicatorsize, lightcolor, padding, relief, shiftrelief, spacing1, spacing2, spacing3, stipple, text, underline, width, wraplength
TScale background, bordercolor, darkcolor, length, lightcolor, orient, relief, shiftrelief, sliderlength, sliderrelief, troughcolor
TScrollbar background, bordercolor, darkcolor, gripcount, lightcolor, orient, relief, shiftrelief, sliderrelief, troughcolor
TSpinbox background, bordercolor, darkcolor, fieldbackground, font, foreground, increment, lightcolor, padding, relief, shiftrelief, width
TNotebook background, bordercolor, darkcolor, lightcolor, padding, relief, shiftrelief, tabmargins, tabposition
Treeview background, bordercolor, darkcolor, fieldbackground, font, foreground, height, lightcolor, padding, relief, shiftrelief, rowheight, selectbackground, selectforeground, show

プロジェクトフォルダの構造

以下に、プロジェクトフォルダの構造を示します。

📂my_desktop_app/
┣ 📜main.py
┣ 📂app/
┃ ┣ 📜__init__.py
┃ ┣ 📜gui.py
┃ ┣ 📜style_theme.py  # 新しく作成するファイル
┃ ┗ 📜app_logic.py
┣ 📂test/
┃ ┗ 📜test_app_logic.py
┣ 📂assets/
┃ ┣ 📂images/
┃ ┗ 📂xml/
┃   ┗ 📜gui.xml
┣ 📂database/
┃ ┣ 📜database.py
┃ ┣ 📜sql.py
┃ ┣ 📜table1.accdb
┃ ┗ 📜table2.accdb
┗ 📜requirements.txt

この構成の中で、カスタムテーマの設定を行う新しいファイルstyle_theme.pyappフォルダ内に作成します。

プログラムのアウトライン

以下に、新しく作成するファイルstyle_theme.pyのアウトラインを示します。このファイルには、カスタムテーマの設定を行うクラスCustomThemeを定義します。

from tkinter import ttk

class CustomTheme:
    """
    tkinterのttkでのカスタムテーマを作成するクラス。

    Attributes:
        style (ttk.Style): ttkのStyleオブジェクト。
    """

    def __init__(self):
        """
        CustomThemeクラスの初期化メソッド。Styleオブジェクトのインスタンスを作成する。
        """
        self.style = None

    def apply_theme(self):
        """
        カスタムテーマを適用するメソッド。
        """
        pass

    def configure_widget(self, widget_name: str):
        """
        各ウィジェットに対するスタイルの設定を行うメソッド。

        Args:
            widget_name (str): スタイルを設定するウィジェットの名前。
        """
        pass

プログラムの詳細

次に、クラス内の各メソッドの詳細について示します。各メソッドにはコメントを記述して、プログラムの動作を詳細に説明します。

  1. __init__メソッド
def __init__(self):
    """
    CustomThemeクラスの初期化メソッド。Styleオブジェクトのインスタンスを作成する。
    """
    self.style = ttk.Style()
  1. apply_themeメソッド