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


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

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理
        #----------------------------------------
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.root, text='デバイス名 : ', anchor='nw').place(x=5, y=5, width=75, height=20)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        self.textbox_device_name_var= tk.StringVar(self.root)
        self.textbox_device_name_var.set("CNT000")
        tk.Entry(self.root, textvariable=self.textbox_device_name_var).place(x=100, y=5, width=100, height=20)
        #--------------------
        # チャネル0～7のLabelを作成
        #--------------------
        tk.Label(self.root, text='ﾁｬﾈﾙ0', anchor='nw').place(x=5, y=100, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ1', anchor='nw').place(x=5, y=125, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ2', anchor='nw').place(x=5, y=150, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ3', anchor='nw').place(x=5, y=175, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ4', anchor='nw').place(x=5, y=200, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ5', anchor='nw').place(x=5, y=225, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ6', anchor='nw').place(x=5, y=250, width=50, height=20)
        tk.Label(self.root, text='ﾁｬﾈﾙ7', anchor='nw').place(x=5, y=275, width=50, height=20)
        #----------------------------------------
        # カウント値のLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_value = tk.LabelFrame(self.root, text="カウント値")
        self.groupbox_counter_value.place(x=45, y=30, width=130, height=280)
        #--------------------
        # カウント値のRadiobuttonを作成
        #--------------------
        self.radiobutton_counter_value_base_var = tk.IntVar()
        self.radiobutton_counter_value_base_var.set(1)
        self.radiobutton_count_value_dec = tk.Radiobutton(self.groupbox_counter_value, text="10進数", variable=self.radiobutton_counter_value_base_var, value=1)
        self.radiobutton_count_value_dec.place(x=10, y=0, width=60, height=20)
        self.radiobutton_count_value_hex = tk.Radiobutton(self.groupbox_counter_value, text="16進数", variable=self.radiobutton_counter_value_base_var, value=2)
        self.radiobutton_count_value_hex.place(x=10, y=20, width=60, height=20)
        #--------------------
        # チャネル0～7カウント値のEntryを作成
        #--------------------
        self.textbox_ch0_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch0_value_var.set('')
        self.textbox_ch0_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch0_value_var, justify=tk.RIGHT)
        self.textbox_ch0_value.place(x=10, y=55, width=105, height=20)
        self.textbox_ch1_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch1_value_var.set('')
        self.textbox_ch1_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch1_value_var, justify=tk.RIGHT)
        self.textbox_ch1_value.place(x=10, y=80, width=105, height=20)
        self.textbox_ch2_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch2_value_var.set('')
        self.textbox_ch2_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch2_value_var, justify=tk.RIGHT)
        self.textbox_ch2_value.place(x=10, y=105, width=105, height=20)
        self.textbox_ch3_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch3_value_var.set('')
        self.textbox_ch3_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch3_value_var, justify=tk.RIGHT)
        self.textbox_ch3_value.place(x=10, y=130, width=105, height=20)
        self.textbox_ch4_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch4_value_var.set('')
        self.textbox_ch4_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch4_value_var, justify=tk.RIGHT)
        self.textbox_ch4_value.place(x=10, y=155, width=105, height=20)
        self.textbox_ch5_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch5_value_var.set('')
        self.textbox_ch5_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch5_value_var, justify=tk.RIGHT)
        self.textbox_ch5_value.place(x=10, y=180, width=105, height=20)
        self.textbox_ch6_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch6_value_var.set('')
        self.textbox_ch6_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch6_value_var, justify=tk.RIGHT)
        self.textbox_ch6_value.place(x=10, y=205, width=105, height=20)
        self.textbox_ch7_value_var = tk.StringVar(self.groupbox_counter_value)
        self.textbox_ch7_value_var.set('')
        self.textbox_ch7_value = tk.Entry(self.groupbox_counter_value, textvariable=self.textbox_ch7_value_var, justify=tk.RIGHT)
        self.textbox_ch7_value.place(x=10, y=230, width=105, height=20)
        #----------------------------------------
        # ステータスのLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_status = tk.LabelFrame(self.root, text="ステータス")
        self.groupbox_counter_status.place(x=180, y=30, width=90, height=280)
        #--------------------
        # チャネル0～7ステータスのEntryを作成
        #--------------------
        self.textbox_ch0_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch0_status_var.set('')
        self.textbox_ch0_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch0_status_var, justify=tk.RIGHT)
        self.textbox_ch0_status.place(x=10, y=55, width=65, height=20)
        self.textbox_ch1_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch1_status_var.set('')
        self.textbox_ch1_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch1_status_var, justify=tk.RIGHT)
        self.textbox_ch1_status.place(x=10, y=80, width=65, height=20)
        self.textbox_ch2_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch2_status_var.set('')
        self.textbox_ch2_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch2_status_var, justify=tk.RIGHT)
        self.textbox_ch2_status.place(x=10, y=105, width=65, height=20)
        self.textbox_ch3_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch3_status_var.set('')
        self.textbox_ch3_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch3_status_var, justify=tk.RIGHT)
        self.textbox_ch3_status.place(x=10, y=130, width=65, height=20)
        self.textbox_ch4_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch4_status_var.set('')
        self.textbox_ch4_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch4_status_var, justify=tk.RIGHT)
        self.textbox_ch4_status.place(x=10, y=155, width=65, height=20)
        self.textbox_ch5_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch5_status_var.set('')
        self.textbox_ch5_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch5_status_var, justify=tk.RIGHT)
        self.textbox_ch5_status.place(x=10, y=180, width=65, height=20)
        self.textbox_ch6_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch6_status_var.set('')
        self.textbox_ch6_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch6_status_var, justify=tk.RIGHT)
        self.textbox_ch6_status.place(x=10, y=205, width=65, height=20)
        self.textbox_ch7_status_var = tk.StringVar(self.groupbox_counter_status)
        self.textbox_ch7_status_var.set('')
        self.textbox_ch7_status = tk.Entry(self.groupbox_counter_status, textvariable=self.textbox_ch7_status_var, justify=tk.RIGHT)
        self.textbox_ch7_status.place(x=10, y=230, width=65, height=20)
        #----------------------------------------
        # プリセット値のLabelFrameを作成
        #----------------------------------------
        self.groupbox_preset_value = tk.LabelFrame(self.root, text="プリセット値")
        self.groupbox_preset_value.place(x=275, y=30, width=100, height=280)
        #--------------------
        # プリセット値のRadiobuttonを作成
        #--------------------
        self.radiobutton_preset_value_base_var = tk.IntVar()
        self.radiobutton_preset_value_base_var.set(1)
        self.radiobutton_preset_value_dec = tk.Radiobutton(self.groupbox_preset_value, text="10進数", variable=self.radiobutton_preset_value_base_var, value=1)
        self.radiobutton_preset_value_dec.place(x=10, y=0, width=60, height=20)
        self.radiobutton_preset_value_hex = tk.Radiobutton(self.groupbox_preset_value, text="16進数", variable=self.radiobutton_preset_value_base_var, value=2)
        self.radiobutton_preset_value_hex.place(x=10, y=20, width=60, height=20)
        #--------------------
        # チャネル0～7プリセット値のEntryを作成
        #--------------------
        self.textbox_ch0_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch0_preset_value_var.set('0')
        self.textbox_ch0_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch0_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch0_preset_value.place(x=10, y=55, width=75, height=20)
        self.textbox_ch1_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch1_preset_value_var.set('100')
        self.textbox_ch1_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch1_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch1_preset_value.place(x=10, y=80, width=75, height=20)
        self.textbox_ch2_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch2_preset_value_var.set('200')
        self.textbox_ch2_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch2_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch2_preset_value.place(x=10, y=105, width=75, height=20)
        self.textbox_ch3_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch3_preset_value_var.set('300')
        self.textbox_ch3_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch3_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch3_preset_value.place(x=10, y=130, width=75, height=20)
        self.textbox_ch4_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch4_preset_value_var.set('400')
        self.textbox_ch4_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch4_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch4_preset_value.place(x=10, y=155, width=75, height=20)
        self.textbox_ch5_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch5_preset_value_var.set('500')
        self.textbox_ch5_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch5_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch5_preset_value.place(x=10, y=180, width=75, height=20)
        self.textbox_ch6_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch6_preset_value_var.set('600')
        self.textbox_ch6_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch6_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch6_preset_value.place(x=10, y=205, width=75, height=20)
        self.textbox_ch7_preset_value_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_ch7_preset_value_var.set('700')
        self.textbox_ch7_preset_value = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_ch7_preset_value_var, justify=tk.RIGHT)
        self.textbox_ch7_preset_value.place(x=10, y=230, width=75, height=20)
        #----------------------------------------
        # 比較結果
        #----------------------------------------
        #--------------------
        # 比較結果のLabelを作成
        #--------------------
        tk.Label(self.root, text='比較結果 : ', anchor='nw').place(x=5, y=330, width=60, height=20)
        #--------------------
        # 比較結果のEntryを作成
        #--------------------
        self.textbox_compare_result_string_var= tk.StringVar(self.root)
        self.textbox_compare_result_string_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_compare_result_string_var).place(x=70, y=330, width=305, height=20)
        #----------------------------------------
        # 戻り値
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=5, y=360, width=50, height=20)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_return_string_var= tk.StringVar(self.root)
        self.textbox_return_string_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=70, y=360, width=305, height=20)
        #----------------------------------------
        # ボタン
        #----------------------------------------
        #--------------------
        # CntInitのButtonを作成
        #--------------------
        self.button_cnt_init = tk.Button(self.root, text='CntInit')
        self.button_cnt_init.place(x=385, y=15, width=100, height=20)
        #--------------------
        # モード設定のButtonを作成
        #--------------------
        self.button_cnt_mode = tk.Button(self.root, text='モード設定...')
        self.button_cnt_mode.place(x=385, y=60, width=100, height=20)
        #--------------------
        # 比較設定のButtonを作成
        #--------------------
        self.button_cnt_compare = tk.Button(self.root, text='比較設定...')
        self.button_cnt_compare.place(x=385, y=95, width=100, height=20)
        #--------------------
        # CntStartCountのButtonを作成
        #--------------------
        self.button_cnt_start = tk.Button(self.root, text='CntStartCount')
        self.button_cnt_start.place(x=385, y=150, width=100, height=20)
        #--------------------
        # CntPresetのButtonを作成
        #--------------------
        self.button_cnt_preset = tk.Button(self.root, text='CntPreset')
        self.button_cnt_preset.place(x=385, y=185, width=100, height=20)
        #--------------------
        # CntStopCountのButtonを作成
        #--------------------
        self.button_cnt_stop = tk.Button(self.root, text='CntStopCount')
        self.button_cnt_stop.place(x=385, y=220, width=100, height=20)
        #--------------------
        # CntExitのButtonを作成
        #--------------------
        self.button_cnt_exit = tk.Button(self.root, text='CntExit')
        self.button_cnt_exit.place(x=385, y=290, width=100, height=20)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=385, y=350, width=100, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_count_value_dec,
                       command_count_value_hex,
                       command_preset_value_dec,
                       command_preset_value_hex,
                       command_cnt_init,
                       command_cnt_mode,
                       command_cnt_compare,
                       command_cnt_start,
                       command_cnt_preset,
                       command_cnt_stop,
                       command_cnt_exit,
                       command_exit):
        #----------------------------------------
        # 「Dec」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_count_value_dec.configure(command=command_count_value_dec)
        #----------------------------------------
        # 「Hex」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_count_value_hex.configure(command=command_count_value_hex)
        #----------------------------------------
        # 「Dec」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_preset_value_dec.configure(command=command_preset_value_dec)
        #----------------------------------------
        # 「Hex」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_preset_value_hex.configure(command=command_preset_value_hex)
        #----------------------------------------
        # 「CntInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_init.configure(command=command_cnt_init)
        #----------------------------------------
        # 「モード設定...」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_mode.configure(command=command_cnt_mode)
        #----------------------------------------
        # 「比較設定...」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_compare.configure(command=command_cnt_compare)
        #----------------------------------------
        # 「CntStartCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_start.configure(command=command_cnt_start)
        #----------------------------------------
        # 「CntPreset」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_preset.configure(command=command_cnt_preset)
        #----------------------------------------
        # 「CntStopCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_stop.configure(command=command_cnt_stop)
        #----------------------------------------
        # 「CntExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_exit.configure(command=command_cnt_exit)
        #----------------------------------------
        # 「Exit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_exit.configure(command=command_exit)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

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