#================================================================
#================================================================
# API-DIO(WDM)
# トリガ監視サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# TriggerUI クラス
#========================================
class TriggerUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('トリガ監視')
        self.root.minsize(500,315)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=20, y=20, width=90, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.root)
        self.textbox_device_name_var.set('DIO000')
        tk.Entry(self.root, textvariable=self.textbox_device_name_var).place(x=110, y=20, width=90, height=19)
        #--------------------
        # DioInitのButtonを作成
        #--------------------
        self.button_dio_init = tk.Button(self.root, text='DioInit')
        self.button_dio_init.place(x=342, y=20, width=110, height=19)
        #----------------------------------------
        # トリガ監視のLabelFrameを作成
        #----------------------------------------
        self.groupbox_trigger = tk.LabelFrame(self.root, text="トリガ監視")
        self.groupbox_trigger.place(x=15, y=60, width=470, height=155)
        #--------------------
        # ビット0～7のLabelを作成
        #--------------------
        tk.Label(self.groupbox_trigger, text='0', anchor='nw').place(x=122, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='1', anchor='nw').place(x=165, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='2', anchor='nw').place(x=208, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='3', anchor='nw').place(x=251, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='4', anchor='nw').place(x=294, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='5', anchor='nw').place(x=337, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='6', anchor='nw').place(x=380, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='7', anchor='nw').place(x=423, y=1, width=10, height=19)
        #--------------------
        # アップカウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_trigger, text='アップカウント : ', anchor='nw').place(x=5, y=25, width=100, height=19)
        #--------------------
        # ビット0～7のアップカウントのEntryを作成
        #--------------------
        self.textbox_up0_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up0_var, justify=tk.RIGHT).place(x=110, y=25, width=36, height=19)
        self.textbox_up1_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up1_var, justify=tk.RIGHT).place(x=153, y=25, width=36, height=19)
        self.textbox_up2_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up2_var, justify=tk.RIGHT).place(x=196, y=25, width=36, height=19)
        self.textbox_up3_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up3_var, justify=tk.RIGHT).place(x=239, y=25, width=36, height=19)
        self.textbox_up4_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up4_var, justify=tk.RIGHT).place(x=282, y=25, width=36, height=19)
        self.textbox_up5_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up5_var, justify=tk.RIGHT).place(x=325, y=25, width=36, height=19)
        self.textbox_up6_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up6_var, justify=tk.RIGHT).place(x=368, y=25, width=36, height=19)
        self.textbox_up7_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up7_var, justify=tk.RIGHT).place(x=411, y=25, width=36, height=19)
        #--------------------
        # ダウンカウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_trigger, text='ダウンカウント : ', anchor='nw').place(x=5, y=53, width=100, height=19)
        #--------------------
        # ビット0～7のダウンカウントのEntryを作成
        #--------------------
        self.textbox_down0_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down0_var, justify=tk.RIGHT).place(x=110, y=53, width=36, height=19)
        self.textbox_down1_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down1_var, justify=tk.RIGHT).place(x=153, y=53, width=36, height=19)
        self.textbox_down2_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down2_var, justify=tk.RIGHT).place(x=196, y=53, width=36, height=19)
        self.textbox_down3_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down3_var, justify=tk.RIGHT).place(x=239, y=53, width=36, height=19)
        self.textbox_down4_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down4_var, justify=tk.RIGHT).place(x=282, y=53, width=36, height=19)
        self.textbox_down5_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down5_var, justify=tk.RIGHT).place(x=325, y=53, width=36, height=19)
        self.textbox_down6_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down6_var, justify=tk.RIGHT).place(x=368, y=53, width=36, height=19)
        self.textbox_down7_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down7_var, justify=tk.RIGHT).place(x=411, y=53, width=36, height=19)
        #--------------------
        # 有効／無効のLabelを作成
        #--------------------
        tk.Label(self.groupbox_trigger, text='有効／無効 : ', anchor='nw').place(x=5, y=80, width=100, height=19)
        #--------------------
        # ビット0～7の有効／無効のCheckbuttonを作成
        #--------------------
        self.checkbox0_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox0_var.set(1)
        self.checkbox0 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox0_var)
        self.checkbox0.place(x=112, y=80, width=36, height=19)
        self.checkbox1_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox1_var.set(1)
        self.checkbox1 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox1_var)
        self.checkbox1.place(x=155, y=80, width=36, height=19)
        self.checkbox2_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox2_var.set(1)
        self.checkbox2 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox2_var)
        self.checkbox2.place(x=198, y=80, width=36, height=19)
        self.checkbox3_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox3_var.set(1)
        self.checkbox3 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox3_var)
        self.checkbox3.place(x=241, y=80, width=36, height=19)
        self.checkbox4_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox4_var.set(1)
        self.checkbox4 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox4_var)
        self.checkbox4.place(x=284, y=80, width=36, height=19)
        self.checkbox5_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox5_var.set(1)
        self.checkbox5 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox5_var)
        self.checkbox5.place(x=327, y=80, width=36, height=19)
        self.checkbox6_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox6_var.set(1)
        self.checkbox6 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox6_var)
        self.checkbox6.place(x=370, y=80, width=36, height=19)
        self.checkbox7_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox7_var.set(1)
        self.checkbox7 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox7_var)
        self.checkbox7.place(x=413, y=80, width=36, height=19)
        #--------------------
        # Trigger StartのButtonを作成
        #--------------------
        self.button_trigger = tk.Button(self.groupbox_trigger, text='Trigger Start')
        self.button_trigger.place(x=325, y=110, width=110, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=20, y=230, 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=75, y=230, width=250, height=19)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=342, y=230, width=110, height=19)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=342, y=260, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_check0,
                       command_check1,
                       command_check2,
                       command_check3,
                       command_check4,
                       command_check5,
                       command_check6,
                       command_check7,
                       command_trigger,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # 「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # 「ビット0の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox0.configure(command=command_check0)
        #----------------------------------------
        # 「ビット1の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox1.configure(command=command_check1)
        #----------------------------------------
        # 「ビット2の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox2.configure(command=command_check2)
        #----------------------------------------
        # 「ビット3の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox3.configure(command=command_check3)
        #----------------------------------------
        # 「ビット4の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox4.configure(command=command_check4)
        #----------------------------------------
        # 「ビット5の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox5.configure(command=command_check5)
        #----------------------------------------
        # 「ビット6の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox6.configure(command=command_check6)
        #----------------------------------------
        # 「ビット7の有効／無効」チェックボックスのコールバック関数を設定
        #----------------------------------------
        self.checkbox7.configure(command=command_check7)
        #----------------------------------------
        # 「Trigger Start」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_trigger.configure(command=command_trigger)
        #----------------------------------------
        # 「DioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_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()