首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用python和tkinter作为实时GUI时有一些延迟

在使用python和tkinter作为实时GUI时有一些延迟
EN

Stack Overflow用户
提问于 2019-05-29 21:54:37
回答 1查看 37关注 0票数 0

所以我在matlab中有一个程序,它以随机的顺序发送从1到7的数字,当我的python代码收到数字时,必须点亮相应的单词(每个单词都是一个与其相关的特定数字)。这必须尽可能快,因为这是一个脑机接口,计时非常重要。

发生的情况是:当我简单地打印从matlab接收到的值时,没有出现延迟,我收到的结果是这样的:

代码语言:javascript
复制
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)部分并打印值时,它会给我:

代码语言:javascript
复制
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速度太慢,要么是循环不正确,但这很奇怪,因为它只有几行代码:

代码语言:javascript
复制
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是个糟糕的选择吗?或者甚至是蟒蛇?非常感谢

EN

回答 1

Stack Overflow用户

发布于 2019-05-29 22:39:33

update的调用可能是降低循环速度的主要原因。每次调用它时,它都会尝试为所有挂起的事件提供服务。您应该考虑每几百毫秒只调用它一次。

此外,播放声音将非常慢,因为您必须等待声音播放,然后循环才能继续。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56362278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档