-
Notifications
You must be signed in to change notification settings - Fork 4
/
tkinter_gui.py
142 lines (110 loc) · 4.12 KB
/
tkinter_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import time
import tkinter as tk
from extras.orders_async import *
from ordermanager_interface import OrderManager
from utils import log
logger = log.setup_custom_logger(__name__)
DEFAULT_INSTRUMENT = 'BTC-PERPETUAL'
LOOP_INTERVAL = 0.020 # ms
# ^^^^^ 0.020 works well (20ms)
# LOG_LEVEL = logging.DEBUG
# tk.Panel dimensions
HEIGHT = 700
WIDTH = 800
# geom = "%dx%d+%d+%d" % (WIDTH, HEIGHT, 0, -1000)
geom = "%dx%d" % (WIDTH, HEIGHT)
"""
EVENT CALLS
"""
def left_click(event):
print("left")
# bind leftclick to
# frame.bind("<Button-1>", left_click)
# ACTS AS THE ROOT: i.e. root = tk.Tk()
class WindowMarketbuy(tk.Tk):
def __init__(self, ordermanager=None):
if ordermanager:
self.om = ordermanager
else:
self.om = OrderManager(DEFAULT_INSTRUMENT)
BuySellButton.om = self.om
# Order.om = self.om
# tk.Tk (root) init
super().__init__()
self.geometry(geom)
# Order.instrument = self.om.instrument
# Order.client = self.om.client
### TKINTER ELEMENTS CREATION ###
self.frame = tk.Frame(self, bg='lightblue')
self.frame.place(relwidth=0.5, relheight=0.8, relx=0.1, rely=0.1)
# self.textbox = tk.Text(self.frame)
# self.textbox.place(relx=0, rely=0.5, relwidth=0.9, relheight=0.4)
# create buttons dict
self.buttons = []
### BUTTON METHODS ###
def place_buttons(self):
for b in self.buttons:
b.grid(ipadx=5, ipady=5, padx=5, pady=5)
def add_button(self, *buttons):
"""add one or multiple Button objects."""
for b in buttons:
self.buttons.append(b)
# button types
def new_market_button(self, side, amt):
btn = MarketButton(self.frame, side, amt)
return btn
def new_limitchase_button(self, side, amt):
btn = LimitChaseButton(self.frame, side, amt)
return btn
### RUNTIME METHODS ###
async def run(self):
t = time.time()
while True:
await self.run_tk()
# await self.om.run()
if time.time()-t > 3: # print every 3 seconds
logger.info("GUI loop running: @ time {0}".format(time.perf_counter()))
t = time.time()
async def run_tk(self):
"""
Substitutes for root.mainloop() in tkinter. (Makes it async, basically)
"""
# limitbuy.update_price()
self.update()
# print("GUI loop has run: @ time {0}".format(time.perf_counter()))
await asyncio.sleep(LOOP_INTERVAL)
class BuySellButton(tk.Button):
om = None
def __init__(self, master, side, amt):
super().__init__(master)
self.side = side
self.amt = amt
if side == 'buy':
# self.config(text="<orderType> Buy %d" % self.amt,
self.config(None,
bg="lightgreen",
activebackground="green")
elif side == 'sell':
# self.config(text="<orderType> Sell %d" % self.amt,
self.config(None,
bg="firebrick",
activebackground="maroon")
else:
raise ValueError("'side' parameter must be either 'buy' or 'sell'")
logger.info(f"{side.upper()} Button created, amt={amt}...with master={master}")
class MarketButton(BuySellButton):
def __init__(self, master, side, amt):
super().__init__(master, side, amt)
self.config(text=f"Market {self.side} {self.amt}",
command=lambda : self._make())
def _make(self):
ordertask = asyncio.create_task(self.om.market_order(self.side, self.amt))
logger.info(f"Market button ordertask created: {ordertask}")
class LimitChaseButton(BuySellButton):
def __init__(self, master, side, amt):
super().__init__(master, side, amt)
self.config(text=f"Limit CHASE {self.side} {self.amt}",
command=lambda : self._make())
def _make(self):
ordertask = asyncio.create_task(self.om.limit_chase(self.side, self.amt))
logger.info(f"LimitChase button ordertask created: {ordertask}")