﻿#================================================================
#================================================================
# API-DIO(WDM) (BusMaster)
# ジェネレーティングサンプル
# main UIファイル
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# GeneratingUI クラス
#========================================
class GeneratingUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('ジェネレーティング')
        self.root.minsize(490,300)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=360, y=20, width=110, height=19)
        #----------------------------------------
        # 転送
        #----------------------------------------
        #--------------------
        # 条件設定のButtonを作成
        #--------------------
        self.button_condition = tk.Button(self.root, text='条件設定...')
        self.button_condition.place(x=360, y=47, width=110, height=19)
        #--------------------
        # 出力データのButtonを作成
        #--------------------
        self.button_out_data = tk.Button(self.root, text='出力データ...')
        self.button_out_data.place(x=360, y=74, width=110, height=19)
        #--------------------
        # データセットのButtonを作成
        #--------------------
        self.button_set_data = tk.Button(self.root, text='データセット')
        self.button_set_data.place(x=360, y=101, width=110, height=19)
        #--------------------
        # 転送スタートのButtonを作成
        #--------------------
        self.button_start = tk.Button(self.root, text='転送スタート')
        self.button_start.place(x=360, y=128, width=110, height=19)
        #--------------------
        # 転送ストップのButtonを作成
        #--------------------
        self.button_stop = tk.Button(self.root, text='転送ストップ')
        self.button_stop.place(x=360, y=155, width=110, height=19)
        #--------------------
        # 転送カウントのLabelを作成
        #--------------------
        tk.Label(self.root, text='転送カウント : ', anchor='nw').place(x=20, y=182, width=90, height=19)
        #--------------------
        # 転送カウントのEntryを作成
        #--------------------
        self.textbox_trans_count_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_trans_count_var, justify=tk.RIGHT).place(x=110, y=182, width=230, height=19)
        #--------------------
        # 転送カウントのButtonを作成
        #--------------------
        self.button_count = tk.Button(self.root, text='転送カウント')
        self.button_count.place(x=360, y=182, width=110, height=19)
        #--------------------
        # 転送ステータスのLabelを作成
        #--------------------
        tk.Label(self.root, text='ステータス : ', anchor='nw').place(x=20, y=209, width=90, height=19)
        #--------------------
        # 転送ステータスのEntryを作成
        #--------------------
        self.textbox_status_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_status_var).place(x=110, y=209, width=230, height=19)
        #--------------------
        # 転送ステータスのButtonを作成
        #--------------------
        self.button_status = tk.Button(self.root, text='転送ステータス')
        self.button_status.place(x=360, y=209, width=110, height=19)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.root, text='エラー : ', anchor='nw').place(x=20, y=236, width=90, height=19)
        #--------------------
        # エラーのEntryを作成
        #--------------------
        self.textbox_error_var = tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_error_var).place(x=110, y=236, width=230, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=360, y=236, width=110, height=19)
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=20, y=263, 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=110, y=263, width=230, height=19)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=360, y=263, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_condition,
                       command_out_data,
                       command_set_data,
                       command_start,
                       command_stop,
                       command_count,
                       command_status,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # 「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # 「条件設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_condition.configure(command=command_condition)
        #----------------------------------------
        # 「出力データ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_out_data.configure(command=command_out_data)
        #----------------------------------------
        # 「データセット」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_set_data.configure(command=command_set_data)
        #----------------------------------------
        # 「転送スタート」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_start.configure(command=command_start)
        #----------------------------------------
        # 「転送ストップ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_stop.configure(command=command_stop)
        #----------------------------------------
        # 「転送カウント」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_count.configure(command=command_count)
        #----------------------------------------
        # 「転送ステータス」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_status.configure(command=command_status)
        #----------------------------------------
        # 「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()