﻿#================================================================
#================================================================
# API-DIO(WDM) (BusMaster)
# Sync Sampling Sample
# main UI file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


#========================================
# SyncUI class
#========================================
class SyncUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Sync Sampling')
        self.root.minsize(645,370)
        #----------------------------------------
        # Set the font for controls
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Device Name
        #----------------------------------------
        self.groupbox_device_name = tk.LabelFrame(self.root, text="Device Name")
        self.groupbox_device_name.place(x=10, y=13, width=370, height=45)
        #--------------------
        # Create Label for Master Device Name
        #--------------------
        tk.Label(self.groupbox_device_name, text='Master : ', anchor='nw').place(x=10, y=3, width=60, height=19)
        #--------------------
        # Create Entry for Master Device Name
        #--------------------
        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)
        #--------------------
        # Create Label for Slave Device Name
        #--------------------
        tk.Label(self.groupbox_device_name, text='Slave : ', anchor='nw').place(x=180, y=3, width=60, height=19)
        #--------------------
        # Create Entry for Slave Device Name
        #--------------------
        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)
        #----------------------------------------
        # Create LabelFrame for Master Status
        #----------------------------------------
        self.groupbox_m_status = tk.LabelFrame(self.root, text="Master Status")
        self.groupbox_m_status.place(x=10, y=60, width=370, height=130)
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.groupbox_m_status, text='Count : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Count
        #--------------------
        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)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_m_status, text='Status : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        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)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.groupbox_m_status, text='Error : ', anchor='nw').place(x=10, y=55, width=90, height=19)
        #--------------------
        # Create Entry for Error
        #--------------------
        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)
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.groupbox_m_status, text='Return Code : ', anchor='nw').place(x=10, y=80, width=90, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        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)
        #----------------------------------------
        # Create LabelFrame for Slave Status
        #----------------------------------------
        self.groupbox_s_status = tk.LabelFrame(self.root, text="Slave Status")
        self.groupbox_s_status.place(x=10, y=192, width=370, height=130)
        #--------------------
        # Create Label for Count
        #--------------------
        tk.Label(self.groupbox_s_status, text='Count : ', anchor='nw').place(x=10, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Count
        #--------------------
        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)
        #--------------------
        # Create Label for Status
        #--------------------
        tk.Label(self.groupbox_s_status, text='Status : ', anchor='nw').place(x=10, y=30, width=90, height=19)
        #--------------------
        # Create Entry for Status
        #--------------------
        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)
        #--------------------
        # Create Label for Error
        #--------------------
        tk.Label(self.groupbox_s_status, text='Error : ', anchor='nw').place(x=10, y=55, width=90, height=19)
        #--------------------
        # Create Entry for Error
        #--------------------
        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)
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.groupbox_s_status, text='Return Code : ', anchor='nw').place(x=10, y=80, width=90, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        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)
        #----------------------------------------
        # Create LabelFrame for Master
        #----------------------------------------
        self.groupbox_m = tk.LabelFrame(self.root, text="Master")
        self.groupbox_m.place(x=390, y=13, width=120, height=309)
        #--------------------
        # Create Button for DioInit
        #--------------------
        self.button_m_dio_init = tk.Button(self.groupbox_m, text='DioInit')
        self.button_m_dio_init.place(x=8, y=15, width=100, height=19)
        #--------------------
        # Create Button for Set Condition
        #--------------------
        self.button_m_condition = tk.Button(self.groupbox_m, text='Set Condition...')
        self.button_m_condition.place(x=8, y=50, width=100, height=19)
        #--------------------
        # Create Button for Start Transfer
        #--------------------
        self.button_m_start = tk.Button(self.groupbox_m, text='Start Transfer')
        self.button_m_start.place(x=8, y=85, width=100, height=19)
        #--------------------
        # Create Button for Stop Transfer
        #--------------------
        self.button_m_stop = tk.Button(self.groupbox_m, text='Stop Transfer')
        self.button_m_stop.place(x=8, y=120, width=100, height=19)
        #--------------------
        # Create Button for Get Data
        #--------------------
        self.button_m_data = tk.Button(self.groupbox_m, text='Get Data...')
        self.button_m_data.place(x=8, y=155, width=100, height=19)
        #--------------------
        # Create Button for Transfer Count
        #--------------------
        self.button_m_count = tk.Button(self.groupbox_m, text='Transfer Count')
        self.button_m_count.place(x=8, y=190, width=100, height=19)
        #--------------------
        # Create Button for Transfer Status
        #--------------------
        self.button_m_status = tk.Button(self.groupbox_m, text='Transfer Status')
        self.button_m_status.place(x=8, y=225, width=100, height=19)
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_m_dio_exit = tk.Button(self.groupbox_m, text='DioExit')
        self.button_m_dio_exit.place(x=8, y=260, width=100, height=19)
        #----------------------------------------
        # Create LabelFrame for Slave
        #----------------------------------------
        self.groupbox_s = tk.LabelFrame(self.root, text="Slave")
        self.groupbox_s.place(x=515, y=13, width=120, height=309)
        #--------------------
        # Create Button for DioInit
        #--------------------
        self.button_s_dio_init = tk.Button(self.groupbox_s, text='DioInit')
        self.button_s_dio_init.place(x=8, y=15, width=100, height=19)
        #--------------------
        # Create Button for Set Condition
        #--------------------
        self.button_s_condition = tk.Button(self.groupbox_s, text='Set Condition...')
        self.button_s_condition.place(x=8, y=50, width=100, height=19)
        #--------------------
        # Create Button for Start Transfer
        #--------------------
        self.button_s_start = tk.Button(self.groupbox_s, text='Start Transfer')
        self.button_s_start.place(x=8, y=85, width=100, height=19)
        #--------------------
        # Create Button for Stop Transfer
        #--------------------
        self.button_s_stop = tk.Button(self.groupbox_s, text='Stop Transfer')
        self.button_s_stop.place(x=8, y=120, width=100, height=19)
        #--------------------
        # Create Button for Get Data
        #--------------------
        self.button_s_data = tk.Button(self.groupbox_s, text='Get Data...')
        self.button_s_data.place(x=8, y=155, width=100, height=19)
        #--------------------
        # Create Button for Transfer Count
        #--------------------
        self.button_s_count = tk.Button(self.groupbox_s, text='Transfer Count')
        self.button_s_count.place(x=8, y=190, width=100, height=19)
        #--------------------
        # Create Button for Transfer Status
        #--------------------
        self.button_s_status = tk.Button(self.groupbox_s, text='Transfer Status')
        self.button_s_status.place(x=8, y=225, width=100, height=19)
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_s_dio_exit = tk.Button(self.groupbox_s, text='DioExit')
        self.button_s_dio_exit.place(x=8, y=260, width=100, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=455, y=336, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    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):
        #----------------------------------------
        # Set the callback function for the master [DioInit] button
        #----------------------------------------
        self.button_m_dio_init.configure(command=command_m_dio_init)
        #----------------------------------------
        # Set the callback function for the master [Set Condition...] button
        #----------------------------------------
        self.button_m_condition.configure(command=command_m_condition)
        #----------------------------------------
        # Set the callback function for the master [Start Transfer] button
        #----------------------------------------
        self.button_m_start.configure(command=command_m_start)
        #----------------------------------------
        # Set the callback function for the master [Stop Transfer] button
        #----------------------------------------
        self.button_m_stop.configure(command=command_m_stop)
        #----------------------------------------
        # Set the callback function for the master [Get Data...] button
        #----------------------------------------
        self.button_m_data.configure(command=command_m_data)
        #----------------------------------------
        # Set the callback function for the master [Transfer Count] button
        #----------------------------------------
        self.button_m_count.configure(command=command_m_count)
        #----------------------------------------
        # Set the callback function for the master [Transfer Status] button
        #----------------------------------------
        self.button_m_status.configure(command=command_m_status)
        #----------------------------------------
        # Set the callback function for the master [DioExit] button
        #----------------------------------------
        self.button_m_dio_exit.configure(command=command_m_dio_exit)
        #----------------------------------------
        # Set the callback function for the slave [DioInit] button
        #----------------------------------------
        self.button_s_dio_init.configure(command=command_s_dio_init)
        #----------------------------------------
        # Set the callback function for the slave [Set Condition...] button
        #----------------------------------------
        self.button_s_condition.configure(command=command_s_condition)
        #----------------------------------------
        # Set the callback function for the slave [Start Transfer] button
        #----------------------------------------
        self.button_s_start.configure(command=command_s_start)
        #----------------------------------------
        # Set the callback function for the slave [Stop Transfer] button
        #----------------------------------------
        self.button_s_stop.configure(command=command_s_stop)
        #----------------------------------------
        # Set the callback function for the slave [Get Data...] button
        #----------------------------------------
        self.button_s_data.configure(command=command_s_data)
        #----------------------------------------
        # Set the callback function for the slave [Transfer Count] button
        #----------------------------------------
        self.button_s_count.configure(command=command_s_count)
        #----------------------------------------
        # Set the callback function for the slave [Transfer Status] button
        #----------------------------------------
        self.button_s_status.configure(command=command_s_status)
        #----------------------------------------
        # Set the callback function for the slave [DioExit] button
        #----------------------------------------
        self.button_s_dio_exit.configure(command=command_s_dio_exit)
        #----------------------------------------
        # Set the callback function for the slave [Exit] button
        #----------------------------------------
        self.button_exit.configure(command=command_exit)
 
    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

    #========================================
    # Run the main event loop
    #========================================
    def run(self):
        self.root.mainloop()