from Tkinter import *
import ttk
import tkFileDialog


def main():
    root = create_root()
    frame = create_frame(root)

    # ttk.Button(root, text="Hello World").grid()
    tree = create_treeview(frame)

    text = Text(frame, wrap='none')
    text.grid(column=0, row=2, sticky=(N, W, E, S), pady=12)

    v_scroll_bar = ttk.Scrollbar(frame, orient=VERTICAL, command=text.yview)
    set_mousewheel(text, lambda e: text.yview('scroll', e.delta < 0 and 1 or -1, 'units'))

    v_scroll_bar.grid(column=1, row=2, sticky=(N, W, E, S), pady=12)
    text['yscrollcommand'] = v_scroll_bar.set

    h_scroll_bar = ttk.Scrollbar(frame, orient=HORIZONTAL, command=text.xview)
    set_mousewheel(text, lambda e: text.xview('scroll', e.delta < 0 and 1 or -1, 'units'))

    h_scroll_bar.grid(column=0, row=3, sticky=(N, W, E, S))
    text['xscrollcommand'] = h_scroll_bar.set
    for i in range(1, 40):
        tree.insert("", i, i, text=str(i), values=('20200101', 'POS_STATECHANGED', '123456789', '2020-01-01T00:00:00.000', '2020-01-01T00:00:00.000'))

    root.mainloop()


def create_treeview(frame):
    tree = ttk.Treeview(frame, columns=('business_period', 'type', 'order_id', 'creation_date', 'event_date'))
    tree.grid(column=0, row=1, sticky=(N, W, E, S))
    tree.column('#0', width=50)
    tree.heading('#0', text='ID')
    tree.column('business_period', width=100)
    tree.heading('business_period', text='Business Period')
    tree.column('type', width=200)
    tree.heading('type', text='Event Type')
    tree.column('order_id', width=100)
    tree.heading('order_id', text='Order ID')
    tree.column('creation_date', width=150)
    tree.heading('creation_date', text='Creation Date')
    tree.column('event_date', width=150)
    tree.heading('event_date', text='Event Date')

    scroll_bar = ttk.Scrollbar(frame, orient=VERTICAL, command=tree.yview)
    set_mousewheel(tree, lambda e: tree.yview('scroll', e.delta < 0 and 1 or -1, 'units'))

    scroll_bar.grid(column=1, row=1, sticky=(N, W, E, S))
    tree['yscrollcommand'] = scroll_bar.set
    return tree


def create_frame(root):
    frame = ttk.Frame(root, padding="12 12 12 12")
    frame.grid(column=0, row=0, sticky=(N, W, E, S))
    frame.columnconfigure(0, weight=1)
    frame.rowconfigure(0, weight=1)
    frame.rowconfigure(1, weight=1)
    return frame


def create_root():
    root = Tk()
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    return root


def set_mousewheel(widget, command):
    """Active / deactivate mousewheel scrolling when
    cursor is over / not over the widget respectively."""
    widget.bind("<Enter>", lambda _: widget.bind_all('<MouseWheel>', lambda e: command(e)))
    widget.bind("<Leave>", lambda e: widget.unbind_all('<MouseWheel>'))


if __name__ == '__main__':
    main()
