﻿#================================================================
#================================================================
# API-AIO(WDM)
# タイマーサンプル
# main UIファイル
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# TimerUI クラス
#========================================
class TimerUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('タイマー')
        self.root.minsize(515,460)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="初期化処理")
        self.groupbox_init.place(x=15, y=10, width=487, height=75)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.groupbox_init)
        self.textbox_device_name_var.set('AIO000')
        tk.Entry(self.groupbox_init, textvariable=self.textbox_device_name_var).place(x=110, y=5, width=100, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=350, y=5, width=120, height=19)
        #--------------------
        # AioResetDeviceのButtonを作成
        #--------------------
        self.button_aio_reset_device = tk.Button(self.groupbox_init, text='AioResetDevice')
        self.button_aio_reset_device.place(x=350, y=30, width=120, height=19)
        #----------------------------------------
        # インターバルタイマーのLabelFrameを作成
        #----------------------------------------
        self.groupbox_interval = tk.LabelFrame(self.root, text="インターバルタイマー")
        self.groupbox_interval.place(x=15, y=90, width=487, height=100)
        #--------------------
        # インターバルのLabelを作成
        #--------------------
        tk.Label(self.groupbox_interval, text='インターバル : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # インターバルのEntryを作成
        #--------------------
        self.textbox_interval_var = tk.StringVar(self.groupbox_interval)
        self.textbox_interval_var.set("1000")
        tk.Entry(self.groupbox_interval, textvariable=self.textbox_interval_var, justify=tk.RIGHT).place(x=110, y=5, width=100, height=19)
        #--------------------
        # インターバル単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_interval, text='[μsec]', anchor='nw').place(x=220, y=5, width=90, height=19)
        #--------------------
        # 通知回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_interval, text='通知回数 : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # 通知回数のEntryを作成
        #--------------------
        self.textbox_event_count_var = tk.StringVar(self.groupbox_interval)
        self.textbox_event_count_var.set("0")
        tk.Entry(self.groupbox_interval, textvariable=self.textbox_event_count_var, justify=tk.RIGHT).place(x=110, y=30, width=100, height=19)
        #--------------------
        # 動作開始のButtonを作成
        #--------------------
        self.button_interval_start = tk.Button(self.groupbox_interval, text='動作開始')
        self.button_interval_start.place(x=350, y=0, width=120, height=19)
        #--------------------
        # 動作停止のButtonを作成
        #--------------------
        self.button_interval_stop = tk.Button(self.groupbox_interval, text='動作停止')
        self.button_interval_stop.place(x=350, y=55, width=120, height=19)
        #----------------------------------------
        # ストップウォッチタイマーのLabelFrameを作成
        #----------------------------------------
        self.groupbox_stopwatch = tk.LabelFrame(self.root, text="ストップウォッチタイマー")
        self.groupbox_stopwatch.place(x=15, y=195, width=487, height=125)
        #--------------------
        # 経過時間のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stopwatch, text='経過時間 : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # 経過時間のEntryを作成
        #--------------------
        self.textbox_lap_time_var = tk.StringVar(self.groupbox_stopwatch)
        self.textbox_lap_time_var.set("0")
        tk.Entry(self.groupbox_stopwatch, textvariable=self.textbox_lap_time_var, justify=tk.RIGHT).place(x=110, y=30, width=100, height=19)
        #--------------------
        # 経過時間単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stopwatch, text='[msec]', anchor='nw').place(x=220, y=30, width=90, height=19)
        #--------------------
        # 動作開始のButtonを作成
        #--------------------
        self.button_watch_start = tk.Button(self.groupbox_stopwatch, text='動作開始')
        self.button_watch_start.place(x=350, y=5, width=120, height=19)
        #--------------------
        # AioLapTmCountのButtonを作成
        #--------------------
        self.button_aio_lap_tm_count = tk.Button(self.groupbox_stopwatch, text='AioLapTmCount')
        self.button_aio_lap_tm_count.place(x=350, y=30, width=120, height=19)
        #--------------------
        # AioResetTmCountのButtonを作成
        #--------------------
        self.button_aio_reset_tm_count = tk.Button(self.groupbox_stopwatch, text='AioResetTmCount')
        self.button_aio_reset_tm_count.place(x=350, y=55, width=120, height=19)
        #--------------------
        # 動作停止のButtonを作成
        #--------------------
        self.button_watch_stop = tk.Button(self.groupbox_stopwatch, text='動作停止')
        self.button_watch_stop.place(x=350, y=80, width=120, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=325, width=487, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=350, y=5, width=120, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=390, width=90, height=19)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_return_string_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=127, y=390, width=220, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=367, y=415, width=120, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_interval_start,
                       command_interval_stop,
                       command_watch_start,
                       command_aio_lap_tm_count,
                       command_aio_reset_tm_count,
                       command_watch_stop,
                       command_aio_exit,
                       command_close):
        #----------------------------------------
        # 「AioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # 「AioResetDevice」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_reset_device.configure(command=command_aio_reset_device)
        #----------------------------------------
        # インターバルタイマー「動作開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_interval_start.configure(command=command_interval_start)
        #----------------------------------------
        # インターバルタイマー「動作停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_interval_stop.configure(command=command_interval_stop)
        #----------------------------------------
        # ストップウォッチタイマー「動作開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_watch_start.configure(command=command_watch_start)
        #----------------------------------------
        # 「AioLapTmCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_lap_tm_count.configure(command=command_aio_lap_tm_count)
        #----------------------------------------
        # 「AioResetTmCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_reset_tm_count.configure(command=command_aio_reset_tm_count)
        #----------------------------------------
        # 「ストップウォッチタイマー「動作停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_watch_stop.configure(command=command_watch_stop)
        #----------------------------------------
        # 「AioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_exit.configure(command=command_aio_exit)
        #----------------------------------------
        # 「閉じる」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_close.configure(command=command_close)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

    #========================================
    # メインイベントループを実行
    #========================================
    def run(self):
        self.root.mainloop()