#================================================================
#================================================================
# API-AIO(WDM)
# アナログ簡易入力サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AiSimpleUI クラス
#========================================
class AiSimpleUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('アナログ簡易入力')
        self.root.minsize(740,510)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=710, height=50)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, y=5, width=85, 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=125, y=5, width=130, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # レンジ設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_range_settings = tk.LabelFrame(self.root, text="レンジ設定")
        self.groupbox_range_settings.place(x=15, y=65, width=710, height=50)
        #--------------------
        # レンジのLabelを作成
        #--------------------
        tk.Label(self.groupbox_range_settings, text='レンジ : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # レンジのComboboxを作成
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_range_settings)
        self.combobox_range.place(x=125, y=5, width=130, height=19)
        self.combobox_range['value'] = ('-10 - +10V', '-5 - +5V', '-2.5 - +2.5V', '-1.25 - +1.25V', '0 - +10V', '0 - +5V', '0 - +2.5V', '0 - +1.25V', '0 - 20mA', '4 - 20mA', '-20 - +20mA', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # AioSetAiRangeAllのButtonを作成
        #--------------------
        self.button_aio_set_ai_range_all = tk.Button(self.groupbox_range_settings, text='AioSetAiRangeAll')
        self.button_aio_set_ai_range_all.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 入力方式設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_input_method_settings = tk.LabelFrame(self.root, text="入力方式設定")
        self.groupbox_input_method_settings.place(x=15, y=120, width=710, height=50)
        #--------------------
        # 入力方式のLabelを作成
        #--------------------
        tk.Label(self.groupbox_input_method_settings, text='入力方式 : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # 入力方式のComboboxを作成
        #--------------------
        self.combobox_input_method = ttk.Combobox(self.groupbox_input_method_settings)
        self.combobox_input_method.place(x=125, y=5, width=130, height=19)
        self.combobox_input_method['value'] = ('シングルエンド入力', '差動入力')
        self.combobox_input_method.current(0)
        self.combobox_input_method['state'] = 'readonly'
        #--------------------
        # AioSetAiInputMethodのButtonを作成
        #--------------------
        self.button_aio_set_ai_input_method = tk.Button(self.groupbox_input_method_settings, text='AioSetAiInputMethod')
        self.button_aio_set_ai_input_method.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 指定チャネル簡易入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_single_ai_ex = tk.LabelFrame(self.root, text="指定チャネル簡易入力")
        self.groupbox_single_ai_ex.place(x=15, y=175, width=710, height=50)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_single_ai_ex, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_channel_no_var = tk.StringVar(self.groupbox_single_ai_ex)
        self.textbox_channel_no_var.set("0")
        tk.Entry(self.groupbox_single_ai_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_no_var).place(x=125, y=5, width=130, height=19)
        #--------------------
        # 入力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_single_ai_ex, text='入力データ : ', anchor='nw').place(x=260, y=5, width=75, height=19)
        #--------------------
        # 入力データのEntryを作成
        #--------------------
        self.textbox_single_ai_data_var = tk.StringVar(self.groupbox_single_ai_ex)
        tk.Entry(self.groupbox_single_ai_ex, justify=tk.RIGHT, textvariable=self.textbox_single_ai_data_var).place(x=355, y=5, width=130, height=19)
        #--------------------
        # AioSingleAiExのButtonを作成
        #--------------------
        self.button_aio_single_ai_ex = tk.Button(self.groupbox_single_ai_ex, text='AioSingleAiEx')
        self.button_aio_single_ai_ex.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 複数チャネル簡易入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_multi_ai_ex = tk.LabelFrame(self.root, text="複数チャネル簡易入力")
        self.groupbox_multi_ai_ex.place(x=15, y=230, width=710, height=150)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='チャネル数 : ', anchor='nw').place(x=15, y=30, width=85, height=19)
        #--------------------
        # チャネル数のEntryを作成
        #--------------------
        self.textbox_channel_num_var = tk.StringVar(self.groupbox_multi_ai_ex)
        self.textbox_channel_num_var.set("1")
        tk.Entry(self.groupbox_multi_ai_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_num_var).place(x=125, y=30, width=130, height=19)
        #--------------------
        # 入力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='入力データ : ', anchor='nw').place(x=260, y=30, width=75, height=19)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='チャネル番号', anchor='nw').place(x=355, y=5, width=160, height=19)
        #--------------------
        # 電圧/電流値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ai_ex, text='電圧/電流値', anchor='nw').place(x=440, y=5, width=160, height=19)
        #--------------------
        # 入力データのListboxを作成
        #--------------------
        self.listbox_multi_ai_data_var = tk.StringVar(self.groupbox_multi_ai_ex)
        self.listbox_multi_ai_data = tk.Listbox(self.groupbox_multi_ai_ex, listvariable=self.listbox_multi_ai_data_var)
        self.listbox_y_scrollbar = tk.Scrollbar(self.groupbox_multi_ai_ex, orient="vertical", command=self.listbox_multi_ai_data.yview)
        self.listbox_multi_ai_data.config(yscrollcommand=self.listbox_y_scrollbar.set)
        self.listbox_multi_ai_data.place(x=355, y=30, width=170, height=90)
        self.listbox_y_scrollbar.place(x=525, y=30, width=10, height=90)
        #--------------------
        # AioMultiAiExのButtonを作成
        #--------------------
        self.button_aio_multi_ai_ex = tk.Button(self.groupbox_multi_ai_ex, text='AioMultiAiEx')
        self.button_aio_multi_ai_ex.place(x=550, y=30, width=150, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=385, width=710, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=450, width=160, height=19)
        #--------------------
        # 戻り値の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=142, y=450, width=330, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=567, y=475, width=150, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_set_ai_range_all,
                       command_aio_set_ai_input_method,
                       command_aio_single_ai_ex,
                       command_aio_multi_ai_ex,
                       command_aio_exit,
                       command_close):
        #----------------------------------------
        # 「AioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # 「AioSetAiRangeAll」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_range_all.configure(command=command_aio_set_ai_range_all)
        #----------------------------------------
        # 「AioSetAiInputMethod」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ai_input_method.configure(command=command_aio_set_ai_input_method)
        #----------------------------------------
        # 「AioSingleAiEx」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_single_ai_ex.configure(command=command_aio_single_ai_ex)
        #----------------------------------------
        # 「AioMultiAiEx」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_multi_ai_ex.configure(command=command_aio_multi_ai_ex)
        #----------------------------------------
        # 「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()