所以我在Matlab中有一个程序,它以随机顺序发送1到7之间的数字,我的python代码在接收到数字时必须点亮相应的单词(每个单词都是与之相关的特定数字)。这必须尽可能快,因为这是一个大脑计算机接口,而且时间安排非常重要。
正在发生的是:当我只打印从Matlab接收到的值时,不会出现延迟,我会收到这样的消息:
b'1\n'
b'7\n'
b'4\n'
b'2\n'
b'6\n'
b'3\n'
b'1\n'
b'5\n'
etc..
但是当我包括GUI(我使用过Tkinter)部分并打印值时,它给了我:
b'1\n'
1
b'7\n'
7
b'4\n'
4
b'2\n'
2
b'6\n'
6
b'3\n'
3
b'1\n5\n1\n'
1
5
1
b'4\n3\n7\n'
4
3
7
b'6\n2\n5\n7\n2\n'
6
2
5
etc...
因此,当它一次开始接收多个号码时,会大大降低程序速度。 在我看来,要么tkinter变慢,要么循环不正确,但这很奇怪,因为它只有几行代码:
from tkinter import *
import time
import winsound
import socket
import sys
sounds = ["sim_esquerda_elev0_azim100.wav","nao_direita_elev0_azim100.wav",\
"fome_esquerda_elev30_azim120.wav", "sede_direita_elev-40_azim148.wav",\
"urinar_esquerda_elev-40_azim32.wav","ar_frente_elev0_azim0.wav",\
"posicao_direita_elev30_azim60.wav"]
# TCP connection
try:
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
print ("socket creation failed with error %s" %(err))
#default port for socket
port = 2000
# default time out
#so.settimeout(1000000)
try:
host_ip = socket.gethostbyname('localhost')
except socket.gaierror:
# this means could not resolve the host
print ("there was an error resolving the host")
sys.exit()
# connecting to the server
so.connect((host_ip,port))
# MATLAB INFORMATION FOR OFFLINE EXPERIMENT
Nepoch = 10 #nr de epochs por trial
Nwords = 7 #nr de palavras (SIM, NAO, FOME, SEDE, URINAR, AR, POSICAO)
SeqTrain = [1, 3, 5, 7, 2, 4, 6, 1, 3, 5, 7, 2, 4, 6] #sequencia offline de treino
# read the TCP sequence received
def sequencia():
num = 0
for i in range(0,999):
s = so.recv(port) #+ b'\n' #since the sequence received is : 1\n 2\n 5\n etc
print(s)
for c in s.split():
y = int(c)
#print(c)
#feedbak offline (for the user to know which are the words)
if (num in (0, Nepoch*Nwords+1, Nepoch*Nwords*2+2, Nepoch*Nwords*3+3, Nepoch*Nwords*4+4, Nepoch*Nwords*5+5,\
Nepoch*Nwords*6+6)):
labels1[y-1].configure(foreground="white")
root.update()
elif (num in (Nepoch*Nwords*7+7, Nepoch*Nwords*8+8, Nepoch*Nwords*9+9, Nepoch*Nwords*10+10,\
Nepoch*Nwords*11+11, Nepoch*Nwords*12+12, Nepoch*Nwords*13+13)):
labels2[y-1].configure(foreground="white")
root.update()
else:
labels[y-1].configure(background="green",foreground="red")
root.update()
winsound.PlaySound(sounds[y-1], winsound.SND_FILENAME)
labels[y-1].configure(background="gray",foreground="white")
root.update()
num = num + 1
#window
root = Tk()
root.title("Sequencia")
#root.attributes('-fullscreen', True) #for fullscreen
#root.bind('<Escape>',lambda e: root.destroy()) #to get out of the full screen
root.state('zoomed') #just zoomed not fullscreen
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
fm = Frame(root, width=w, height=h, bg="black")
#draw center lines
#wi = Canvas(fm, width=20, height=20, bg ="green", selectborderwidth=0)
#wi.create_line(0, 200, 150, 150, width=10, fill="blue")
#~~~~~~~~~~~~~LABELS FOR THE OFFLINE TEST TO SHOW UP IN THE SCREEN~~~~~~~~~~~~~
label_sim1 = Label(fm, width=10, height=1, bg="black", text = "SIM", font = ('TkDefaultFont',15), fg="black")
label_nao1 = Label(fm, width=10, height=1, bg="black", text = "NÃO", font = ('TkDefaultFont',15), fg="black")
label_fome1 = Label(fm, width=10, height=1, bg="black", text = "FOME", font = ('TkDefaultFont',15), fg="black")
label_sede1 = Label(fm, width=10, height=1, bg="black", text = "SEDE", font = ('TkDefaultFont',15), fg="black")
label_urinar1 = Label(fm, width=10, height=1, bg="black", text = "URINAR", font = ('TkDefaultFont',15), fg="black")
label_ar1 = Label(fm, width=10, height=1, bg="black", text = "AR", font = ('TkDefaultFont',15), fg="black")
label_posicao1 = Label(fm, width=10, height=1, bg="black", text = "POSIÇÃO", font = ('TkDefaultFont',15), fg="black")
labels1 = [label_sim1,label_nao1,label_fome1,label_sede1,label_urinar1,label_ar1,label_posicao1]
label_sim2 = Label(fm, width=10, height=1, bg="black", text = "SIM", font = ('TkDefaultFont',15), fg="black")
label_nao2 = Label(fm, width=10, height=1, bg="black", text = "NÃO", font = ('TkDefaultFont',15), fg="black")
label_fome2 = Label(fm, width=10, height=1, bg="black", text = "FOME", font = ('TkDefaultFont',15), fg="black")
label_sede2 = Label(fm, width=10, height=1, bg="black", text = "SEDE", font = ('TkDefaultFont',15), fg="black")
label_urinar2 = Label(fm, width=10, height=1, bg="black", text = "URINAR", font = ('TkDefaultFont',15), fg="black")
label_ar2 = Label(fm, width=10, height=1, bg="black", text = "AR", font = ('TkDefaultFont',15), fg="black")
label_posicao2 = Label(fm, width=10, height=1, bg="black", text = "POSIÇÃO", font = ('TkDefaultFont',15), fg="black")
labels2 = [label_sim2,label_nao2,label_fome2,label_sede2,label_urinar2,label_ar2,label_posicao2]
#posicionamento das palavras: [1, 3, 5, 7, 2, 4, 6, 1, 3, 5, 7, 2, 4, 6] sim, fome, urinar, posição, nao, sede, ar, sim, fome, urinar, posição, nao, sede, ar
fm.pack()
label_sim1.place(relx=0.09, rely=0.07, anchor=CENTER)
label_fome1.place(relx=0.14, rely=0.07, anchor=CENTER)
label_urinar1.place(relx=0.2, rely=0.07, anchor=CENTER)
label_posicao1.place(relx=0.27, rely=0.07, anchor=CENTER)
label_nao1.place(relx=0.32, rely=0.07, anchor=CENTER)
label_sede1.place(relx=0.37, rely=0.07, anchor=CENTER)
label_ar1.place(relx=0.42, rely=0.07, anchor=CENTER)
label_sim2.place(relx=0.46, rely=0.07, anchor=CENTER)
label_fome2.place(relx=0.515, rely=0.07, anchor=CENTER)
label_urinar2.place(relx=0.575, rely=0.07, anchor=CENTER)
label_posicao2.place(relx=0.645, rely=0.07, anchor=CENTER)
label_nao2.place(relx=0.695, rely=0.07, anchor=CENTER)
label_sede2.place(relx=0.745, rely=0.07, anchor=CENTER)
label_ar2.place(relx=0.80, rely=0.07, anchor=CENTER)
#~~~~~~~~~~LABELS OF THE PARADIGM~~~~~~~~~~
label_sim = Label(fm, width=10, height=1, bg="gray", text = "SIM", font = ('TkDefaultFont',28), fg="white")
label_nao = Label(fm, width=10, height=1, bg="gray", text = "NÃO", font = ('TkDefaultFont',28), fg="white")
label_fome = Label(fm, width=10, height=1, bg="gray", text = "FOME", font = ('TkDefaultFont',28), fg="white")
label_sede = Label(fm, width=10, height=1, bg="gray", text = "SEDE", font = ('TkDefaultFont',28), fg="white")
label_urinar = Label(fm, width=10, height=1, bg="gray", text = "URINAR", font = ('TkDefaultFont',28), fg="white")
label_ar = Label(fm, width=10, height=1, bg="gray", text = "AR", font = ('TkDefaultFont',28), fg="white")
label_posicao = Label(fm, width=10, height=1, bg="gray", text = "POSIÇÃO", font = ('TkDefaultFont',28), fg="white")
labels = [label_sim,label_nao,label_fome,label_sede,label_urinar,label_ar,label_posicao]
#ordem de sequencia (1 2 3 4 5 6 7): (SIM, NAO, FOME, SEDE, URINAR, AR, POSICAO)
#posicionamento das palavras:
fm.pack()
label_ar.place(relx=0.5, rely=0.15, anchor=CENTER)
label_posicao.place(relx=0.70, rely=0.3, anchor=CENTER)
label_urinar.place(relx=0.3, rely=0.3, anchor=CENTER)
label_nao.place(relx=0.72, rely=0.55, anchor=CENTER)
label_sim.place(relx=0.28, rely=0.55, anchor=CENTER)
label_sede.place(relx=0.60, rely=0.77, anchor=CENTER)
label_fome.place(relx=0.4, rely=0.77, anchor=CENTER)
root.update()
sequencia()
您对此有何想法?或改善建议?我已经尝试过用线程分离代码,但是没有用。 Tkinter是一个不好的选择吗?甚至是python? 非常感谢
答案 0 :(得分:0)
调用update
可能是减慢循环速度的主要因素。每次调用它时,它将尝试服务所有未决事件。您应该考虑每隔几百毫秒仅调用一次。
此外,播放声音将非常慢,因为您必须等待声音播放才能继续循环。