adicionado programa em python de interface gráfica e imagens

parent 3ed60a75
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from PIL import Image, ImageTk
import time
import serial.tools.list_ports
#Define janela de tela cheia
root = Tk()
root.state('zoomed')
root.resizable(False, False)
canvas_big = Canvas(root, bg='black')
canvas_big.place(relx=0, rely=0, relwidth=1, relheight=1)
#--------------------------------------------------Tela 1 (lateral esquerda)-------------------------------------------------------
canvas1 = Canvas(canvas_big, bg='pale turquoise')
canvas1.place(relx=0.025, rely=0, relwidth=0.3, relheight=0.7)
#Seleção de protocolo:
prot_text1 = Label(canvas1, text="Selecione o primeiro protocolo:", bg='pale turquoise')
prot_text1.place(relx=0, rely=0, relwidth=1, relheight=0.075)
protocol_list1 = Listbox(canvas1)
protocol_list1.configure(justify=CENTER)
protocol_list1.insert(1,"Basal unipolar")
protocol_list1.insert(2,"Basal bipolar")
protocol_list1.insert(3,"TBS unipolar")
protocol_list1.insert(4,"TBS bipolar")
protocol_list1.insert(5,"PBS")
protocol_list1.place(relx=0.1, rely=0.075, relwidth=0.5, relheight=0.15)
button_prot_1 = Button(canvas1, text="Selecionar", command = lambda:select_protocol(canvas1, protocol_list1.get(ACTIVE)))
button_prot_1.place(relx=0.6, rely=0.075, relwidth=0.4, relheight=0.15)
#--------------------------------------------------Tela 2 (meio)-------------------------------------------------------
canvas2 = Canvas(canvas_big, bg='pale turquoise')
canvas2.place(relx=0.35, rely=0, relwidth=0.3, relheight=0.7)
#Seleção de protocolo:
prot_text2 = Label(canvas2, text="Selecione o segundo protocolo:", bg='pale turquoise')
prot_text2.place(relx=0, rely=0, relwidth=1, relheight=0.075)
protocol_list2 = Listbox(canvas2)
protocol_list2.configure(justify=CENTER)
protocol_list2.insert(1,"Basal unipolar")
protocol_list2.insert(2,"Basal bipolar")
protocol_list2.insert(3,"TBS unipolar")
protocol_list2.insert(4,"TBS bipolar")
protocol_list2.insert(5,"PBS")
protocol_list2.place(relx=0.1, rely=0.075, relwidth=0.5, relheight=0.15)
button_prot_2 = Button(canvas2, text="Selecionar", command = lambda:select_protocol(canvas2, protocol_list2.get(ACTIVE)))
button_prot_2.place(relx=0.6, rely=0.075, relwidth=0.4, relheight=0.15)
#--------------------------------------------------Tela 3 (lateral direita)-------------------------------------------------------
canvas3 = Canvas(canvas_big, bg='pale turquoise')
canvas3.place(relx=0.675, rely=0, relwidth=0.3, relheight=0.7)
#Seleção de protocolo:
prot_text3 = Label(canvas3, text="Selecione o terceiro protocolo:", bg='pale turquoise')
prot_text3.place(relx=0, rely=0, relwidth=1, relheight=0.075)
protocol_list3 = Listbox(canvas3)
protocol_list3.configure(justify=CENTER)
protocol_list3.insert(1,"Basal unipolar")
protocol_list3.insert(2,"Basal bipolar")
protocol_list3.insert(3,"TBS unipolar")
protocol_list3.insert(4,"TBS bipolar")
protocol_list3.insert(5,"PBS")
protocol_list3.place(relx=0.1, rely=0.075, relwidth=0.5, relheight=0.15)
button_prot_3 = Button(canvas3, text="Selecionar", command = lambda:select_protocol(canvas3, protocol_list3.get(ACTIVE)))
button_prot_3.place(relx=0.6, rely=0.075, relwidth=0.4, relheight=0.15)
#--------------------------------------------------Tela de avisos (log) e envios ao microcontrolador--------------------------------------------
canvas4 = Canvas(canvas_big, bg='blue')
canvas4.place(relx=0.025, rely=0.725, relwidth=0.95, relheight=0.25)
log = Text(canvas4)
log.place(relx=0.025, rely=0.025, relwidth=0.5, relheight=0.8)
button_con = Button(canvas4, text="Detectar e conectar Arduino", command = lambda:detect_arduino())
button_con.place(relx=0.6, rely=0.075, relwidth=0.4, relheight=0.15)
button_send = Button(canvas4, text="Enviar", command = lambda:enviar_arduino())
button_send.place(relx=0.6, rely=0.5, relwidth=0.4, relheight=0.15)
def writeToLog(msg):
numlines = log.index('end - 1 line').split('.')[0]
log['state'] = 'normal'
if numlines==9:
log.delete(1.0, 2.0)
if log.index('end-1c')!='1.0':
log.insert('end', '\n')
log.insert('end', msg)
log['state'] = 'disabled'
#Função para definicação de protocolo:
def select_protocol(canvas, protocol):
list = canvas.place_slaves()
for l in list:
if(l!=prot_text1 and l!=prot_text2 and l!=prot_text3 and l!=protocol_list1 and l!=protocol_list2 and l!=protocol_list3 and l!=button_prot_1 and l!=button_prot_2 and l!=button_prot_3):
l.destroy()
if (protocol=="Basal unipolar"):
prot_image = Image.open("protocolo_basal.PNG")
prot_photo = ImageTk.PhotoImage(prot_image.resize((420,110)))
prot_label = Label(canvas, image=prot_photo)
prot_label.image = prot_photo
prot_label.place(relx=0.0, rely=0.225, relwidth=1, relheight=0.25)
T_text = Label(canvas, text="Intervalo T (us):")
T_text.place(relx=0, rely=0.475, relwidth=0.4, relheight=0.075)
T_entry = Entry(canvas, justify=CENTER)
T_entry.place(relx=0.4, rely=0.475, relwidth=0.3, relheight=0.075)
T_lim = Label(canvas, text="Limites: 5us-1000us")
T_lim.place(relx=0.6, rely=0.475, relwidth=0.4, relheight=0.075)
IPI_text = Label(canvas, text="Intervalo IPI (ms):")
IPI_text.place(relx=0, rely=0.55, relwidth=0.4, relheight=0.075)
IPI_entry = Entry(canvas, justify=CENTER)
IPI_entry.place(relx=0.4, rely=0.55, relwidth=0.3, relheight=0.075)
IPI_lim = Label(canvas, text="Limites: 1ms-64ms")
IPI_lim.place(relx=0.6, rely=0.55, relwidth=0.4, relheight=0.075)
ITI_text = Label(canvas, text="Intervalo ITI (s):")
ITI_text.place(relx=0, rely=0.625, relwidth=0.4, relheight=0.075)
ITI_entry = Entry(canvas, justify=CENTER)
ITI_entry.place(relx=0.4, rely=0.625, relwidth=0.3, relheight=0.075)
ITI_lim = Label(canvas, text="Limites: 1s-4s")
ITI_lim.place(relx=0.6, rely=0.625, relwidth=0.4, relheight=0.075)
dur_text = Label(canvas, text="Duração (min):")
dur_text.place(relx=0, rely=0.7, relwidth=0.4, relheight=0.075)
dur_entry = Entry(canvas, justify=CENTER)
dur_entry.place(relx=0.4, rely=0.7, relwidth=0.3, relheight=0.075)
dur_lim = Label(canvas, text="Limites: 1min - 30min")
dur_lim.place(relx=0.6, rely=0.7, relwidth=0.4, relheight=0.075)
button_par_1 = Button(canvas, text="Selecionar parâmetros", command = lambda:select_par_basal(canvas, T_entry.get(), IPI_entry.get(), ITI_entry.get(), dur_entry.get()))
button_par_1.place(relx=0, rely=0.775, relwidth=1, relheight=0.075)
if (canvas==canvas1):
global prot1
prot1 = 'bu'
if (canvas==canvas2):
global prot2
prot2 = 'bu'
if (canvas==canvas3):
global prot3
prot3 = 'bu'
if (protocol=="TBS unipolar"):
prot_image = Image.open("protocolo_TBS.PNG")
prot_photo = ImageTk.PhotoImage(prot_image.resize((450,130)))
prot_label = Label(canvas, image=prot_photo)
prot_label.image = prot_photo
prot_label.place(relx=0.0, rely=0.225, relwidth=1, relheight=0.25)
T_text = Label(canvas, text="Intervalo T (ms):")
T_text.place(relx=0, rely=0.475, relwidth=0.4, relheight=0.05)
T_entry = Entry(canvas, justify=CENTER)
T_entry.place(relx=0.4, rely=0.475, relwidth=0.3, relheight=0.05)
T_lim = Label(canvas, text="Limites: 1ms-100ms")
T_lim.place(relx=0.6, rely=0.475, relwidth=0.4, relheight=0.05)
IPI_text = Label(canvas, text="Intervalo IPI (ms):")
IPI_text.place(relx=0, rely=0.525, relwidth=0.4, relheight=0.05)
IPI_entry = Entry(canvas, justify=CENTER)
IPI_entry.place(relx=0.4, rely=0.525, relwidth=0.3, relheight=0.05)
IPI_lim = Label(canvas, text="Limites: 1ms-100ms")
IPI_lim.place(relx=0.6, rely=0.525, relwidth=0.4, relheight=0.05)
n_ep_text = Label(canvas, text="Nº de epochs por trem:")
n_ep_text.place(relx=0, rely=0.575, relwidth=0.4, relheight=0.05)
n_ep_entry = Entry(canvas, justify=CENTER)
n_ep_entry.place(relx=0.4, rely=0.575, relwidth=0.3, relheight=0.05)
n_ep_lim = Label(canvas, text="Limites: 1-20")
n_ep_lim.place(relx=0.6, rely=0.575, relwidth=0.4, relheight=0.05)
IEI_text = Label(canvas, text="Intervalo IEI (ms):")
IEI_text.place(relx=0, rely=0.625, relwidth=0.4, relheight=0.05)
IEI_entry = Entry(canvas, justify=CENTER)
IEI_entry.place(relx=0.4, rely=0.625, relwidth=0.3, relheight=0.05)
IEI_lim = Label(canvas, text="Limites: 200ms-1000ms")
IEI_lim.place(relx=0.6, rely=0.625, relwidth=0.4, relheight=0.05)
ITI_text = Label(canvas, text="Intervalo ITI (s):")
ITI_text.place(relx=0, rely=0.675, relwidth=0.4, relheight=0.05)
ITI_entry = Entry(canvas, justify=CENTER)
ITI_entry.place(relx=0.4, rely=0.675, relwidth=0.3, relheight=0.05)
ITI_lim = Label(canvas, text="Limites: 1s-20s")
ITI_lim.place(relx=0.6, rely=0.675, relwidth=0.4, relheight=0.05)
dur_text = Label(canvas, text="Duração (min):")
dur_text.place(relx=0, rely=0.725, relwidth=0.4, relheight=0.05)
dur_entry = Entry(canvas, justify=CENTER)
dur_entry.place(relx=0.4, rely=0.725, relwidth=0.3, relheight=0.05)
dur_lim = Label(canvas, text="Limites: 1min - 30min")
dur_lim.place(relx=0.6, rely=0.725, relwidth=0.4, relheight=0.05)
button_par_1 = Button(canvas, text="Selecionar parâmetros", command = lambda:select_par_TBS(canvas, T_entry.get(), IPI_entry.get(), n_ep_entry.get(), IEI_entry.get(), ITI_entry.get(), dur_entry.get()))
button_par_1.place(relx=0, rely=0.775, relwidth=1, relheight=0.05)
if (canvas==canvas1):
prot1 = 'tu'
if (canvas==canvas2):
prot2 = 'tu'
if (canvas==canvas3):
prot3 = 'tu'
if (protocol=="PBS"):
prot_image = Image.open("protocolo_PBS.PNG")
prot_photo = ImageTk.PhotoImage(prot_image.resize((450,140)))
prot_label = Label(canvas, image=prot_photo)
prot_label.image = prot_photo
prot_label.place(relx=0.0, rely=0.225, relwidth=1, relheight=0.25)
PP_text = Label(canvas, text="Intervalo do pulso PP(us):")
PP_text.place(relx=0, rely=0.475, relwidth=0.4, relheight=0.05)
PP_entry = Entry(canvas, justify=CENTER)
PP_entry.place(relx=0.4, rely=0.475, relwidth=0.3, relheight=0.05)
PP_lim = Label(canvas, text="Limites: 5us-1000us")
PP_lim.place(relx=0.6, rely=0.475, relwidth=0.4, relheight=0.05)
PPI_text = Label(canvas, text="Intervalo PPI (ms):")
PPI_text.place(relx=0, rely=0.525, relwidth=0.4, relheight=0.05)
PPI_entry = Entry(canvas, justify=CENTER)
PPI_entry.place(relx=0.4, rely=0.525, relwidth=0.3, relheight=0.05)
PPI_lim = Label(canvas, text="Limites: 1ms-64ms")
PPI_lim.place(relx=0.6, rely=0.525, relwidth=0.4, relheight=0.05)
T_text = Label(canvas, text="Intervalo T (us):")
T_text.place(relx=0, rely=0.575, relwidth=0.4, relheight=0.05)
T_entry = Entry(canvas, justify=CENTER)
T_entry.place(relx=0.4, rely=0.575, relwidth=0.3, relheight=0.05)
T_lim = Label(canvas, text="Limites: 5us-1000us")
T_lim.place(relx=0.6, rely=0.575, relwidth=0.4, relheight=0.05)
IPI_text = Label(canvas, text="Intervalo IPI (us):")
IPI_text.place(relx=0, rely=0.625, relwidth=0.4, relheight=0.05)
IPI_entry = Entry(canvas, justify=CENTER)
IPI_entry.place(relx=0.4, rely=0.625, relwidth=0.3, relheight=0.05)
IPI_lim = Label(canvas, text="Limites: 5us-1000us")
IPI_lim.place(relx=0.6, rely=0.625, relwidth=0.4, relheight=0.05)
IBI_text = Label(canvas, text="Intervalo IBI (ms):")
IBI_text.place(relx=0, rely=0.675, relwidth=0.4, relheight=0.05)
IBI_entry = Entry(canvas, justify=CENTER)
IBI_entry.place(relx=0.4, rely=0.675, relwidth=0.3, relheight=0.05)
IBI_lim = Label(canvas, text="Limites: 200ms-1000ms")
IBI_lim.place(relx=0.6, rely=0.675, relwidth=0.4, relheight=0.05)
dur_text = Label(canvas, text="Duração (min):")
dur_text.place(relx=0, rely=0.725, relwidth=0.4, relheight=0.05)
dur_entry = Entry(canvas, justify=CENTER)
dur_entry.place(relx=0.4, rely=0.725, relwidth=0.3, relheight=0.05)
dur_lim = Label(canvas, text="Limites: 1min - 30min")
dur_lim.place(relx=0.6, rely=0.725, relwidth=0.4, relheight=0.05)
button_par_1 = Button(canvas, text="Selecionar parâmetros", command = lambda:select_par_PBS(canvas, PP_entry.get(), PPI_entry.get(), T_entry.get(), IPI_entry.get(), IBI_entry.get(), dur_entry.get()))
button_par_1.place(relx=0, rely=0.775, relwidth=1, relheight=0.05)
if (canvas==canvas1):
prot1 = 'pb'
if (canvas==canvas2):
prot2 = 'pb'
if (canvas==canvas3):
prot3 = 'pb'
#Função para interface de seleção dos parâmetros de protocolo Basal unipolar
def select_par_basal(canvas, T, IPI, ITI, dur):
try:
T_f = int(T)
IPI_f = int(IPI)
ITI_f = int(ITI)
dur_f = int(dur)
n_train_calc = (60*dur_f)/((2*((T_f/1000000)+(IPI_f/1000)))+ITI_f)
n_train_calc = int(n_train_calc)
n_train_text = Label(canvas, text="Nº de trens do protocolo:")
n_train_text.place(relx=0, rely=0.85, relwidth=0.5, relheight=0.075)
n_train_text_var = Label(canvas, text=n_train_calc)
n_train_text_var.place(relx=0.5, rely=0.85, relwidth=0.5, relheight=0.075)
if (canvas==canvas1):
global T_f_1
global IPI_f_1
global ITI_f_1
global dur_f_1
T_f_1 = T_f
IPI_f_1 = IPI_f
ITI_f_1 = ITI_f
dur_f_1 = dur_f
if (canvas==canvas2):
global T_f_2
global IPI_f_2
global ITI_f_2
global dur_f_2
T_f_2 = T_f
IPI_f_2 = IPI_f
ITI_f_2 = ITI_f
dur_f_2 = dur_f
if (canvas==canvas3):
global T_f_3
global IPI_f_3
global ITI_f_3
global dur_f_3
T_f_3 = T_f
IPI_f_3 = IPI_f
ITI_f_3 = ITI_f
dur_f_3 = dur_f
writeToLog("Parâmetros definidos.")
except:
writeToLog("Algum erro ocorreu. Tente preencher todos os parâmetros.")
#Função para interface de seleção dos parâmetros de protocolo TBS unipolar
def select_par_TBS(canvas, T, IPI, n_ep, IEI, ITI, dur):
try:
T_f = int(T)
IPI_f = int(IPI)
n_ep_f = int(n_ep)
IEI_f = int(IEI)
ITI_f = int(ITI)
dur_f = int(dur)
n_train_calc = (60*dur_f)/(((4*(T_f+IPI_f)/1000)+IEI_f/1000)*n_ep_f + ITI_f)
n_train_calc = int(n_train_calc)
n_train_text = Label(canvas, text="Nº de trens do protocolo:")
n_train_text.place(relx=0, rely=0.775, relwidth=0.5, relheight=0.075)
n_train_text_var = Label(canvas, text=n_train_calc)
n_train_text_var.place(relx=0.5, rely=0.775, relwidth=0.5, relheight=0.075)
f_p_calc = 1000/(T_f+IPI_f)
f_p_text = Label(canvas, text="Frequência dos pulsos (Hz):")
f_p_text.place(relx=0, rely=0.85, relwidth=0.5, relheight=0.075)
f_p_text_var = Label(canvas, text=f_p_calc)
f_p_text_var.place(relx=0.5, rely=0.85, relwidth=0.5, relheight=0.075)
if (canvas==canvas1):
global T_f_1
global IPI_f_1
global n_ep_f_1
global IEI_f_1
global ITI_f_1
global dur_f_1
T_f_1 = T_f
IPI_f_1 = IPI_f
n_ep_f_1 = n_ep_f
IEI_f_1 = IEI_f
ITI_f_1 = ITI_f
dur_f_1 = dur_f
if (canvas==canvas2):
global T_f_2
global IPI_f_2
global n_ep_f_2
global IEI_f_2
global ITI_f_2
global dur_f_2
T_f_2 = T_f
IPI_f_2 = IPI_f
n_ep_f_2 = n_ep_f
IEI_f_2 = IEI_f
ITI_f_2 = ITI_f
dur_f_2 = dur_f
if (canvas==canvas3):
global T_f_3
global IPI_f_3
global n_ep_f_3
global IEI_f_3
global ITI_f_3
global dur_f_3
T_f_3 = T_f
IPI_f_3 = IPI_f
n_ep_f_3 = n_ep_f
IEI_f_3 = IEI_f
ITI_f_3 = ITI_f
dur_f_3 = dur_f
writeToLog("Parâmetros definidos.")
except:
writeToLog("Algum erro ocorreu. Tente preencher todos os parâmetros.")
#Função para interface de seleção dos parâmetros de protocolo PBS
def select_par_PBS(canvas, PP, PPI, T, IPI, IBI, dur):
try:
PP_f = int(PP)
PPI_f = int(PPI)
T_f = int(T)
IPI_f = int(IPI)
IBI_f = int(IBI)
dur_f = int(dur)
n_train_calc = (60*dur_f)/(((4*(T_f+IPI_f)/1000)+IEI_f/1000)*n_ep_f + ITI_f)
n_train_calc = int(n_train_calc)
n_train_text = Label(canvas, text="Nº de trens do protocolo:")
n_train_text.place(relx=0, rely=0.825, relwidth=0.5, relheight=0.075)
n_train_text_var = Label(canvas, text=n_train_calc)
n_train_text_var.place(relx=0.5, rely=0.825, relwidth=0.5, relheight=0.075)
f_p_calc = 1000/(T_f+IPI_f)
f_p_text = Label(canvas, text="Frequência dos pulsos (Hz):")
f_p_text.place(relx=0, rely=0.875, relwidth=0.5, relheight=0.075)
f_p_text_var = Label(canvas, text=f_p_calc)
f_p_text_var.place(relx=0.5, rely=0.875, relwidth=0.5, relheight=0.075)
if (canvas==canvas1):
global PP_f_1
global PPI_f_1
global T_f_1
global IPI_f_1
global IBI_f_1
global dur_f_1
PP_f_1 = PP_f
PPI_f_1 = PPI_f
T_f_1 = T_f
IPI_f_1 = IPI_f
IBI_f_1 = IBI_f
dur_f_1 = dur_f
if (canvas==canvas2):
global PP_f_2
global PPI_f_2
global T_f_2
global IPI_f_2
global IBI_f_2
global dur_f_2
PP_f_2 = PP_f
PPI_f_2 = PPI_f
T_f_2 = T_f
IPI_f_2 = IPI_f
IBI_f_2 = IBI_f
dur_f_2 = dur_f
if (canvas==canvas3):
global PP_f_3
global PPI_f_3
global T_f_3
global IPI_f_3
global IBI_f_3
global dur_f_3
PP_f_3 = PP_f
PPI_f_3 = PPI_f
T_f_3 = T_f
IPI_f_3 = IPI_f
IBI_f_3 = IBI_f
dur_f_3 = dur_f
writeToLog("Parâmetros definidos.")
except:
writeToLog("Algum erro ocorreu. Tente preencher todos os parâmetros.")
#Função para detecção automática de Arduino e sua porta
def detect_arduino():
ports = list(serial.tools.list_ports.comports())
global Arduino_ports
Arduino_ports=[]
for p in ports:
if 'Arduino' in p.description:
Arduino_ports.append(p)
if len(Arduino_ports)==1:
writeToLog("Arduino detectado. PORTA: %s" %Arduino_ports[0].device)
else:
writeToLog("Nenhum Arduino detectado.")
if len(Arduino_ports)>=1:
try:
global arduino
arduino=serial.Serial(Arduino_ports[0].device,9600, timeout=2)
writeToLog("Conexão com Arduino estabelecida.")
time.sleep(1)
arduino.write(b'1')
arduino.close()
except:
writeToLog("Problema na conexão com Arduino. Reconectar USB.")
#Função de envio de dados para Arduino
def enviar_arduino():
try:
arduino=serial.Serial(Arduino_ports[0].device,9600, timeout=2)
time.sleep(1)
arduino.write(b'0')
arduino.close()
except:
writeToLog("Não foi possível enviar. Problema na conexão com Arduino.")
root.mainloop()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment