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


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

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # デバイス名のLabelFrameを作成
        #----------------------------------------
        self.groupbox_device_name = tk.LabelFrame(self.root, text="デバイス名")
        self.groupbox_device_name.place(x=10, y=13, width=370, height=45)
        #--------------------
        # マスターのデバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_device_name, text='マスター : ', anchor='nw').place(x=10, y=3, width=60, height=19)
        #--------------------
        # マスターのデバイス名のEntryを作成
        #--------------------
        self.textbox_m_device_name_var = tk.StringVar(self.groupbox_device_name)
        self.textbox_m_device_name_var.set('DIO000')
        tk.Entry(self.groupbox_device_name, textvariable=self.textbox_m_device_name_var).place(x=80, y=3, width=80, height=19)
        #--------------------
        # スレーブのデバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_device_name, text='スレーブ : ', anchor='nw').place(x=180, y=3, width=60, height=19)
        #--------------------
        # スレーブのデバイス名のEntryを作成
        #--------------------
        self.textbox_s_device_name_var = tk.StringVar(self.groupbox_device_name)
        self.textbox_s_device_name_var.set('DIO001')
        tk.Entry(self.groupbox_device_name, textvariable=self.textbox_s_device_name_var).place(x=250, y=3, width=80, height=19)
        #----------------------------------------
        # マスターステータスのLabelFrameを作成
        #----------------------------------------
        self.groupbox_m_status = tk.LabelFrame(self.root, text="マスターステータス")
        self.groupbox_m_status.place(x=10, y=60, width=370, height=130)
        #--------------------
        # カウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_m_status, text='カウント : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # カウントのEntryを作成
        #--------------------
        self.textbox_m_trans_count_var = tk.StringVar(self.groupbox_m_status)
        tk.Entry(self.groupbox_m_status, textvariable=self.textbox_m_trans_count_var, justify=tk.RIGHT).place(x=100, y=5, width=240, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_m_status, text='ステータス : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_m_status_var = tk.StringVar(self.groupbox_m_status)
        tk.Entry(self.groupbox_m_status, textvariable=self.textbox_m_status_var).place(x=100, y=30, width=240, height=19)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.groupbox_m_status, text='エラー : ', anchor='nw').place(x=10, y=55, width=90, height=19)
        #--------------------
        # エラーのEntryを作成
        #--------------------
        self.textbox_m_error_var = tk.StringVar(self.groupbox_m_status)
        tk.Entry(self.groupbox_m_status, textvariable=self.textbox_m_error_var).place(x=100, y=55, width=240, height=19)
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_m_status, text='戻り値 : ', anchor='nw').place(x=10, y=80, width=90, height=19)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_m_return_string_var= tk.StringVar(self.groupbox_m_status)
        tk.Entry(self.groupbox_m_status, textvariable=self.textbox_m_return_string_var).place(x=100, y=80, width=240, height=19)
        #----------------------------------------
        # スレーブステータスのLabelFrameを作成
        #----------------------------------------
        self.groupbox_s_status = tk.LabelFrame(self.root, text="スレーブステータス")
        self.groupbox_s_status.place(x=10, y=192, width=370, height=130)
        #--------------------
        # カウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_s_status, text='カウント : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # カウントのEntryを作成
        #--------------------
        self.textbox_s_trans_count_var = tk.StringVar(self.groupbox_s_status)
        tk.Entry(self.groupbox_s_status, textvariable=self.textbox_s_trans_count_var, justify=tk.RIGHT).place(x=100, y=5, width=240, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_s_status, text='ステータス : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_s_status_var = tk.StringVar(self.groupbox_s_status)
        tk.Entry(self.groupbox_s_status, textvariable=self.textbox_s_status_var).place(x=100, y=30, width=240, height=19)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.groupbox_s_status, text='エラー : ', anchor='nw').place(x=10, y=55, width=90, height=19)
        #--------------------
        # エラーのEntryを作成
        #--------------------
        self.textbox_s_error_var = tk.StringVar(self.groupbox_s_status)
        tk.Entry(self.groupbox_s_status, textvariable=self.textbox_s_error_var).place(x=100, y=55, width=240, height=19)
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_s_status, text='戻り値 : ', anchor='nw').place(x=10, y=80, width=90, height=19)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_s_return_string_var= tk.StringVar(self.groupbox_s_status)
        tk.Entry(self.groupbox_s_status, textvariable=self.textbox_s_return_string_var).place(x=100, y=80, width=240, height=19)
        #----------------------------------------
        # マスターのLabelFrameを作成
        #----------------------------------------
        self.groupbox_m = tk.LabelFrame(self.root, text="マスター")
        self.groupbox_m.place(x=390, y=13, width=110, height=309)
        #--------------------
        # DioInitのButtonを作成
        #--------------------
        self.button_m_dio_init = tk.Button(self.groupbox_m, text='DioInit')
        self.button_m_dio_init.place(x=8, y=15, width=90, height=19)
        #--------------------
        # 条件設定のButtonを作成
        #--------------------
        self.button_m_condition = tk.Button(self.groupbox_m, text='条件設定...')
        self.button_m_condition.place(x=8, y=50, width=90, height=19)
        #--------------------
        # 転送スタートのButtonを作成
        #--------------------
        self.button_m_start = tk.Button(self.groupbox_m, text='転送スタート')
        self.button_m_start.place(x=8, y=85, width=90, height=19)
        #--------------------
        # 転送ストップのButtonを作成
        #--------------------
        self.button_m_stop = tk.Button(self.groupbox_m, text='転送ストップ')
        self.button_m_stop.place(x=8, y=120, width=90, height=19)
        #--------------------
        # 取得データのButtonを作成
        #--------------------
        self.button_m_data = tk.Button(self.groupbox_m, text='取得データ...')
        self.button_m_data.place(x=8, y=155, width=90, height=19)
        #--------------------
        # 転送カウントのButtonを作成
        #--------------------
        self.button_m_count = tk.Button(self.groupbox_m, text='転送カウント')
        self.button_m_count.place(x=8, y=190, width=90, height=19)
        #--------------------
        # 転送ステータスのButtonを作成
        #--------------------
        self.button_m_status = tk.Button(self.groupbox_m, text='転送ステータス')
        self.button_m_status.place(x=8, y=225, width=90, height=19)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_m_dio_exit = tk.Button(self.groupbox_m, text='DioExit')
        self.button_m_dio_exit.place(x=8, y=260, width=90, height=19)
        #----------------------------------------
        # スレーブのLabelFrameを作成
        #----------------------------------------
        self.groupbox_s = tk.LabelFrame(self.root, text="スレーブ")
        self.groupbox_s.place(x=505, y=13, width=110, height=309)
        #--------------------
        # DioInitのButtonを作成
        #--------------------
        self.button_s_dio_init = tk.Button(self.groupbox_s, text='DioInit')
        self.button_s_dio_init.place(x=8, y=15, width=90, height=19)
        #--------------------
        # 条件設定のButtonを作成
        #--------------------
        self.button_s_condition = tk.Button(self.groupbox_s, text='条件設定...')
        self.button_s_condition.place(x=8, y=50, width=90, height=19)
        #--------------------
        # 転送スタートのButtonを作成
        #--------------------
        self.button_s_start = tk.Button(self.groupbox_s, text='転送スタート')
        self.button_s_start.place(x=8, y=85, width=90, height=19)
        #--------------------
        # 転送ストップのButtonを作成
        #--------------------
        self.button_s_stop = tk.Button(self.groupbox_s, text='転送ストップ')
        self.button_s_stop.place(x=8, y=120, width=90, height=19)
        #--------------------
        # 取得データのButtonを作成
        #--------------------
        self.button_s_data = tk.Button(self.groupbox_s, text='取得データ...')
        self.button_s_data.place(x=8, y=155, width=90, height=19)
        #--------------------
        # 転送カウントのButtonを作成
        #--------------------
        self.button_s_count = tk.Button(self.groupbox_s, text='転送カウント')
        self.button_s_count.place(x=8, y=190, width=90, height=19)
        #--------------------
        # 転送ステータスのButtonを作成
        #--------------------
        self.button_s_status = tk.Button(self.groupbox_s, text='転送ステータス')
        self.button_s_status.place(x=8, y=225, width=90, height=19)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_s_dio_exit = tk.Button(self.groupbox_s, text='DioExit')
        self.button_s_dio_exit.place(x=8, y=260, width=90, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=445, y=336, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_m_dio_init,
                       command_m_condition,
                       command_m_start,
                       command_m_stop,
                       command_m_data,
                       command_m_count,
                       command_m_status,
                       command_m_dio_exit,
                       command_s_dio_init,
                       command_s_condition,
                       command_s_start,
                       command_s_stop,
                       command_s_data,
                       command_s_count,
                       command_s_status,
                       command_s_dio_exit,
                       command_exit):
        #----------------------------------------
        # マスターの「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_dio_init.configure(command=command_m_dio_init)
        #----------------------------------------
        # マスターの「条件設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_condition.configure(command=command_m_condition)
        #----------------------------------------
        # マスターの「転送スタート」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_start.configure(command=command_m_start)
        #----------------------------------------
        # マスターの「転送ストップ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_stop.configure(command=command_m_stop)
        #----------------------------------------
        # マスターの「取得データ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_data.configure(command=command_m_data)
        #----------------------------------------
        # マスターの「転送カウント」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_count.configure(command=command_m_count)
        #----------------------------------------
        # マスターの「転送ステータス」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_status.configure(command=command_m_status)
        #----------------------------------------
        # マスターの「DioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_dio_exit.configure(command=command_m_dio_exit)
        #----------------------------------------
        # スレーブの「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_dio_init.configure(command=command_s_dio_init)
        #----------------------------------------
        # スレーブの「条件設定」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_condition.configure(command=command_s_condition)
        #----------------------------------------
        # スレーブの「転送スタート」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_start.configure(command=command_s_start)
        #----------------------------------------
        # スレーブの「転送ストップ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_stop.configure(command=command_s_stop)
        #----------------------------------------
        # スレーブの「取得データ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_data.configure(command=command_s_data)
        #----------------------------------------
        # スレーブの「転送カウント」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_count.configure(command=command_s_count)
        #----------------------------------------
        # スレーブの「転送ステータス」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_status.configure(command=command_s_status)
        #----------------------------------------
        # スレーブの「DioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_dio_exit.configure(command=command_s_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()