Ошибка спустя 10 минут работы в Tkinter. Python
Здравствуйте уважаемые форумчане! У меня возникла проблема, которую не могу решить уже неделю. Есть интерфейс, написанный на Tkinter. При запуске, всё отрабатывает как надо, но спустя 10-15 минут работы виджеты, которые обновляются с разной периодичностью, сбиваются в левом верхнем углу. При том, что нет ни какой замены координат, или пересечений с глобальной переменной, т.к. работает во многопоточном режиме.
Прошу помощи. Сам, на данный момент, увы, бессилен.
import tkinter as tk
import customtkinter as ctk
from tkinter import ttk
import threading
from user import *
from ttkwidgets import CheckboxTreeview
from BTC_future import BTC_list_instruments_future, BTC_future
from BTC_option import BTC_list_instruments_option, list_name_BTC, instrument_list_B
from ETH_future import ETH_list_instruments_future, ETH_future
from ETH_option import ETH_list_instruments_option, list_name_ETH, instrument_list
from data_add_position import book_instruments_add_position, open_auth_book
from subscribe_ETH import open_auth_ETH
from volatility_BTC import open_vol_BTC
from volatility_ETH import open_vol_ETH
from volatility_BTC import PRICE_VOL
from add_position_update import add_position_restart
from PNL_ETH import book_instruments_position, open_auth_PNL, instrumetn_name, instrumetn_name_add
root = ctk.CTk()
logo = tk.PhotoImage(file='logo.png')
root.iconphoto(False, logo)
root.config(bg='#00cfbe')
root.geometry(f'800x600+350+150')
locker = threading.RLock()
open_order_book_add = []
quantiti = {}
def root_title():
time.sleep(3)
user = user_acc['us_name']
root.title(f'Deribit.com (Account: {user} )')
tiker = tk.Label(root, text=f'BTC $0 VOL: 0 ETH $ 0 VOL: 0 ',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
tiker.place(x=10, y=0)
def tiker_func():
time.sleep(2)
while True:
locker.acquire()
BTC = PRICE_VOL['price_btc']
ETH = PRICE_VOL['price_eth']
BTC_vol = PRICE_VOL['vol_btc']
ETH_vol = PRICE_VOL['vol_eth']
BTC_pr = tk.Label(root, text=f'${BTC}', bg='#00cfbe', fg='black', font=('Arial',10,'bold'))
BTC_pr.place(x=40, y=0)
BTC_vol = tk.Label(root, text=f'{BTC_vol}%', bg='#00cfbe',
fg='black', font=('Arial', 10, 'bold'))
BTC_vol.place(x=145, y=0)
ETH_pr = tk.Label(root, text=f'${ETH}', bg='#00cfbe',
fg='black', font=('Arial', 10, 'bold'))
ETH_pr.place(x=230, y=0)
ETH_vol = tk.Label(root, text=f'{ETH_vol}% ', bg='#00cfbe',
fg='black', font=('Arial', 10, 'bold'))
ETH_vol.place(x=325, y=0)
locker.release()
time.sleep(0.3)
def balance(com):
threading.Thread(target=equity_main_BTC).start()
threading.Thread(target=equity_main_ETH).start()
threading.Thread(target=equity_main_USDC).start()
threading.Thread(target=Available_BTC).start()
threading.Thread(target=Available_ETH).start()
threading.Thread(target=Available_USDC).start()
threading.Thread(target=root_title).start()
# threading.Thread(target=PNL_MARK_ETH, args=(com,)).start()
# threading.Thread(target=PNL_BEST_ETH, args=(com,)).start()
# threading.Thread(target=PNL_WORST_ETH, args=(com,)).start()
# def equti_main():
Equity = tk.Label(root, text='Equity:',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
Equity.place(x=10, y=25)
def equity_main_BTC():
time.sleep(3)
while True:
eq_btc = user_acc['BTC_equity']
pr_btc = PRICE_VOL['price_btc']
p = pr_btc * eq_btc
p = round(p, 2)
if p > 1:
Equity_BTC = tk.Label(root, text=f'{eq_btc} BTC ({p}$);',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
Equity_BTC.place(x=60, y=25)
time.sleep(30)
def equity_main_ETH():
time.sleep(3)
while True:
eq_eth = user_acc['ETH_equity']
pr_eth = PRICE_VOL['price_eth']
p = pr_eth * eq_eth
p = round(p, 2)
if p > 1:
Equity_ETH = tk.Label(root, text=f'{eq_eth} ETH (${p})',
bg='#00cfbe',
fg='black',
font=('Arial', 10, 'bold')
)
Equity_ETH.place(x=195, y=25)
time.sleep(30)
def equity_main_USDC():
time.sleep(3)
while True:
eq_USDC = user_acc['USDC_equity']
if eq_USDC > 1:
Equity_USDC = tk.Label(root, text=f'{eq_USDC} usdc',
bg='#00cfbe',
fg='black',
font=('Arial', 10, 'bold')
)
Equity_USDC.place(x=300, y=25)
time.sleep(30)
# def Available_main():
Available = tk.Label(root, text='Available:',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
Available.place(x=10, y=50)
def Available_BTC():
time.sleep(3)
while True:
av_btc = user_acc['BTC_available']
pr_btc = PRICE_VOL['price_btc']
p = pr_btc * av_btc
p = round(p, 2)
if p > 1:
Available_btc = tk.Label(root, text=f'{av_btc} BTC ({p}$);',
bg='#00cfbe',
fg='black',
font=('Arial', 10, 'bold')
)
Available_btc.place(x=80, y=50)
time.sleep(30)
def Available_ETH():
time.sleep(3)
while True:
av_eth = user_acc['ETH_available']
pr_eth = PRICE_VOL['price_eth']
p = pr_eth * av_eth
p = round(p, 2)
if p > 1:
Available_eth = tk.Label(root, text=f'{av_eth} ETH (${p})',
bg='#00cfbe',
fg='black',
font=('Arial', 10, 'bold')
)
Available_eth.place(x=215, y=50)
time.sleep(30)
def Available_USDC():
time.sleep(3)
while True:
av_USDC = user_acc['USDC_available']
if av_USDC > 1:
Available_usdc = tk.Label(root, text='20 000 usdc',
bg='#00cfbe',
fg='black',
font=('Arial', 10, 'bold')
)
Available_usdc.place(x=325, y=50)
time.sleep(30)
def PNL_MARK_BTC(com):
for instrument in open_order_book_add:
list_pnl = []
list_qnt = []
data = book_instruments_position[instrument]
pnl = data['PNL']
qnt = data['size']
list_pnl.append(pnl)
list_qnt.append(qnt)
pnl1 = sum(list_pnl)
qnt1 = sum(list_qnt)
com = 0.1
PNL = pnl1 - (qnt1 * com)
pnl_mark = tk.Label(root, text='0.5 BTC ( 10 000$);',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_mark.place(x=10, y=75)
def PNL_MARK_ETH(com):
time.sleep(3)
while True:
locker.acquire()
list_pnl = []
list_qnt = []
for i in open_order_book_add:
data = book_instruments_position[i]
pnl = data['PNL']
qnt = data['size']
list_pnl.append(pnl)
list_qnt.append(qnt)
com = com / 100
com = round(com, 6)
pnl1 = round(sum(list_pnl), 6)
qnt1 = sum(list_qnt)
PNL = round(pnl1 - (qnt1 * com), 6)
price = PRICE_VOL['price_eth']
price = round(price * PNL, 2)
pnl_mark = tk.Label(root, text=f'{PNL} ETH ({price} $)',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_mark.place(x=80, y=75)
locker.release()
time.sleep(0.1)
def PNL_BEST_BTC(com):
pnl_best = tk.Label(root, text=' 0.51 BTC ( 10 100$);',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_best.place(x=10, y=100)
def PNL_BEST_ETH(com):
time.sleep(3.5)
while True:
locker.acquire()
list_pnl = []
list_qnt = []
for i in open_order_book_add:
data = book_instruments_position[i]
data1 = book_instruments_add_position[i]
average = data['average_price']
qnt = data['size']
bid = data1['bid']
ask = data1['ask']
if qnt > 0:
pnl = average - ask
list_pnl.append(pnl)
else:
pnl = average - bid
list_pnl.append(pnl)
list_qnt.append(abs(qnt))
com = com / 100
pnl1 = round(sum(list_pnl), 6)
qnt1 = sum(list_qnt)
PNL = round(pnl1 - (qnt1 * com), 6)
price = PRICE_VOL['price_eth']
price = round(price * PNL, 2)
pnl_best = tk.Label(root, text=f'{PNL} ETH ({price} $)',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_best.place(x=80, y=100)
locker.release()
time.sleep(0.1)
def PNL_WORST_BTC(com):
pnl_worst = tk.Label(root, text='0.49 BTC (9 900$);',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_worst.place(x=10, y=125)
def PNL_WORST_ETH(com):
time.sleep(4)
while True:
locker.acquire()
list_pnl = []
list_qnt = []
for i in open_order_book_add:
data = book_instruments_position[i]
data1 = book_instruments_add_position[i]
average = data['average_price']
qnt = data['size']
bid = data1['bid']
ask = data1['ask']
if qnt > 0:
pnl = average - bid
list_pnl.append(pnl)
else:
pnl = average - ask
list_pnl.append(pnl)
list_qnt.append(abs(qnt))
com = com / 100
pnl1 = round(sum(list_pnl), 6)
qnt1 = sum(list_qnt)
PNL = round(pnl1 - (qnt1 * com), 6)
price = PRICE_VOL['price_eth']
price = round(price * PNL, 2)
pnl_worst = tk.Label(root, text=f'{PNL} ETH ({price} $)',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_worst.place(x=85, y=125)
locker.release()
time.sleep(0.1)
pnl_m = tk.Label(root, text=f'PnL Mark:',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_m.place(x=10, y=75)
pnl_b = tk.Label(root, text=f'PnL Best:',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_b.place(x=10, y=100)
pnl_w = tk.Label(root, text=f'PnL Worst:',
bg='#00cfbe',
fg='black',
font=('Arial',10,'bold')
)
pnl_w.place(x=10, y=125)
comm = tk.Label(root, text='Comm', font=('Arial',10,'bold'), bg='#00cfbe').place(x=400)
comm_1 = tk.Entry(root, width=6)
comm_1.place(x=450)
percent = tk.Label(root, text='%', bg='#00cfbe').place(x=490)
close_mark = tk.Button(root, text='Close',
bg='#ffffff',
fg='black',
font=('Arial', 9,'bold'),
activebackground='red'
)
close_best = tk.Button(root, text='Close',
bg='#ffffff',
fg='black',
font=('Arial', 9,'bold'),
activebackground='red'
)
close_worst = tk.Button(root, text='Close',
bg='#ffffff',
fg='black',
font=('Arial', 9,'bold'),
activebackground='red'
)
close_mark.place(x=350, y=75)
close_best.place(x=350, y=100)
close_worst.place(x=350, y=125)
def on_off():
var = s_var.get()
com = float(comm_1.get())
if var == 'Вкл':
print('Терминал включился')
thr = threading.Thread(target=open_auth).start()
thr1 = threading.Thread(target=open_auth_ETH).start()
thr2 = threading.Thread(target=open_vol_ETH).start()
thr3 = threading.Thread(target=open_vol_BTC).start()
thr4 = threading.Thread(target=tiker_func).start()
thr5 = threading.Thread(target=balance, args=(com,)).start()
thr6 = threading.Thread(target=frame_selected).start()
thr7 = threading.Thread(target=open_auth_book).start()
thr8 = threading.Thread(target=open_auth_PNL).start()
thr9 = threading.Thread(target=get_position).start()
BTC_list_instruments_future()
BTC_list_instruments_option()
ETH_list_instruments_future()
ETH_list_instruments_option()
# equti_main()
# Available_main()
else:
print('Терминал выключился')
# def cycle():
# get_position()
def get_position():
time.sleep(3)
while True:
if instrumetn_name != []:
for inst in instrumetn_name:
name = inst
book = book_instruments_position[name]
qnt = book['size']
mark_pr = book['mark_price']
average_pr = book['average_price']
book_i = book_instruments_add_position[name]
bid_pr = book_i['bid']
ask_pr = book_i['ask']
iv = book_i['mark_iv']
frame.insert('', 'end', values=(f'{name}', f'{qnt}', f'{mark_pr}', f'{average_pr}', f'{bid_pr}', f'{ask_pr}', f'{iv}'))
else:
print(f'Открытых позиций нет.')
time.sleep(0.1)
add_currency = ['BTC', 'ETH']
add_inst = ['Options', 'Furute']
add_direction = ['Call', 'Put']
add_expire = ['JAN', 'FEB', 'MAR', 'JUN', 'SEP','DEC',]
switch_1 = ctk.CTkSwitch(master=root, text='', bg_color='#00cfbe', progress_color='green', fg_color='red', button_color='white')
switch_1.place(x=10, y=160)
take = ctk.CTkLabel(root, text='Take', bg_color='#00cfbe', width=5, height=5)
take.place(x=60, y=162)
take1 = ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10)
take1.place(x=100, y=160)
vola = ctk.CTkLabel(root, text='Vola', bg_color='#00cfbe', width=5, height=5)
vola.place(x=170, y=162)
vola1 = ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10)
vola1.place(x=210, y=160)
# switch_2 = ctk.CTkSwitch(master=root, progress_color='green', fg_color='red', button_color='white')
# switch_2.place(x=10, y=210)
#
# switch_3 = ctk.CTkSwitch(master=root, progress_color='green', fg_color='red', button_color='white')
# switch_3.place(x=10, y=240)
s_var = ctk.StringVar(value='Выкл')
switch_on = ctk.CTkSwitch(master=root, variable=s_var, textvariable=s_var, bg_color='#00cfbe', progress_color='green', fg_color='red', button_color='white',, offvalue='Выкл',
command=on_off, height=25, width=40)
switch_on.place(x=700, y=5)
position = tk.Label(root, text = 'Pisition:', bg='#00cfbe', fg='black', font=('Arial', 10,'bold')).place(x=10, y=185)
add_position = tk.Label(root, text = 'Add Pisition:', bg='#00cfbe', fg='black', font=('Arial', 10,'bold')).place(x=10, y=345)
def selected_instrument(event):
c = combo.get()
c1 = combo1.get()
c2 = combo2.get()
c3 = combo3.get()
print(c, c1, c2, c3)
if c == 'BTC' and c1 == 'Options':
if c2 == 'Call':
list_name_BTC(c3, c2)
else:
list_name_BTC(c3, c2)
elif c == 'ETH' and c1 == 'Options':
if c2 == 'Call':
list_name_ETH(c3, c2)
else:
list_name_ETH(c3, c2)
return inst_name()
def inst_name():
cur = combo.get()
op_future = combo1.get()
if op_future == 'Options':
if cur == 'BTC':
combo4['values'] = instrument_list_B[0]
else:
combo4['values'] = instrument_list[0]
else:
if cur == 'BTC':
combo4['values'] = BTC_future
else:
combo4['values'] = ETH_future
return
combo = ttk.Combobox(root, width=5, values=add_currency)
combo.place(x=10, y=365)
combo1 = ttk.Combobox(root, width=10, values=add_inst)
combo1.place(x=70, y=365)
combo2 = ttk.Combobox(root, width=5, values=add_direction)
combo2.place(x=160, y=365)
combo3 = ttk.Combobox(root, width=10, values=add_expire)
combo3.place(x=220, y=365)
combo4 = ttk.Combobox(root, width=25)
combo4.place(x=310, y=365)
combo5 = ttk.Entry(root, width=10)
combo5.place(x=490, y=365)
combo3.bind('<<ComboboxSelected>>', selected_instrument)
combo2.bind('<<ComboboxSelected>>', selected_instrument)
combo1.bind('<<ComboboxSelected>>', selected_instrument)
def ok_btn():
name = combo4.get()
instrumetn_name_add.append(name)
qnt = combo5.get()
ii = 0
time.sleep(1)
data = book_instruments_add_position[name]
mark = data['mark_price']
bid = data['bid']
ask = data['ask']
iv = data['mark_iv']
qnt1 = 0
i = 1
quantiti = {name:{'qnt':qnt, 'shift':i}}
frame1.insert('', 'end', values=(f'{name}', f'{qnt}/{qnt1}', f'{mark}', f'{bid}', f'{ask}', f'{iv}', f'{i}'))
# def cycle():
# thr10 = threading.Thread(target=frame_selected).start()
def frame_selected():
while True:
str_name = frame.get_checked()
for str in str_name:
meaning = frame.item(str, option='values')
# print(meaning[0])
open_order_book_add.append(meaning[0])
list(set(open_order_book_add))
time.sleep(0.1)
def frame1_selected():
delet = frame1.selection()[0]
test = frame1.get_children()
btn = ctk.CTkButton(root, bg_color='#00cfbe', text_color='Black', width=10, hover_color='green', fg_color='white', height=10, text='Open Mark').place(x=10, y=570)
btn1 = ctk.CTkButton(root, bg_color='#00cfbe', text_color='Black', width=10, hover_color='green', fg_color='white', height=10, text='Open Best').place(x=95, y=570)
btn2 = ctk.CTkButton(root, bg_color='#00cfbe', text_color='Black', width=10, hover_color='green', fg_color='white', height=10, text='Open Worst').place(x=175, y=570)
ctk.CTkLabel(root, text='Spread price:', bg_color='#00cfbe', width=10, height=10).place(x=10, y=530)
ctk.CTkLabel(root, bg_color='#00cfbe', text='=', width=10, height=10).place(x=280, y=530)
ctk.CTkEntry(root, bg_color='#00cfbe', width=80, height=10).place(x=300, y=530)
ctk.CTkLabel(root, text='Low price:', bg_color='#00cfbe', width=10, height=10).place(x=270, y=570)
ctk.CTkLabel(root, text='High price:', bg_color='#00cfbe', width=10, height=10).place(x=410, y=570)
ctk.CTkLabel(root, text='Vola:', bg_color='#00cfbe', width=10, height=10).place(x=550, y=570)
vola_oo = 0
def vola_o():
global vola_oo
if vola_oo % 2:
btn_auto.configure(fg_color='red', hover_color='green')
vola_oo += 1
x = frame1.selection()
for xx in x:
y = frame1.item(xx, option='values')
print(y)
else:
btn_auto.configure(fg_color='green', hover_color='red')
vola_oo += 1
ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10).place(x=330, y=570)
ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10).place(x=470, y=570)
ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10).place(x=580, y=570)
btn_auto = ctk.CTkButton(root, width=10, command=vola_o, bg_color='#00cfbe', hover_color='green', text_color='Black', fg_color='red', height=10, text='Auto')
btn_auto.place(x=650, y=570)
ctk.CTkLabel(root, text='IceBerg', bg_color='#00cfbe', width=20, height=15).place(x=450, y=75)
ice = ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10).place(x=520, y=75)
shift_oo = 0
def shift_o():
global shift_oo
if shift_oo % 2 :
sh = shift.get()
btn_shift.configure(fg_color='red', hover_color='green')
chek_shift = 1
shift_oo += 1
x = frame1.selection()
else:
btn_shift.configure(fg_color='green', hover_color='red')
shift_oo += 1
chek_shift = 0
ctk.CTkLabel(root, text='Shift', bg_color='#00cfbe', width=20, height=15).place(x=450, y=100)
shift = ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10)
shift.place(x=520, y=100)
ctk.CTkLabel(root, text='sec.', bg_color='#00cfbe', width=20, height=15).place(x=720, y=100)
sec = ctk.CTkEntry(root, bg_color='#00cfbe', width=50, height=10).place(x=660, y=100)
btn_shift = ctk.CTkButton(root, width=10, hover_color='green', command=frame1_selected, bg_color='#00cfbe', text_color='Black', fg_color='red', height=10, text='Auto Shift')
btn_shift.place(x=580, y=100)
ctk.CTkEntry(root, bg_color='#00cfbe', width=40, height=10).place(x=120, y=530)
ctk.CTkComboBox(root, bg_color='#00cfbe', width=50, height=25, values='+-/*').place(x=165, y=530)
ctk.CTkEntry(root, bg_color='#00cfbe', width=40, height=10).place(x=220, y=530)
frame = CheckboxTreeview(root, height=5, show='tree headings', columns=('ID', '1', '3', '4', '5', '6', '7'))
frame.place(x=10, y=210)
# frame = ttk.Treeview(root, height=5, show='headings', selectmode='browse', columns=('ID', '1', '3', '4', '5', '6', '7'))
# frame.place(y=210, x=10)
frame1 = ttk.Treeview(root, height=5, show='headings', columns=('ID', '1','4', '5', '6', '7', '8'))
frame1.place(y=390, x=10)
btn_ok = ctk.CTkButton(root, bg_color='#00cfbe', command=ok_btn, text='Ok', width=5, height=5).place(x=560, y=365)
scrol = ctk.CTkScrollbar(frame, orientation='vertical', height=80, fg_color='white', command=frame.yview())
frame.configure(yscrollcommand=scrol.set)
scrol.place(y=30, x=655)
scrol = ctk.CTkScrollbar(frame1, orientation='vertical', height=80, fg_color='white', command=frame.yview())
frame1.configure(yscrollcommand=scrol.set)
scrol.place(y=30, x=655)
frame.heading('#0', text='On/Off')
frame.heading('ID', text='Инструмент', anchor='center')
frame.heading('1', text='Объём', anchor='center')
# frame.heading('2', text='PUT/CALL', anchor='center')
frame.heading('3', text='Avr', anchor='center')
frame.heading('4', text='Mark', anchor='center')
frame.heading('5', text='Bid', anchor='center')
frame.heading('6', text='Ask', anchor='center')
frame.heading('7', text='IV(mark)', anchor='center')
frame.column('#0', width=70, anchor='center')
frame.column('ID', width=190, anchor='center')
frame.column('1', width=50, anchor='center')
# frame.column('2', width=80, anchor='center')
frame.column('3', width=60, anchor='center')
frame.column('4', width=80, anchor='center')
frame.column('5', width=80, anchor='center')
frame.column('6', width=80, anchor='center')
frame.column('7', width=60, anchor='center')
frame1.heading('ID', text='Инструмент', anchor='center')
frame1.heading('1', text='Объём', anchor='center')
# frame1.heading('2', text='PUT/CALL', anchor='center')
# frame1.heading('3', text='Avr', anchor='center')
frame1.heading('4', text='Mark', anchor='center')
frame1.heading('5', text='Bid', anchor='center')
frame1.heading('6', text='Ask', anchor='center')
frame1.heading('7', text='IV(mark)', anchor='center')
frame1.heading('8', text='Shift', anchor='center')
# frame1.column('#0', width=70, anchor='center')
frame1.column('ID', width=200, anchor='center')
frame1.column('1', width=50, anchor='center')
# frame1.column('2', width=80, anchor='center')
# frame1.column('3', width=50, anchor='center')
frame1.column('4', width=100, anchor='center')
frame1.column('5', width=100, anchor='center')
frame1.column('6', width=100, anchor='center')
frame1.column('7', width=60, anchor='center')
frame1.column('8', width=60, anchor='center')
root.mainloop()
Источник: Stack Overflow на русском