#================================================================
#================================================================
# API-DIO(WDM)
# 単純入出力サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# SimpleIOUI クラス
#========================================
class SimpleIOUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('単純入出力')
        self.root.minsize(595,420)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=347, y=20, width=110, height=19)
        #----------------------------------------
        # 入出力設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_dir_settings = tk.LabelFrame(self.root, text="入出力設定")
        self.groupbox_dir_settings.place(x=15, y=60, width=570, height=50)
        #--------------------
        # 入出力方向のLabelを作成
        #--------------------
        tk.Label(self.groupbox_dir_settings, text='入出力方向 : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # 入出力方向のEntryを作成
        #--------------------
        self.textbox_dir_var = tk.StringVar(self.groupbox_dir_settings)
        tk.Entry(self.groupbox_dir_settings, textvariable=self.textbox_dir_var, justify=tk.RIGHT).place(x=108, y=5, width=60, height=19)
        #--------------------
        # DioSetIoDirectionのButtonを作成
        #--------------------
        self.button_dio_set_io_direction = tk.Button(self.groupbox_dir_settings, text='DioSetIoDirection')
        self.button_dio_set_io_direction.place(x=330, y=5, width=110, height=19)
        #----------------------------------------
        # 1ポート入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_inp_port = tk.LabelFrame(self.root, text="1ポート入力")
        self.groupbox_inp_port.place(x=15, y=120, width=570, height=50)
        #--------------------
        # 論理ポート番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='論理ポート番号 : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # 論理ポート番号のEntryを作成
        #--------------------
        self.textbox_inp_port_no_var = tk.StringVar(self.groupbox_inp_port)
        self.textbox_inp_port_no_var.set("0")
        tk.Entry(self.groupbox_inp_port, textvariable=self.textbox_inp_port_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # 入力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='入力データ : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # 入力データのEntryを作成
        #--------------------
        self.textbox_inp_port_data_var = tk.StringVar(self.groupbox_inp_port)
        tk.Entry(self.groupbox_inp_port, textvariable=self.textbox_inp_port_data_var, justify=tk.RIGHT).place(x=235, y=5, width=40, height=19)
        #--------------------
        # 入力データ進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_port, text='(16進数)', anchor='nw').place(x=275, y=5, width=80, height=19)
        #--------------------
        # DioInpByteのButtonを作成
        #--------------------
        self.button_dio_inp_byte = tk.Button(self.groupbox_inp_port, text='DioInpByte')
        self.button_dio_inp_byte.place(x=330, y=5, width=110, height=19)
        #----------------------------------------
        # 1ビット入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_inp_bit = tk.LabelFrame(self.root, text="1ビット入力")
        self.groupbox_inp_bit.place(x=15, y=180, width=570, height=50)
        #--------------------
        # 論理ビット番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='論理ビット番号 : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # 論理ビット番号のEntryを作成
        #--------------------
        self.textbox_inp_bit_no_var = tk.StringVar(self.groupbox_inp_bit)
        self.textbox_inp_bit_no_var.set("0")
        tk.Entry(self.groupbox_inp_bit, textvariable=self.textbox_inp_bit_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # 入力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_inp_bit, text='入力データ : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # 入力データのEntryを作成
        #--------------------
        self.textbox_inp_bit_data_var = tk.StringVar(self.groupbox_inp_bit)
        tk.Entry(self.groupbox_inp_bit, textvariable=self.textbox_inp_bit_data_var, justify=tk.RIGHT).place(x=235, y=5, width=40, height=19)
        #--------------------
        # DioInpBitのButtonを作成
        #--------------------
        self.button_dio_inp_bit = tk.Button(self.groupbox_inp_bit, text='DioInpBit')
        self.button_dio_inp_bit.place(x=330, y=5, width=110, height=19)
        #----------------------------------------
        # 1ポート出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_out_port = tk.LabelFrame(self.root, text="1ポート出力")
        self.groupbox_out_port.place(x=15, y=240, width=570, height=50)
        #--------------------
        # 論理ポート番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='論理ポート番号 : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # 論理ポート番号のEntryを作成
        #--------------------
        self.textbox_out_port_no_var = tk.StringVar(self.groupbox_out_port)
        self.textbox_out_port_no_var.set("0")
        tk.Entry(self.groupbox_out_port, textvariable=self.textbox_out_port_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # 出力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='出力データ : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # 出力データのEntryを作成
        #--------------------
        self.textbox_out_port_data_var = tk.StringVar(self.groupbox_out_port)
        self.textbox_out_port_data_var.set("55")
        tk.Entry(self.groupbox_out_port, textvariable=self.textbox_out_port_data_var, justify=tk.RIGHT).place(x=235, y=5, width=40, height=19)
        #--------------------
        # 出力データ進数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_port, text='(16進数)', anchor='nw').place(x=275, y=5, width=80, height=19)
        #--------------------
        # DioOutByteのButtonを作成
        #--------------------
        self.button_dio_out_byte = tk.Button(self.groupbox_out_port, text='DioOutByte')
        self.button_dio_out_byte.place(x=330, y=5, width=110, height=19)
        #--------------------
        # DioEchoBackByteのButtonを作成
        #--------------------
        self.button_dio_echo_back_byte = tk.Button(self.groupbox_out_port, text='DioEchoBackByte')
        self.button_dio_echo_back_byte.place(x=445, y=5, width=110, height=19)
        #----------------------------------------
        # 1ビット出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_out_bit = tk.LabelFrame(self.root, text="1ビット出力")
        self.groupbox_out_bit.place(x=15, y=300, width=570, height=50)
        #--------------------
        # 論理ビット番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_bit, text='論理ビット番号 : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # 論理ビット番号のEntryを作成
        #--------------------
        self.textbox_out_bit_no_var = tk.StringVar(self.groupbox_out_bit)
        self.textbox_out_bit_no_var.set("0")
        tk.Entry(self.groupbox_out_bit, textvariable=self.textbox_out_bit_no_var, justify=tk.RIGHT).place(x=110, y=5, width=40, height=19)
        #--------------------
        # 出力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_out_bit, text='出力データ : ', anchor='nw').place(x=155, y=5, width=90, height=19)
        #--------------------
        # 出力データのEntryを作成
        #--------------------
        self.textbox_out_bit_data_var = tk.StringVar(self.groupbox_out_bit)
        self.textbox_out_bit_data_var.set("1")
        tk.Entry(self.groupbox_out_bit, textvariable=self.textbox_out_bit_data_var, justify=tk.RIGHT).place(x=235, y=5, width=40, height=19)
        #--------------------
        # DioOutBitのButtonを作成
        #--------------------
        self.button_dio_out_bit = tk.Button(self.groupbox_out_bit, text='DioOutBit')
        self.button_dio_out_bit.place(x=330, y=5, width=110, height=19)
        #--------------------
        # DioEchoBackBitのButtonを作成
        #--------------------
        self.button_dio_echo_back_bit = tk.Button(self.groupbox_out_bit, text='DioEchoBackBit')
        self.button_dio_echo_back_bit.place(x=445, y=5, width=110, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=20, y=360, 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=360, width=250, height=19)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=347, y=360, width=110, height=19)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=347, y=390, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_dio_set_io_direction,
                       command_dio_inp_byte,
                       command_dio_inp_bit,
                       command_dio_out_byte,
                       command_dio_echo_back_byte,
                       command_dio_out_bit,
                       command_dio_echo_back_bit,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # 「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # 「DioSetIoDirection」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_set_io_direction.configure(command=command_dio_set_io_direction)
        #----------------------------------------
        # 「DioInpByte」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_inp_byte.configure(command=command_dio_inp_byte)
        #----------------------------------------
        # 「DioInpBit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_inp_bit.configure(command=command_dio_inp_bit)
        #----------------------------------------
        # 「DioOutByte」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_out_byte.configure(command=command_dio_out_byte)
        #----------------------------------------
        # 「DioEchoBackByte」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_echo_back_byte.configure(command=command_dio_echo_back_byte)
        #----------------------------------------
        # 「DioOutBit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_out_bit.configure(command=command_dio_out_bit)
        #----------------------------------------
        # 「DioEchoBackBit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_echo_back_bit.configure(command=command_dio_echo_back_bit)
        #----------------------------------------
        # 「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()