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


#========================================
# TriggerUI class
#========================================
class TriggerUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self):
        #----------------------------------------
        # Start creating main screen
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.root.title('Trigger')
        self.root.minsize(500,315)
        #----------------------------------------
        # 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):
        #----------------------------------------
        # Initialize
        #----------------------------------------
        #--------------------
        # Create Label for Device Name
        #--------------------
        tk.Label(self.root, text='Device Name : ', anchor='nw').place(x=20, y=20, width=90, height=19)
        #--------------------
        # Create Entry for Device Name
        #--------------------
        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)
        #--------------------
        # Create Button for DioInit
        #--------------------
        self.button_dio_init = tk.Button(self.root, text='DioInit')
        self.button_dio_init.place(x=342, y=20, width=110, height=19)
        #----------------------------------------
        # Create LabelFrame for Trigger
        #----------------------------------------
        self.groupbox_trigger = tk.LabelFrame(self.root, text="Trigger")
        self.groupbox_trigger.place(x=15, y=60, width=470, height=155)
        #--------------------
        # Create Label for Bit0-7
        #--------------------
        tk.Label(self.groupbox_trigger, text='0', anchor='nw').place(x=122, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='1', anchor='nw').place(x=165, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='2', anchor='nw').place(x=208, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='3', anchor='nw').place(x=251, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='4', anchor='nw').place(x=294, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='5', anchor='nw').place(x=337, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='6', anchor='nw').place(x=380, y=1, width=10, height=19)
        tk.Label(self.groupbox_trigger, text='7', anchor='nw').place(x=423, y=1, width=10, height=19)
        #--------------------
        # Create Label for Up-count
        #--------------------
        tk.Label(self.groupbox_trigger, text='Up-count : ', anchor='nw').place(x=5, y=25, width=100, height=19)
        #--------------------
        # Create Entry for Bit0-7 Up-count
        #--------------------
        self.textbox_up0_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up0_var, justify=tk.RIGHT).place(x=110, y=25, width=36, height=19)
        self.textbox_up1_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up1_var, justify=tk.RIGHT).place(x=153, y=25, width=36, height=19)
        self.textbox_up2_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up2_var, justify=tk.RIGHT).place(x=196, y=25, width=36, height=19)
        self.textbox_up3_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up3_var, justify=tk.RIGHT).place(x=239, y=25, width=36, height=19)
        self.textbox_up4_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up4_var, justify=tk.RIGHT).place(x=282, y=25, width=36, height=19)
        self.textbox_up5_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up5_var, justify=tk.RIGHT).place(x=325, y=25, width=36, height=19)
        self.textbox_up6_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up6_var, justify=tk.RIGHT).place(x=368, y=25, width=36, height=19)
        self.textbox_up7_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_up7_var, justify=tk.RIGHT).place(x=411, y=25, width=36, height=19)
        #--------------------
        # Create Label for Down-count
        #--------------------
        tk.Label(self.groupbox_trigger, text='Down-count : ', anchor='nw').place(x=5, y=53, width=100, height=19)
        #--------------------
        # Create Entry for Bit0-7 Up-count
        #--------------------
        self.textbox_down0_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down0_var, justify=tk.RIGHT).place(x=110, y=53, width=36, height=19)
        self.textbox_down1_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down1_var, justify=tk.RIGHT).place(x=153, y=53, width=36, height=19)
        self.textbox_down2_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down2_var, justify=tk.RIGHT).place(x=196, y=53, width=36, height=19)
        self.textbox_down3_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down3_var, justify=tk.RIGHT).place(x=239, y=53, width=36, height=19)
        self.textbox_down4_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down4_var, justify=tk.RIGHT).place(x=282, y=53, width=36, height=19)
        self.textbox_down5_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down5_var, justify=tk.RIGHT).place(x=325, y=53, width=36, height=19)
        self.textbox_down6_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down6_var, justify=tk.RIGHT).place(x=368, y=53, width=36, height=19)
        self.textbox_down7_var = tk.StringVar(self.groupbox_trigger)
        tk.Entry(self.groupbox_trigger, textvariable=self.textbox_down7_var, justify=tk.RIGHT).place(x=411, y=53, width=36, height=19)
        #--------------------
        # Create Label for Valid / Invalid
        #--------------------
        tk.Label(self.groupbox_trigger, text='Valid / Invalid : ', anchor='nw').place(x=5, y=80, width=110, height=19)
        #--------------------
        # Create Checkbutton for Valid / Invalid
        #--------------------
        self.checkbox0_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox0_var.set(1)
        self.checkbox0 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox0_var)
        self.checkbox0.place(x=112, y=80, width=36, height=19)
        self.checkbox1_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox1_var.set(1)
        self.checkbox1 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox1_var)
        self.checkbox1.place(x=155, y=80, width=36, height=19)
        self.checkbox2_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox2_var.set(1)
        self.checkbox2 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox2_var)
        self.checkbox2.place(x=198, y=80, width=36, height=19)
        self.checkbox3_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox3_var.set(1)
        self.checkbox3 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox3_var)
        self.checkbox3.place(x=241, y=80, width=36, height=19)
        self.checkbox4_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox4_var.set(1)
        self.checkbox4 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox4_var)
        self.checkbox4.place(x=284, y=80, width=36, height=19)
        self.checkbox5_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox5_var.set(1)
        self.checkbox5 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox5_var)
        self.checkbox5.place(x=327, y=80, width=36, height=19)
        self.checkbox6_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox6_var.set(1)
        self.checkbox6 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox6_var)
        self.checkbox6.place(x=370, y=80, width=36, height=19)
        self.checkbox7_var = tk.IntVar(self.groupbox_trigger)
        self.checkbox7_var.set(1)
        self.checkbox7 = tk.Checkbutton(self.groupbox_trigger, variable=self.checkbox7_var)
        self.checkbox7.place(x=413, y=80, width=36, height=19)
        #--------------------
        # Create Button for Trigger Start
        #--------------------
        self.button_trigger = tk.Button(self.groupbox_trigger, text='Trigger Start')
        self.button_trigger.place(x=325, y=110, width=110, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Label for Return Code
        #--------------------
        tk.Label(self.root, text='Return Code : ', anchor='nw').place(x=20, y=230, width=90, height=19)
        #--------------------
        # Create Entry for Return Code
        #--------------------
        self.textbox_return_string_var= tk.StringVar(self.root)
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=105, y=230, width=225, height=19)
        #--------------------
        # Create Button for DioExit
        #--------------------
        self.button_dio_exit = tk.Button(self.root, text='DioExit')
        self.button_dio_exit.place(x=342, y=230, width=110, height=19)
        #--------------------
        # Create Button for Exit
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=342, y=260, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_check0,
                       command_check1,
                       command_check2,
                       command_check3,
                       command_check4,
                       command_check5,
                       command_check6,
                       command_check7,
                       command_trigger,
                       command_dio_exit,
                       command_exit):
        #----------------------------------------
        # Set the callback function for the [DioInit] button
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # Set the callback function for the [Bit 0 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox0.configure(command=command_check0)
        #----------------------------------------
        # Set the callback function for the [Bit 1 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox1.configure(command=command_check1)
        #----------------------------------------
        # Set the callback function for the [Bit 2 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox2.configure(command=command_check2)
        #----------------------------------------
        # Set the callback function for the [Bit 3 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox3.configure(command=command_check3)
        #----------------------------------------
        # Set the callback function for the [Bit 4 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox4.configure(command=command_check4)
        #----------------------------------------
        # Set the callback function for the [Bit 5 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox5.configure(command=command_check5)
        #----------------------------------------
        # Set the callback function for the [Bit 6 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox6.configure(command=command_check6)
        #----------------------------------------
        # Set the callback function for the [Bit 7 Valid / Invalid] checkbox
        #----------------------------------------
        self.checkbox7.configure(command=command_check7)
        #----------------------------------------
        # Set the callback function for the [Trigger Start] button
        #----------------------------------------
        self.button_trigger.configure(command=command_trigger)
        #----------------------------------------
        # Set the callback function for the [DioExit] button
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_exit)
        #----------------------------------------
        # Set the callback function for the [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()