memopy

pythonで作ってみました的なブログ

ttk Treeview Headingのfontを変更する方法

ttk Treeview Headingのfontを変更する方法

ツリービュー ヘッダーのフォントを変更するのに苦労したのでまとめる。
f:id:memopy:20170604091854p:plain

styleクラスを使用すると、ウィジェットの全てのクラスに対して一括でオプションを設定できる。

import tkinter as ttk
style = ttk.Style()
# configure(オプションを一括で適用するウィジェットのクラス名,オプション・・・)
# ※今回は、フォントサイズのみ14に変更する例
style.configure("Treeview.Heading",font=("",14))

ウィジェットのクラス名指定の例

フォントサイズを14に変更するクラスの範囲を指定する例

# 全てのウィジェット
style.configure(".",font=("",14))
# Treeviewの全部
style.configure("Treeview",font=("",14))
# TreeviewのHeading部分のみ
style.configure("Treeview.Heading",font=("",14))


スタイルクラスを用いた、ページ上部のGUIスクリプト

import tkinter as tk
import tkinter.ttk as ttk

# ルート画面の作成
root = tk.Tk()

# スタイルの設定
style = ttk.Style()
# TreeViewの全部に対して、フォントサイズの変更
style.configure("Treeview",font=("",12))
# TreeViewのHeading部分に対して、フォントサイズの変更と太字の設定
style.configure("Treeview.Heading",font=("",14,"bold"))

# ツリービュー(表)の作成
tree = ttk.Treeview(root)
# 各列に対してインデックスを付ける
tree["columns"]=(1,2,3)
# ツリービューの非表示
tree["show"]="headings"
# 各列に対する設定(
tree.column(1, width=100)
tree.column(2, width=100)
tree.column(3, width=100)
# 各列のヘッダーの設定
tree.heading(1, text="日付")
tree.heading(2, text="内訳")
tree.heading(3, text="金額")
# レコードの挿入
tree.insert("" ,"end",values=("2017/5/1","食費",3500))

tree.pack()
root.mainloop()