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


#========================================
# CounterUI クラス
#========================================
class CounterUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('カウンタ')
        self.root.minsize(630,465)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=600, height=75)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, 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=115, 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=470, 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=470, y=30, width=120, height=19)
        #----------------------------------------
        # カウンタ設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_settings = tk.LabelFrame(self.root, text="カウンタ設定")
        self.groupbox_counter_settings.place(x=15, y=90, width=600, height=50)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_settings, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_channel_no_var = tk.StringVar(self.groupbox_counter_settings)
        self.textbox_channel_no_var.set("0")
        self.textbox_channel_no = tk.Entry(self.groupbox_counter_settings, textvariable=self.textbox_channel_no_var, justify=tk.RIGHT)
        self.textbox_channel_no.place(x=115, y=5, width=100, height=19)
        #--------------------
        # 設定のButtonを作成
        #--------------------
        self.button_setting = tk.Button(self.groupbox_counter_settings, text='設定...')
        self.button_setting.place(x=470, y=5, width=120, height=19)
        #----------------------------------------
        # カウンタプリセットのLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_preset = tk.LabelFrame(self.root, text="カウンタプリセット")
        self.groupbox_counter_preset.place(x=15, y=145, width=600, height=50)
        #--------------------
        # プリセット値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_preset, text='プリセット値 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # プリセット値のEntryを作成
        #--------------------
        self.textbox_preset_data_var = tk.StringVar(self.groupbox_counter_preset)
        self.textbox_preset_data_var.set("0")
        tk.Entry(self.groupbox_counter_preset, textvariable=self.textbox_preset_data_var, justify=tk.RIGHT).place(x=115, y=5, width=100, height=19)
        #--------------------
        # AioPresetCntのButtonを作成
        #--------------------
        self.button_aio_preset_cnt = tk.Button(self.groupbox_counter_preset, text='AioPresetCnt')
        self.button_aio_preset_cnt.place(x=470, y=5, width=120, height=19)
        #----------------------------------------
        # カウンタ動作のLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_operation = tk.LabelFrame(self.root, text="カウンタ動作")
        self.groupbox_counter_operation.place(x=15, y=200, width=600, height=125)
        #--------------------
        # カウント値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_operation, text='カウント値 : ', anchor='nw').place(x=15, y=20, width=90, height=19)
        #--------------------
        # カウント値のEntryを作成
        #--------------------
        self.textbox_count_var = tk.StringVar(self.groupbox_counter_operation)
        self.textbox_count_var.set("0")
        tk.Entry(self.groupbox_counter_operation, textvariable=self.textbox_count_var, justify=tk.RIGHT).place(x=115, y=20, width=320, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_operation, text='ステータス : ', anchor='nw').place(x=15, y=45, width=90, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_status_var = tk.StringVar(self.groupbox_counter_operation)
        tk.Entry(self.groupbox_counter_operation, textvariable=self.textbox_status_var).place(x=115, y=45, width=320, height=19)
        #--------------------
        # 比較一致回数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_operation, text='比較一致回数 : ', anchor='nw').place(x=15, y=70, width=90, height=19)
        #--------------------
        # 比較一致回数のEntryを作成
        #--------------------
        self.textbox_event_count_var = tk.StringVar(self.groupbox_counter_operation)
        self.textbox_event_count_var.set("0")
        tk.Entry(self.groupbox_counter_operation, textvariable=self.textbox_event_count_var, justify=tk.RIGHT).place(x=115, y=70, width=320, height=19)
        #--------------------
        # カウント開始のButtonを作成
        #--------------------
        self.button_count_start = tk.Button(self.groupbox_counter_operation, text='カウント開始')
        self.button_count_start.place(x=470, y=5, width=120, height=19)
        #--------------------
        # AioGetCntCountのButtonを作成
        #--------------------
        self.button_aio_get_cnt_count = tk.Button(self.groupbox_counter_operation, text='AioGetCntCount')
        self.button_aio_get_cnt_count.place(x=470, y=30, width=120, height=19)
        #--------------------
        # AioGetCntStatusのButtonを作成
        #--------------------
        self.button_aio_get_cnt_status = tk.Button(self.groupbox_counter_operation, text='AioGetCntStatus')
        self.button_aio_get_cnt_status.place(x=470, y=55, width=120, height=19)
        #--------------------
        # カウント停止のButtonを作成
        #--------------------
        self.button_count_stop = tk.Button(self.groupbox_counter_operation, text='カウント停止')
        self.button_count_stop.place(x=470, y=80, width=120, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=330, width=600, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=470, y=5, width=120, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=395, 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=132, y=395, width=320, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=487, y=420, width=120, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_reset_device,
                       command_channel_no,
                       command_settings,
                       command_aio_preset_cnt,
                       command_start,
                       command_aio_get_cnt_count,
                       command_aio_get_cnt_status,
                       command_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.textbox_channel_no.bind("<KeyRelease>", command_channel_no)
        #----------------------------------------
        # 「設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_setting.configure(command=command_settings)
        #----------------------------------------
        # 「AioPresetCnt」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_preset_cnt.configure(command=command_aio_preset_cnt)
        #----------------------------------------
        # 「カウント開始」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_count_start.configure(command=command_start)
        #----------------------------------------
        # 「AioGetCntCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_cnt_count.configure(command=command_aio_get_cnt_count)
        #----------------------------------------
        # 「AioGetCntStatus」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_get_cnt_status.configure(command=command_aio_get_cnt_status)
        #----------------------------------------
        # 「カウント停止」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_count_stop.configure(command=command_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()