import sqlite3
import Tkinter as tk
import ttk
# SQLiteデータベースに接続
conn = sqlite3.connect('log_data.db')
cursor = conn.cursor()
# Tkinterウィンドウを作成
window = tk.Tk()
window.title('ERR_LOG Viewer')
# 更新ボタンを作成
def update_treeview():
load_data()
update_button = tk.Button(window, text='更新', command=update_treeview)
# 更新ボタンを左下に配置
update_button.pack(side='bottom', anchor='sw') # ボタンを左下 (southwest) に配置
# ツリービューを作成してヘッダーを設定
tree = ttk.Treeview(window, columns=('Timestamp', 'Event Type', 'Additional Info'), show='headings')
tree.heading('Timestamp', text='Report_time', command=lambda col='Timestamp': sort_column(tree, col, False))
tree.heading('Event Type', text='Event Type', command=lambda col='Event Type': sort_column(tree, col, False))
tree.heading('Additional Info', text='Server_Info', command=lambda col='Additional Info': sort_column(tree, col, False))
tree.pack(fill='both', expand=True) # ツリービューをウィンドウにフィットさせる
# ツリービューの 'Additional Info' 列にスクロールバーを追加
vsb = ttk.Scrollbar(window, orient='vertical', command=tree.yview)
vsb.pack(side='right', fill='y')
tree.configure(yscrollcommand=vsb.set)
# 列の幅を設定
tree.column('Timestamp', width=130) # 幅130ピクセル
tree.column('Event Type', width=80) # 幅 80ピクセル
tree.column('Additional Info', width=600) # 幅600ピクセル
# データベースからデータを取得して表示
def load_data():
cursor.execute('SELECT timestamp, event_type, additional_info FROM logs')
data = cursor.fetchall()
# ツリービューをクリア
for item in tree.get_children():
tree.delete(item)
# 新しいデータを挿入
for row in data:
tree.insert('', 'end', values=row)
load_data() # 初回のデータ読み込み
# 列のソート関数を定義
def sort_column(tv, col, reverse):
l = [(tv.set(k, col), k) for k in tv.get_children('')]
l.sort(reverse=reverse)
for index, (val, k) in enumerate(l):
tv.move(k, '', index)
tv.heading(col, command=lambda col=col: sort_column(tv, col, not reverse))
# メインループを開始
window.mainloop()
# データベース接続を閉じる
conn.close()